void (@NotNull String varName, @NotNull String declarationText) { declarationText = String.format("public String %s = %s", varName, declarationText); PsiField field = myFactory.createFieldFromText(declarationText, null); PsiClass containingClass = myMethod.getContainingClass(); PsiElement previousField = PsiTreeUtil.findChildOfType(containingClass, PsiField.class); containingClass.addAfter(field, previousField); }