Search in sources :

Example 41 with GrOpenBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.

the class GroovyInlineMethodUtil method hasBadReturns.

private static boolean hasBadReturns(GrMethod method) {
    Collection<GrStatement> returnStatements = ControlFlowUtils.collectReturns(method.getBlock());
    GrOpenBlock block = method.getBlock();
    if (block == null || returnStatements.isEmpty())
        return false;
    boolean checked = checkTailOpenBlock(block, returnStatements);
    return !(checked && returnStatements.isEmpty());
}
Also used : GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 42 with GrOpenBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.

the class GroovyMethodInliner method inlineReferenceImpl.

@Nullable
static RangeMarker inlineReferenceImpl(@NotNull GrCallExpression call, @NotNull GrMethod method, boolean resultOfCallExplicitlyUsed, boolean isTailMethodCall, @Nullable Editor editor) {
    try {
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(call.getProject());
        final Project project = call.getProject();
        // Variable declaration for qualifier expression
        GrVariableDeclaration qualifierDeclaration = null;
        GrReferenceExpression innerQualifier = null;
        GrExpression qualifier = null;
        if (call instanceof GrMethodCallExpression && ((GrMethodCallExpression) call).getInvokedExpression() != null) {
            GrExpression invoked = ((GrMethodCallExpression) call).getInvokedExpression();
            if (invoked instanceof GrReferenceExpression && ((GrReferenceExpression) invoked).getQualifierExpression() != null) {
                qualifier = ((GrReferenceExpression) invoked).getQualifierExpression();
                if (PsiUtil.isSuperReference(qualifier)) {
                    qualifier = null;
                } else if (!GroovyInlineMethodUtil.isSimpleReference(qualifier)) {
                    String qualName = generateQualifierName(call, method, project, qualifier);
                    qualifier = (GrExpression) PsiUtil.skipParentheses(qualifier, false);
                    qualifierDeclaration = factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, qualifier, null, qualName);
                    innerQualifier = (GrReferenceExpression) factory.createExpressionFromText(qualName);
                } else {
                    innerQualifier = (GrReferenceExpression) qualifier;
                }
            }
        }
        GrMethod _method = prepareNewMethod(call, method, qualifier);
        GrExpression result = getAloneResultExpression(_method);
        if (result != null) {
            GrExpression expression = call.replaceWithExpression(result, false);
            TextRange range = expression.getTextRange();
            return editor != null ? editor.getDocument().createRangeMarker(range.getStartOffset(), range.getEndOffset(), true) : null;
        }
        GrMethod newMethod = prepareNewMethod(call, method, innerQualifier);
        String resultName = InlineMethodConflictSolver.suggestNewName("result", newMethod, call);
        // Add variable for method result
        Collection<GrStatement> returnStatements = ControlFlowUtils.collectReturns(newMethod.getBlock());
        final int returnCount = returnStatements.size();
        PsiType methodType = method.getInferredReturnType();
        GrOpenBlock body = newMethod.getBlock();
        assert body != null;
        GrExpression replaced;
        if (resultOfCallExplicitlyUsed && !isTailMethodCall) {
            GrExpression resultExpr = null;
            if (PsiType.VOID.equals(methodType)) {
                resultExpr = factory.createExpressionFromText("null");
            } else if (returnCount == 1) {
                final GrExpression returnExpression = ControlFlowUtils.extractReturnExpression(returnStatements.iterator().next());
                if (returnExpression != null) {
                    resultExpr = factory.createExpressionFromText(returnExpression.getText());
                }
            } else if (returnCount > 1) {
                resultExpr = factory.createExpressionFromText(resultName);
            }
            if (resultExpr == null) {
                resultExpr = factory.createExpressionFromText("null");
            }
            replaced = call.replaceWithExpression(resultExpr, false);
        } else {
            replaced = call;
        }
        // Calculate anchor to insert before
        GrExpression enclosingExpr = GroovyRefactoringUtil.addBlockIntoParent(replaced);
        GrVariableDeclarationOwner owner = PsiTreeUtil.getParentOfType(enclosingExpr, GrVariableDeclarationOwner.class);
        assert owner != null;
        PsiElement element = enclosingExpr;
        while (element != null && element.getParent() != owner) {
            element = element.getParent();
        }
        assert element != null && element instanceof GrStatement;
        GrStatement anchor = (GrStatement) element;
        if (!resultOfCallExplicitlyUsed) {
            assert anchor == enclosingExpr;
        }
        // add qualifier reference declaration
        if (qualifierDeclaration != null) {
            owner.addVariableDeclarationBefore(qualifierDeclaration, anchor);
        }
        // Process method return statements
        if (returnCount > 1 && !PsiType.VOID.equals(methodType) && !isTailMethodCall) {
            PsiType type = methodType != null && methodType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) ? null : methodType;
            GrVariableDeclaration resultDecl = factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, "", type, resultName);
            GrStatement statement = ((GrStatementOwner) owner).addStatementBefore(resultDecl, anchor);
            JavaCodeStyleManager.getInstance(statement.getProject()).shortenClassReferences(statement);
            // Replace all return statements with assignments to 'result' variable
            for (GrStatement returnStatement : returnStatements) {
                GrExpression value = ControlFlowUtils.extractReturnExpression(returnStatement);
                if (value != null) {
                    GrExpression assignment = factory.createExpressionFromText(resultName + " = " + value.getText());
                    returnStatement.replaceWithStatement(assignment);
                } else {
                    returnStatement.replaceWithStatement(factory.createExpressionFromText(resultName + " = null"));
                }
            }
        }
        if (!isTailMethodCall && resultOfCallExplicitlyUsed && returnCount == 1) {
            returnStatements.iterator().next().removeStatement();
        } else if (!isTailMethodCall && (PsiType.VOID.equals(methodType) || returnCount == 1)) {
            for (GrStatement returnStatement : returnStatements) {
                if (returnStatement instanceof GrReturnStatement) {
                    final GrExpression returnValue = ((GrReturnStatement) returnStatement).getReturnValue();
                    if (returnValue != null && GroovyRefactoringUtil.hasSideEffect(returnValue)) {
                        returnStatement.replaceWithStatement(returnValue);
                        continue;
                    }
                } else if (GroovyRefactoringUtil.hasSideEffect(returnStatement)) {
                    continue;
                }
                returnStatement.removeStatement();
            }
        }
        // Add all method statements
        GrStatement[] statements = body.getStatements();
        for (GrStatement statement : statements) {
            ((GrStatementOwner) owner).addStatementBefore(statement, anchor);
        }
        if (resultOfCallExplicitlyUsed && !isTailMethodCall) {
            TextRange range = replaced.getTextRange();
            RangeMarker marker = editor != null ? editor.getDocument().createRangeMarker(range.getStartOffset(), range.getEndOffset(), true) : null;
            reformatOwner(owner);
            return marker;
        } else {
            GrStatement stmt;
            if (isTailMethodCall && enclosingExpr.getParent() instanceof GrReturnStatement) {
                stmt = (GrReturnStatement) enclosingExpr.getParent();
            } else {
                stmt = enclosingExpr;
            }
            stmt.removeStatement();
            reformatOwner(owner);
            return null;
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return null;
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrVariableDeclarationOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) Project(com.intellij.openapi.project.Project) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) Nullable(org.jetbrains.annotations.Nullable)

