private static boolean generateDelegate(GrChangeInfoImpl grInfo) { final GrMethod method = grInfo.getMethod(); final PsiClass psiClass = method.getContainingClass(); GrMethod newMethod = (GrMethod) method.copy(); newMethod = (GrMethod) psiClass.addAfter(newMethod, method); StringBuilder buffer = new StringBuilder(); buffer.append("\n"); if (method.isConstructor()) { buffer.append("this"); } else { if (!PsiType.VOID.equals(method.getReturnType())) { buffer.append("return "); } buffer.append( GrChangeSignatureUtil.getNameWithQuotesIfNeeded( grInfo.getNewName(), method.getProject())); } generateParametersForDelegateCall(grInfo, method, buffer); final GrCodeBlock codeBlock = GroovyPsiElementFactory.getInstance(method.getProject()) .createMethodBodyFromText(buffer.toString()); newMethod.setBlock(codeBlock); newMethod.getModifierList().setModifierProperty(PsiModifier.ABSTRACT, false); CodeStyleManager.getInstance(method.getProject()).reformat(newMethod); return processPrimaryMethodInner(grInfo, method, null); }
private static PsiStatement addInitializationToConstructors( PsiLocalVariable local, PsiField field, PsiMethod enclosingConstructor, PsiElementFactory factory) throws IncorrectOperationException { PsiClass aClass = field.getContainingClass(); PsiMethod[] constructors = aClass.getConstructors(); PsiStatement assignment = createAssignment(local, field.getName(), factory); boolean added = false; for (PsiMethod constructor : constructors) { if (constructor == enclosingConstructor) continue; PsiCodeBlock body = constructor.getBody(); if (body == null) continue; PsiStatement[] statements = body.getStatements(); if (statements.length > 0) { PsiStatement first = statements[0]; if (first instanceof PsiExpressionStatement) { PsiExpression expression = ((PsiExpressionStatement) first).getExpression(); if (expression instanceof PsiMethodCallExpression) { @NonNls String text = ((PsiMethodCallExpression) expression).getMethodExpression().getText(); if ("this".equals(text)) { continue; } if ("super".equals(text) && enclosingConstructor == null && PsiTreeUtil.isAncestor(constructor, local, false)) { local.delete(); return (PsiStatement) body.addAfter(assignment, first); } } } if (enclosingConstructor == null && PsiTreeUtil.isAncestor(constructor, local, false)) { local.delete(); return (PsiStatement) body.addBefore(assignment, first); } } assignment = (PsiStatement) body.add(assignment); added = true; } if (!added && enclosingConstructor == null) { if (aClass instanceof PsiAnonymousClass) { final PsiClassInitializer classInitializer = (PsiClassInitializer) aClass.addAfter(factory.createClassInitializer(), field); assignment = (PsiStatement) classInitializer.getBody().add(assignment); } else { PsiMethod constructor = (PsiMethod) aClass.add(factory.createConstructor()); assignment = (PsiStatement) constructor.getBody().add(assignment); } } if (enclosingConstructor == null) local.delete(); return assignment; }
public static void createFieldAndAddAssignment( final @NotNull Project project, final @NotNull PsiClass targetClass, final @NotNull PsiMethod method, final @NotNull PsiParameter parameter, final @NotNull PsiType fieldType, final @NotNull String fieldName, final boolean isStatic, final boolean isFinal) throws IncorrectOperationException { PsiManager psiManager = PsiManager.getInstance(project); PsiElementFactory factory = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory(); PsiField field = factory.createField(fieldName, fieldType); PsiModifierList modifierList = field.getModifierList(); if (modifierList == null) return; modifierList.setModifierProperty(PsiModifier.STATIC, isStatic); modifierList.setModifierProperty(PsiModifier.FINAL, isFinal); NullableNotNullManager.getInstance(project).copyNullableOrNotNullAnnotation(parameter, field); PsiCodeBlock methodBody = method.getBody(); if (methodBody == null) return; PsiStatement[] statements = methodBody.getStatements(); Ref<Pair<PsiField, Boolean>> anchorRef = new Ref<Pair<PsiField, Boolean>>(); int i = findFieldAssignmentAnchor(statements, anchorRef, targetClass, parameter); Pair<PsiField, Boolean> fieldAnchor = anchorRef.get(); String stmtText = fieldName + " = " + parameter.getName() + ";"; if (fieldName.equals(parameter.getName())) { @NonNls String prefix = isStatic ? targetClass.getName() == null ? "" : targetClass.getName() + "." : "this."; stmtText = prefix + stmtText; } PsiStatement assignmentStmt = factory.createStatementFromText(stmtText, methodBody); assignmentStmt = (PsiStatement) CodeStyleManager.getInstance(project).reformat(assignmentStmt); if (i == statements.length) { methodBody.add(assignmentStmt); } else { methodBody.addAfter(assignmentStmt, i > 0 ? statements[i - 1] : null); } if (fieldAnchor != null) { PsiVariable psiVariable = fieldAnchor.getFirst(); psiVariable.normalizeDeclaration(); } if (targetClass.findFieldByName(fieldName, false) == null) { if (fieldAnchor != null) { Boolean insertBefore = fieldAnchor.getSecond(); PsiField inField = fieldAnchor.getFirst(); if (insertBefore.booleanValue()) { targetClass.addBefore(field, inField); } else { targetClass.addAfter(field, inField); } } else { targetClass.add(field); } } }