Search in sources :

Example 6 with GrReturnStatement

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

the class InitialInfo method inferOutputType.

@Nullable
private PsiType inferOutputType(VariableInfo[] outputInfos, GrStatement[] statements, ArrayList<GrStatement> returnStatements, boolean hasReturnValue, StringPartInfo stringPartInfo) {
    if (stringPartInfo != null) {
        return stringPartInfo.getLiteral().getType();
    }
    PsiType outputType = PsiType.VOID;
    if (outputInfos.length > 0) {
        if (outputInfos.length == 1) {
            outputType = outputInfos[0].getType();
        } else {
            outputType = JavaPsiFacade.getElementFactory(myProject).createTypeFromText(CommonClassNames.JAVA_UTIL_LIST, getContext());
        }
    } else if (ExtractUtil.isSingleExpression(statements)) {
        outputType = ((GrExpression) statements[0]).getType();
    } else if (hasReturnValue) {
        assert !returnStatements.isEmpty();
        List<PsiType> types = new ArrayList<>(returnStatements.size());
        for (GrStatement statement : returnStatements) {
            if (statement instanceof GrReturnStatement) {
                GrExpression returnValue = ((GrReturnStatement) statement).getReturnValue();
                if (returnValue != null) {
                    types.add(returnValue.getType());
                }
            } else if (statement instanceof GrExpression) {
                types.add(((GrExpression) statement).getType());
            }
        }
        outputType = TypesUtil.getLeastUpperBoundNullable(types, getContext().getManager());
    }
    return outputType;
}
Also used : ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiType(com.intellij.psi.PsiType) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with GrReturnStatement

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

the class GroovyMethodInliner method isOnExpressionOrReturnPlace.

/*
  Method call is used as expression in some enclosing expression or
  is method return result
  */
private static boolean isOnExpressionOrReturnPlace(@NotNull GrCallExpression call) {
    PsiElement parent = call.getParent();
    if (!(parent instanceof GrVariableDeclarationOwner)) {
        return true;
    }
    // tail calls in methods and closures
    GrVariableDeclarationOwner owner = (GrVariableDeclarationOwner) parent;
    if (owner instanceof GrClosableBlock || owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod) {
        GrStatement[] statements = ((GrCodeBlock) owner).getStatements();
        assert statements.length > 0;
        GrStatement last = statements[statements.length - 1];
        if (last == call)
            return true;
        if (last instanceof GrReturnStatement && call == ((GrReturnStatement) last).getReturnValue()) {
            return true;
        }
    }
    return false;
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrVariableDeclarationOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 8 with GrReturnStatement

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

the class GroovyInlineMethodUtil method isTailMethodCall.

/**
   * Checks whether given method call is tail call of other method or closure
   *
   * @param call [tail?] Method call
   * @return
   */
static boolean isTailMethodCall(GrCallExpression call) {
    GrStatement stmt = call;
    PsiElement parent = call.getParent();
    // return statement
    if (parent instanceof GrReturnStatement) {
        stmt = ((GrReturnStatement) parent);
        parent = parent.getParent();
    }
    // method body result
    if (parent instanceof GrOpenBlock) {
        if (parent.getParent() instanceof GrMethod) {
            GrStatement[] statements = ((GrOpenBlock) parent).getStatements();
            return statements.length > 0 && stmt == statements[statements.length - 1];
        }
    }
    // closure result
    if (parent instanceof GrClosableBlock) {
        GrStatement[] statements = ((GrClosableBlock) parent).getStatements();
        return statements.length > 0 && stmt == statements[statements.length - 1];
    }
    // todo test me!
    if (stmt instanceof GrReturnStatement) {
        GrMethod method = PsiTreeUtil.getParentOfType(stmt, GrMethod.class);
        if (method != null) {
            Collection<GrStatement> returnStatements = ControlFlowUtils.collectReturns(method.getBlock());
            return returnStatements.contains(stmt) && !hasBadReturns(method);
        }
    }
    return false;
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) 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)

Example 9 with GrReturnStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement 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 10 with GrReturnStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement 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)

Aggregations

GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)27 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)16 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)12 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)9 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)8 PsiElement (com.intellij.psi.PsiElement)7 Nullable (org.jetbrains.annotations.Nullable)5 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)5 PsiType (com.intellij.psi.PsiType)4 NotNull (org.jetbrains.annotations.NotNull)4 GrControlFlowOwner (org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)3 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)3 GrThrowStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement)3 GrVariableDeclarationOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner)3 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiElementPredicate (org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate)2