Example 43 with GrOpenBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.

the class GrIntroduceFieldProcessor method initializeInConstructor.

private void initializeInConstructor(@NotNull GrVariable field, @NotNull PsiClass scope, @NotNull Collection<PsiElement> replaced) {
    PsiMethod[] constructors = scope.getConstructors();
    if (constructors.length == 0) {
        constructors = new PsiMethod[] { generateConstructor(scope) };
    }
    for (PsiMethod constructor : constructors) {
        final GrConstructorInvocation invocation = PsiImplUtil.getChainingConstructorInvocation((GrMethod) constructor);
        if (invocation != null && invocation.isThisCall())
            continue;
        final GrOpenBlock body = ((GrMethod) constructor).getBlock();
        final GrStatement anchor = findAnchorForAssignment(body, replaced);
        initializeInMethodInner(field, body, anchor, ContainerUtil.getFirstItem(replaced));
    }
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)

Example 44 with GrOpenBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.

the class ExtractClosureFromMethodProcessor method performRefactoring.

@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    final IntroduceParameterData data = new IntroduceParameterDataAdapter();
    IntroduceParameterUtil.processUsages(usages, data);
    final PsiMethod toSearchFor = (PsiMethod) myHelper.getToSearchFor();
    final boolean methodsToProcessAreDifferent = myMethod != toSearchFor;
    if (myHelper.generateDelegate()) {
        GroovyIntroduceParameterUtil.generateDelegate(myMethod, data.getParameterInitializer(), myProject);
        if (methodsToProcessAreDifferent) {
            final GrMethod method = GroovyIntroduceParameterUtil.generateDelegate(toSearchFor, data.getParameterInitializer(), myProject);
            final PsiClass containingClass = method.getContainingClass();
            if (containingClass != null && containingClass.isInterface()) {
                final GrOpenBlock block = method.getBlock();
                if (block != null) {
                    block.delete();
                }
            }
        }
    }
    // Changing signature of initial method
    // (signature of myMethodToReplaceIn will be either changed now or have already been changed)
    final FieldConflictsResolver fieldConflictsResolver = new FieldConflictsResolver(myHelper.getName(), myMethod.getBlock());
    IntroduceParameterUtil.changeMethodSignatureAndResolveFieldConflicts(new UsageInfo(myMethod), usages, data);
    if (methodsToProcessAreDifferent) {
        IntroduceParameterUtil.changeMethodSignatureAndResolveFieldConflicts(new UsageInfo(toSearchFor), usages, data);
    }
    // Replacing expression occurrences
    for (UsageInfo usage : usages) {
        if (usage instanceof ChangedMethodCallInfo) {
            PsiElement element = usage.getElement();
            GroovyIntroduceParameterUtil.processChangedMethodCall(element, myHelper, myProject);
        }
    }
    final GrStatement newStatement = ExtractUtil.replaceStatement(myDeclarationOwner, myHelper);
    final Editor editor = PsiUtilBase.findEditor(newStatement);
    if (editor != null) {
        PsiDocumentManager.getInstance(myProject).commitDocument(editor.getDocument());
        editor.getSelectionModel().removeSelection();
        editor.getCaretModel().moveToOffset(newStatement.getTextRange().getEndOffset());
    }
    fieldConflictsResolver.fix();
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) FieldConflictsResolver(org.jetbrains.plugins.groovy.refactoring.introduce.parameter.FieldConflictsResolver) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) Editor(com.intellij.openapi.editor.Editor) UsageInfo(com.intellij.usageView.UsageInfo) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo)

