Search in sources :

Example 26 with GrStatement

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

the class GroovyInlineMethodUtil method replaceAllOccurrencesWithExpression.

private static void replaceAllOccurrencesWithExpression(GrMethod method, GrCallExpression call, GrExpression oldExpression, GrParameter parameter) {
    Collection<PsiReference> refs = ReferencesSearch.search(parameter, new LocalSearchScope(method), false).findAll();
    final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(call.getProject());
    GrExpression expression = elementFactory.createExpressionFromText(oldExpression.getText());
    if (GroovyRefactoringUtil.hasSideEffect(expression) && refs.size() > 1 || !hasUnresolvableWriteAccess(refs, oldExpression)) {
        final String oldName = parameter.getName();
        final String newName = InlineMethodConflictSolver.suggestNewName(oldName, method, call);
        expression = elementFactory.createExpressionFromText(newName);
        final GrOpenBlock body = method.getBlock();
        final GrStatement[] statements = body.getStatements();
        GrStatement anchor = null;
        if (statements.length > 0) {
            anchor = statements[0];
        }
        body.addStatementBefore(elementFactory.createStatementFromText(createVariableDefinitionText(parameter, oldExpression, newName)), anchor);
    }
    for (PsiReference ref : refs) {
        PsiElement element = ref.getElement();
        if (element instanceof GrReferenceExpression) {
            ((GrReferenceExpression) element).replaceWithExpression(expression, true);
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 27 with GrStatement

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

the class GroovyInlineMethodUtil method checkTailOpenBlock.

private static boolean checkTailOpenBlock(GrOpenBlock block, Collection<GrStatement> returnStatements) {
    if (block == null)
        return false;
    GrStatement[] statements = block.getStatements();
    if (statements.length == 0)
        return false;
    GrStatement last = statements[statements.length - 1];
    if (returnStatements.contains(last)) {
        returnStatements.remove(last);
        return true;
    }
    if (last instanceof GrIfStatement) {
        return checkTailIfStatement(((GrIfStatement) last), returnStatements);
    }
    return false;
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 28 with GrStatement

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

the class GroovyInlineMethodUtil method checkTailIfStatement.

public static boolean checkTailIfStatement(GrIfStatement ifStatement, Collection<GrStatement> returnStatements) {
    GrStatement thenBranch = ifStatement.getThenBranch();
    GrStatement elseBranch = ifStatement.getElseBranch();
    if (elseBranch == null)
        return false;
    boolean tb = false;
    boolean eb = false;
    if (thenBranch instanceof GrReturnStatement) {
        tb = returnStatements.remove(thenBranch);
    } else if (thenBranch instanceof GrBlockStatement) {
        tb = checkTailOpenBlock(((GrBlockStatement) thenBranch).getBlock(), returnStatements);
    }
    if (elseBranch instanceof GrReturnStatement) {
        eb = returnStatements.remove(elseBranch);
    } else if (elseBranch instanceof GrBlockStatement) {
        eb = checkTailOpenBlock(((GrBlockStatement) elseBranch).getBlock(), returnStatements);
    }
    return tb && eb;
}
Also used : GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 29 with GrStatement

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

the class GroovyMethodInliner method getAloneResultExpression.

/**
   * Get method result expression (if it is alone in method)
   *
   * @return null if method has more or less than one return statement or has void type
   */
@Nullable
static GrExpression getAloneResultExpression(@NotNull GrMethod method) {
    GrOpenBlock body = method.getBlock();
    assert body != null;
    GrStatement[] statements = body.getStatements();
    if (statements.length == 1) {
        if (statements[0] instanceof GrExpression)
            return (GrExpression) statements[0];
        if (statements[0] instanceof GrReturnStatement) {
            GrExpression value = ((GrReturnStatement) statements[0]).getReturnValue();
            if (value == null && !PsiType.VOID.equals(PsiUtil.getSmartReturnType(method))) {
                return GroovyPsiElementFactory.getInstance(method.getProject()).createExpressionFromText("null");
            }
            return value;
        }
    }
    return null;
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 30 with GrStatement

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

the class ExtractUtil method createResultStatement.

@NotNull
private static GrStatement[] createResultStatement(ExtractInfoHelper helper) {
    VariableInfo[] outputVars = helper.getOutputVariableInfos();
    PsiType type = helper.getOutputType();
    GrStatement[] statements = helper.getStatements();
    GrMethodCallExpression callExpression = createMethodCall(helper);
    if ((outputVars.length == 0 || PsiType.VOID.equals(type)) && !helper.hasReturnValue())
        return new GrStatement[] { callExpression };
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject());
    if (helper.hasReturnValue()) {
        return new GrStatement[] { factory.createStatementFromText("return " + callExpression.getText()) };
    }
    LOG.assertTrue(outputVars.length > 0);
    final List<VariableInfo> mustAdd = mustAddVariableDeclaration(statements, outputVars);
    if (mustAdd.isEmpty()) {
        return new GrStatement[] { createAssignment(outputVars, callExpression, helper.getProject()) };
    } else if (mustAdd.size() == outputVars.length && outputVars.length == 1) {
        return new GrVariableDeclaration[] { factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, callExpression, outputVars[0].getType(), outputVars[0].getName()) };
    } else if (varsAreEqual(mustAdd, outputVars)) {
        return createTupleDeclaration(outputVars, callExpression, helper.getProject());
    } else {
        final List<GrStatement> result = generateVarDeclarations(mustAdd, helper.getProject(), null);
        result.add(createAssignment(outputVars, callExpression, helper.getProject()));
        return result.toArray(new GrStatement[result.size()]);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) VariableInfo(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo) PsiType(com.intellij.psi.PsiType) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)113 PsiElement (com.intellij.psi.PsiElement)36 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)26 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)22 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)21 GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)17 TextRange (com.intellij.openapi.util.TextRange)14 Nullable (org.jetbrains.annotations.Nullable)14 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)13 NotNull (org.jetbrains.annotations.NotNull)12 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)12 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)12 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)10 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)9 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)9 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 Document (com.intellij.openapi.editor.Document)6