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); }
@Nullable public static PsiType inferReturnType(PsiElement position) { final GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(position); if (flowOwner == null) return null; final PsiElement parent = flowOwner.getContext(); if (flowOwner instanceof GrOpenBlock && parent instanceof GrMethod) { final GrMethod method = (GrMethod) parent; if (method.isConstructor()) return null; return method.getReturnType(); } return null; }
@Nullable public static GrConstructorInvocation getChainingConstructorInvocation(GrMethod constructor) { if (constructor instanceof GrReflectedMethod && ((GrReflectedMethod) constructor).getSkippedParameters().length > 0) return null; LOG.assertTrue(constructor.isConstructor()); GrOpenBlock body = constructor.getBlock(); if (body == null) return null; GrStatement[] statements = body.getStatements(); if (statements.length > 0 && statements[0] instanceof GrConstructorInvocation) { return (GrConstructorInvocation) statements[0]; } return null; }
public void generateMethodBody(GrMethod method) { final GrOpenBlock block = method.getBlock(); boolean shouldInsertReturnNull; myExitPoints.clear(); PsiType returnType = context.typeProvider.getReturnType(method); if (!method.isConstructor() && returnType != PsiType.VOID) { myExitPoints.addAll(ControlFlowUtils.collectReturns(block)); shouldInsertReturnNull = block != null && !(returnType instanceof PsiPrimitiveType) && MissingReturnInspection.methodMissesSomeReturns( block, MissingReturnInspection.ReturnStatus.getReturnStatus(method)); } else { shouldInsertReturnNull = false; } if (block != null) { generateCodeBlock(block, shouldInsertReturnNull); } }