Example 45 with GrOpenBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.

the class GrFinalVariableAccessInspection method appendFieldsInitializedInClassInitializer.

private static void appendFieldsInitializedInClassInitializer(@NotNull GrClassInitializer[] initializers, @Nullable GrClassInitializer initializerToStop, boolean isStatic, @NotNull List<GrField> fields, @NotNull Set<GrVariable> initializedFields) {
    for (GrClassInitializer curInit : initializers) {
        if (curInit.isStatic() != isStatic)
            continue;
        if (curInit == initializerToStop)
            break;
        final GrOpenBlock block = curInit.getBlock();
        final Instruction[] flow = buildFlowForField(block);
        for (GrField field : fields) {
            if (field.hasModifierProperty(PsiModifier.STATIC) == isStatic && !initializedFields.contains(field) && VariableInitializationChecker.isVariableDefinitelyInitializedCached(field, block, flow)) {
                initializedFields.add(field);
            }
        }
    }
}
Also used : GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction)

Aggregations

GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)68 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)24 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)21 PsiElement (com.intellij.psi.PsiElement)13 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)10 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)9 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)9 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)8 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)8 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)7 Nullable (org.jetbrains.annotations.Nullable)6 NotNull (org.jetbrains.annotations.NotNull)5 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)5 Instruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)5 ReadWriteVariableInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction)5 TextRange (com.intellij.openapi.util.TextRange)4 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)4 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)4 UsageInfo (com.intellij.usageView.UsageInfo)3