PsiField (@NotNull PsiClass targetClass, @NotNull PsiField field, @Nullable PsiElement place) { PsiMember enclosingContext = null; PsiClass parentClass; do { enclosingContext = PsiTreeUtil.getParentOfType(enclosingContext == null ? place : enclosingContext, PsiMethod.class, PsiField.class, PsiClassInitializer.class); parentClass = enclosingContext == null ? null : enclosingContext.getContainingClass(); } while (parentClass instanceof PsiAnonymousClass); return CommonJavaRefactoringUtil.appendField(targetClass, field, enclosingContext, null); }