Search in sources :

Example 1 with ReturnStatementTree

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

the class SymbolTableTest method Lambdas.

@Test
public void Lambdas() throws Exception {
    Result result = Result.createFor("Lambdas");
    JavaSymbol barMethod = result.symbol("bar");
    assertThat(barMethod.usages()).hasSize(1);
    MethodTree methodTree = (MethodTree) barMethod.declaration();
    Type Ftype = result.symbol("F").type();
    assertThat(((ReturnStatementTree) methodTree.block().body().get(0)).expression().symbolType()).isSameAs(Ftype);
    JavaSymbol qixMethod = result.symbol("qix");
    LambdaExpressionTree lamdba = ((LambdaExpressionTree) ((ReturnStatementTree) ((MethodTree) qixMethod.declaration()).block().body().get(0)).expression());
    assertThat(((ReturnStatementTree) ((BlockTree) lamdba.body()).body().get(0)).expression().symbolType()).isSameAs(result.symbol("F2").type());
    JavaSymbol fieldSymbol = result.symbol("field");
    assertThat(((VariableTree) fieldSymbol.declaration()).initializer().symbolType()).isSameAs(fieldSymbol.type());
    assertThat(((AssignmentExpressionTree) fieldSymbol.usages().get(0).parent()).expression().symbolType()).isSameAs(fieldSymbol.type());
    JavaSymbol bSymbol = result.symbol("b");
    assertThat(((NewClassTree) ((VariableTree) bSymbol.declaration()).initializer()).arguments().get(0).symbolType()).isSameAs(Ftype);
    JavaSymbol condMethod = result.symbol("cond");
    ConditionalExpressionTree conditionalExpression = (ConditionalExpressionTree) ((ReturnStatementTree) ((MethodTree) condMethod.declaration()).block().body().get(0)).expression();
    assertThat(conditionalExpression.symbolType()).isSameAs(Ftype);
    JavaSymbol parenthMethod = result.symbol("parenth");
    ParenthesizedTree parenthesizedTree = (ParenthesizedTree) ((ReturnStatementTree) ((MethodTree) parenthMethod.declaration()).block().body().get(0)).expression();
    assertThat(parenthesizedTree.symbolType()).isSameAs(Ftype);
    assertThat(result.symbol("s", 33).type().is("java.lang.String")).isTrue();
    JavaSymbol sym = result.symbol("o");
    assertThat(sym.type.is("java.lang.Object")).isTrue();
    assertThat(result.reference(8, 16)).isEqualTo(result.symbol("v", 8));
    assertThat(result.reference(9, 16)).isEqualTo(result.symbol("v", 9));
    JavaSymbol operations = result.symbol("operations");
    MethodInvocationTree mit = (MethodInvocationTree) operations.usages().get(0).parent().parent();
    assertThat(((ParametrizedTypeJavaType) operations.type).typeSubstitution.substitutedTypes().get(0)).isSameAs(mit.arguments().get(0).symbolType());
    JavaSymbol myStringParam = result.symbol("myStringParam");
    Symbol.MethodSymbol stringParamMethod = (Symbol.MethodSymbol) result.symbol("stringParamMethod");
    assertThat(stringParamMethod.usages()).hasSize(1);
    assertThat(myStringParam.type.is("java.lang.String")).isTrue();
    assertThat(result.symbol("s1").type.is("java.lang.String")).as(result.symbol("s1").type.name()).isTrue();
    assertThat(result.symbol("s2").type.is("java.lang.String")).isTrue();
    assertThat(result.symbol("foo", 95).usages()).hasSize(1);
    assertThat(result.symbol("x", 103).type.is("java.lang.Integer")).as(result.symbol("x", 103).type.name()).isTrue();
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ParenthesizedTree(org.sonar.plugins.java.api.tree.ParenthesizedTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) Type(org.sonar.plugins.java.api.semantic.Type) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) Test(org.junit.Test)

Example 2 with ReturnStatementTree

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

