Search in sources :

Example 6 with ReturnStatementTree

use of org.sonar.plugins.java.api.tree.ReturnStatementTree in project sonar-java by SonarSource.

the class ExplodedGraphWalker method handleBlockExit.

private void handleBlockExit(ProgramPoint programPosition) {
    CFG.Block block = (CFG.Block) programPosition.block;
    Tree terminator = block.terminator();
    cleanUpProgramState(block);
    boolean exitPath = node.exitPath;
    if (terminator != null) {
        switch(terminator.kind()) {
            case IF_STATEMENT:
                ExpressionTree ifCondition = ((IfStatementTree) terminator).condition();
                handleBranch(block, cleanupCondition(ifCondition), verifyCondition(ifCondition));
                return;
            case CONDITIONAL_OR:
            case CONDITIONAL_AND:
                handleBranch(block, ((BinaryExpressionTree) terminator).leftOperand());
                return;
            case CONDITIONAL_EXPRESSION:
                handleBranch(block, ((ConditionalExpressionTree) terminator).condition());
                return;
            case FOR_STATEMENT:
                ExpressionTree condition = ((ForStatementTree) terminator).condition();
                if (condition != null) {
                    handleBranch(block, condition, false);
                    return;
                }
                break;
            case WHILE_STATEMENT:
                ExpressionTree whileCondition = ((WhileStatementTree) terminator).condition();
                handleBranch(block, cleanupCondition(whileCondition), verifyCondition(whileCondition));
                return;
            case DO_STATEMENT:
                ExpressionTree doCondition = ((DoWhileStatementTree) terminator).condition();
                handleBranch(block, cleanupCondition(doCondition), verifyCondition(doCondition));
                return;
            case SYNCHRONIZED_STATEMENT:
                resetFieldValues(false);
                break;
            case RETURN_STATEMENT:
                ExpressionTree returnExpression = ((ReturnStatementTree) terminator).expression();
                if (returnExpression != null) {
                    programState.storeExitValue();
                }
                break;
            case THROW_STATEMENT:
                ProgramState.Pop unstack = programState.unstackValue(1);
                SymbolicValue sv = unstack.values.get(0);
                if (sv instanceof SymbolicValue.CaughtExceptionSymbolicValue) {
                    // retrowing the exception from a catch block
                    sv = ((SymbolicValue.CaughtExceptionSymbolicValue) sv).exception();
                } else {
                    sv = constraintManager.createExceptionalSymbolicValue(((ThrowStatementTree) terminator).expression().symbolType());
                }
                programState = unstack.state.stackValue(sv);
                programState.storeExitValue();
                break;
            default:
        }
    }
    // unconditional jumps, for-statement, switch-statement, synchronized:
    if (exitPath) {
        if (block.exitBlock() != null) {
            enqueue(new ProgramPoint(block.exitBlock()), programState, true);
        } else {
            for (CFG.Block successor : block.successors()) {
                enqueue(new ProgramPoint(successor), programState, true);
            }
        }
    } else {
        for (CFG.Block successor : block.successors()) {
            if (!block.isFinallyBlock() || isDirectFlowSuccessorOf(successor, block)) {
                enqueue(new ProgramPoint(successor), programState, successor == block.exitBlock());
            }
        }
    }
}
Also used : ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) CFG(org.sonar.java.cfg.CFG) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) JavaTree(org.sonar.java.model.JavaTree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 7 with ReturnStatementTree

use of org.sonar.plugins.java.api.tree.ReturnStatementTree in project sonar-java by SonarSource.

the class TypeAndReferenceSolver method visitReturnStatement.

@Override
public void visitReturnStatement(ReturnStatementTree tree) {
    super.visitReturnStatement(tree);
    ExpressionTree expression = tree.expression();
    if (expression != null && ((JavaType) expression.symbolType()).isTagged(JavaType.DEFERRED)) {
        // get owner of return (method or lambda)
        Tree parent = tree.parent();
        while (!parent.is(Tree.Kind.METHOD, Tree.Kind.LAMBDA_EXPRESSION)) {
            parent = parent.parent();
            if (parent == null) {
                throw new IllegalStateException("Return statement was unexpected here");
            }
        }
        Type infered;
        if (parent.is(Tree.Kind.METHOD)) {
            infered = ((MethodTree) parent).returnType().symbolType();
        } else {
            infered = ((LambdaExpressionTree) parent).symbolType();
        }
        setInferedType(infered, (DeferredType) expression.symbolType());
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) AbstractTypedTree(org.sonar.java.model.AbstractTypedTree) UnionTypeTree(org.sonar.plugins.java.api.tree.UnionTypeTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ContinueStatementTree(org.sonar.plugins.java.api.tree.ContinueStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) WildcardTree(org.sonar.plugins.java.api.tree.WildcardTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree) BreakStatementTree(org.sonar.plugins.java.api.tree.BreakStatementTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ParenthesizedTree(org.sonar.plugins.java.api.tree.ParenthesizedTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) EnumConstantTree(org.sonar.plugins.java.api.tree.EnumConstantTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ImportTree(org.sonar.plugins.java.api.tree.ImportTree) PrimitiveTypeTree(org.sonar.plugins.java.api.tree.PrimitiveTypeTree) TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) InstanceOfTree(org.sonar.plugins.java.api.tree.InstanceOfTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 8 with ReturnStatementTree

use of org.sonar.plugins.java.api.tree.ReturnStatementTree in project sonar-java by SonarSource.

the class MethodOnlyCallsSuperCheck method isUselessSuperCall.

private static boolean isUselessSuperCall(MethodTree methodTree) {
    ExpressionTree callToSuper = null;
    StatementTree statementTree = methodTree.block().body().get(0);
    if (returnsVoid(methodTree) && statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
        callToSuper = ((ExpressionStatementTree) statementTree).expression();
    } else if (statementTree.is(Tree.Kind.RETURN_STATEMENT)) {
        callToSuper = ((ReturnStatementTree) statementTree).expression();
    }
    return callToSuper != null && isCallToSuper(methodTree, callToSuper) && sameVisibility(methodTree.symbol(), ((MethodInvocationTree) callToSuper).symbol());
}
Also used : ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree)

Example 9 with ReturnStatementTree

use of org.sonar.plugins.java.api.tree.ReturnStatementTree in project sonar-java by SonarSource.

the class SymbolTableTest method conditional_operator_expression_type.

@Test
public void conditional_operator_expression_type() {
    Result res = Result.createFor("ConditionalOperator");
    ExpressionTree conditional = ((ReturnStatementTree) ((MethodTree) res.symbol("fun").declaration()).block().body().get(0)).expression();
    Type conditionalExpressionType = conditional.symbolType();
    assertThat(conditionalExpressionType.is("App$Foo")).isTrue();
    assertThat(((JavaType) conditionalExpressionType).isParameterized()).isTrue();
    List<JavaType> substitutedTypes = ((ParametrizedTypeJavaType) conditionalExpressionType).typeSubstitution.substitutedTypes();
    assertThat(substitutedTypes).hasSize(1);
    assertThat(substitutedTypes.get(0).isTagged(JavaType.WILDCARD)).isTrue();
    assertThat(((WildCardType) substitutedTypes.get(0)).bound.is("java.util.List")).isTrue();
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) Test(org.junit.Test)

Example 10 with ReturnStatementTree

use of org.sonar.plugins.java.api.tree.ReturnStatementTree in project sonar-java by SonarSource.

the class ExpressionUtilsTest method test_skip_parenthesis.

@Test
public void test_skip_parenthesis() throws Exception {
    File file = new File("src/test/java/org/sonar/java/model/ExpressionUtilsTest.java");
    CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
    MethodTree methodTree = (MethodTree) ((ClassTree) tree.types().get(0)).members().get(0);
    ExpressionTree parenthesis = ((ReturnStatementTree) methodTree.block().body().get(0)).expression();
    assertThat(parenthesis.is(Tree.Kind.PARENTHESIZED_EXPRESSION)).isTrue();
    ExpressionTree skipped = ExpressionUtils.skipParentheses(parenthesis);
    assertThat(skipped.is(Tree.Kind.CONDITIONAL_AND)).isTrue();
    assertThat(ExpressionUtils.skipParentheses(((BinaryExpressionTree) skipped).leftOperand()).is(Tree.Kind.IDENTIFIER)).isTrue();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) File(java.io.File) Test(org.junit.Test)

Aggregations

ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)12 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)9 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)7 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)6 Test (org.junit.Test)5 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)5 Type (org.sonar.plugins.java.api.semantic.Type)4 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)4 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)4 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)4 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)4 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)3 LiteralTree (org.sonar.plugins.java.api.tree.LiteralTree)3 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)3 Tree (org.sonar.plugins.java.api.tree.Tree)3 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)3 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)2 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)2 ArrayDimensionTree (org.sonar.plugins.java.api.tree.ArrayDimensionTree)2 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)2