PsiVariable (@NotNull Project project, @NotNull PsiClass targetClass, @NotNull PsiMethod method, @NotNull PsiParameter myParameter, PsiType type, @NotNull String fieldName, boolean methodStatic, boolean isFinal) { GrOpenBlock block = ((GrMethod)method).getBlock(); if (block == null) return null; GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project); GrAssignmentExpression assignment = createAssignment(targetClass, myParameter, fieldName, methodStatic, factory); GrStatement anchor = getAnchor(block); GrStatement statement = block.addStatementBefore(assignment, anchor); JavaCodeStyleManager.getInstance(project).shortenClassReferences(statement); if (targetClass.findFieldByName(fieldName, false) == null) { String[] modifiers = getModifiers(methodStatic, isFinal); GrVariableDeclaration fieldDeclaration = factory.createFieldDeclaration(modifiers, fieldName, null, type); GrVariableDeclaration inserted = (GrVariableDeclaration)targetClass.add(fieldDeclaration); JavaCodeStyleManager.getInstance(project).shortenClassReferences(inserted); return inserted.getVariables()[0]; } return null; }