the class UselessIncrementCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    if (tree.is(Tree.Kind.RETURN_STATEMENT)) {
        ExpressionTree returnExpression = ((ReturnStatementTree) tree).expression();
        if (returnExpression != null && isPostfix(returnExpression)) {
            UnaryExpressionTree unaryExpression = (UnaryExpressionTree) returnExpression;
            ExpressionTree expression = ExpressionUtils.skipParentheses(unaryExpression.expression());
            if (expression.is(Tree.Kind.IDENTIFIER) && ((IdentifierTree) expression).symbol().owner().isMethodSymbol()) {
                reportIssue(unaryExpression);
            }
        }
    } else {
        AssignmentExpressionTree aet = (AssignmentExpressionTree) tree;
        if (isPostfix(aet.expression())) {
            UnaryExpressionTree postfix = (UnaryExpressionTree) aet.expression();
            if (SyntacticEquivalence.areEquivalent(aet.variable(), postfix.expression())) {
                reportIssue(postfix);
            }
        }
    }
}
Also used : UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Example 3 with ReturnStatementTree

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

the class JavaTreeModelTest method return_statement.

/**
 * 14.17. The return Statement
 */
@Test
public void return_statement() {
    ReturnStatementTree tree = (ReturnStatementTree) firstMethodFirstStatement("class T { boolean m() { return ; } }");
    assertThat(tree.is(Tree.Kind.RETURN_STATEMENT)).isTrue();
    assertThat(tree.returnKeyword().text()).isEqualTo("return");
    assertThat(tree.expression()).isNull();
    assertThat(tree.semicolonToken().text()).isEqualTo(";");
    assertThatChildrenIteratorHasSize(tree, 2);
    tree = (ReturnStatementTree) firstMethodFirstStatement("class T { boolean m() { return true; } }");
    assertThat(tree.is(Tree.Kind.RETURN_STATEMENT)).isTrue();
    assertThat(tree.returnKeyword().text()).isEqualTo("return");
    assertThat(tree.expression()).isNotNull();
    assertThat(tree.semicolonToken().text()).isEqualTo(";");
    assertThatChildrenIteratorHasSize(tree, 3);
}
Also used : ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) Test(org.junit.Test)

Example 4 with ReturnStatementTree

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

the class UCFGJavaVisitor method buildBasicBlock.

private UCFGBuilder.BlockBuilder buildBasicBlock(CFG.Block javaBlock, MethodTree methodTree, IdentifierGenerator idGenerator) {
    UCFGBuilder.BlockBuilder blockBuilder = UCFGBuilder.newBasicBlock(String.valueOf(javaBlock.id()), location(javaBlock));
    javaBlock.elements().forEach(e -> buildCall(e, blockBuilder, idGenerator));
    Tree terminator = javaBlock.terminator();
    if (terminator != null && terminator.is(Tree.Kind.RETURN_STATEMENT)) {
        ExpressionTree returnedExpression = ((ReturnStatementTree) terminator).expression();
        Expression retExpr = constant(IdentifierGenerator.CONST);
        if (methodTree.returnType() != null && isString(methodTree.returnType().symbolType())) {
            retExpr = idGenerator.lookupExpressionFor(returnedExpression);
        }
        blockBuilder.ret(retExpr, location(terminator));
        return blockBuilder;
    }
    Set<CFG.Block> successors = javaBlock.successors();
    if (!successors.isEmpty()) {
        blockBuilder.jumpTo(successors.stream().map(b -> UCFGBuilder.createLabel(Integer.toString(b.id()))).toArray(Label[]::new));
        return blockBuilder;
    }
    Preconditions.checkState(javaBlock.id() == 0);
    blockBuilder.ret(constant("implicit return"), location(methodTree.lastToken()));
    return blockBuilder;
}
Also used : UCFGBuilder(org.sonar.ucfg.UCFGBuilder) Expression(org.sonar.ucfg.Expression) Label(org.sonar.ucfg.Label) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) 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) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) 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) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) BlockBuilder(org.sonar.ucfg.UCFGBuilder.BlockBuilder)

Example 5 with ReturnStatementTree

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

the class ConstantMethodCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodTree methodTree = (MethodTree) tree;
    BlockTree body = methodTree.block();
    if (!methodTree.modifiers().annotations().isEmpty() || ModifiersUtils.hasModifier(methodTree.modifiers(), Modifier.DEFAULT)) {
        return;
    }
    if (Boolean.FALSE.equals(methodTree.isOverriding()) && body != null && body.body().size() == 1) {
        StatementTree uniqueStatement = body.body().get(0);
        if (uniqueStatement.is(Kind.RETURN_STATEMENT)) {
            ExpressionTree returnedExpression = ((ReturnStatementTree) uniqueStatement).expression();
            if (isConstant(returnedExpression)) {
                reportIssue(returnedExpression, "Remove this method and declare a constant for this value.");
            }
        }
    }
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree)

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