Search in sources :

Example 36 with StatementTree

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

the class ReassignmentFinderTest method parameter_with_usage.

@Test
public void parameter_with_usage() throws Exception {
    String code = newCode("int foo(boolean test) {", "  if (test) {}", "  return test;", "}");
    MethodTree method = methodTree(code);
    List<StatementTree> statements = method.block().body();
    assertThatLastReassignmentsOfReturnedVariableIsEqualTo(statements, null);
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 37 with StatementTree

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

the class NonNullSetToNullCheck method callsThisConstructor.

private static boolean callsThisConstructor(MethodTree constructor) {
    List<StatementTree> body = constructor.block().body();
    if (body.isEmpty()) {
        return false;
    }
    StatementTree firstStatement = body.get(0);
    if (!firstStatement.is(Tree.Kind.EXPRESSION_STATEMENT)) {
        return false;
    }
    ExpressionTree expression = ((ExpressionStatementTree) firstStatement).expression();
    if (!expression.is(Tree.Kind.METHOD_INVOCATION)) {
        return false;
    }
    ExpressionTree methodSelect = ((MethodInvocationTree) expression).methodSelect();
    return methodSelect.is(Tree.Kind.IDENTIFIER) && "this".equals(((IdentifierTree) methodSelect).name());
}
Also used : ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 38 with StatementTree

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

the class MethodIdenticalImplementationsCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    List<MethodWithUsedVariables> methods = classTree.members().stream().filter(member -> member.is(Tree.Kind.METHOD)).map(MethodTree.class::cast).filter(methodTree -> isDuplicateCandidate(methodTree, classTree)).map(MethodWithUsedVariables::new).collect(Collectors.toList());
    if (methods.size() <= 1) {
        return;
    }
    Set<MethodTree> reported = new HashSet<>();
    for (int i = 0; i < methods.size(); i++) {
        MethodWithUsedVariables methodWithVariables = methods.get(i);
        MethodTree method = methodWithVariables.method;
        SyntaxToken methodIdentifier = method.simpleName().identifierToken();
        List<StatementTree> methodBody = method.block().body();
        methods.stream().skip(i + 1L).filter(otherMethodWithVariables -> !reported.contains(otherMethodWithVariables.method)).filter(otherMethodWithVariables -> !methodIdentifier.text().equals(otherMethodWithVariables.method.simpleName().name())).filter(otherMethodWithVariables -> SyntacticEquivalence.areEquivalent(methodBody, otherMethodWithVariables.method.block().body())).filter(methodWithVariables::isUsingSameVariablesWithSameTypes).forEach(otherMethodWithVariables -> {
            MethodTree otherMethod = otherMethodWithVariables.method;
            reportIssue(otherMethod.simpleName(), String.format(ISSUE_MSG, methodIdentifier.text(), methodIdentifier.line()), Collections.singletonList(new JavaFileScannerContext.Location("original implementation", methodIdentifier)), null);
            reported.add(otherMethod);
        });
    }
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) AccessorsUtils(org.sonar.java.ast.visitors.AccessorsUtils) BaseTreeVisitor(org.sonar.plugins.java.api.tree.BaseTreeVisitor) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) Set(java.util.Set) HashMap(java.util.HashMap) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) Type(org.sonar.plugins.java.api.semantic.Type) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) List(java.util.List) SyntacticEquivalence(org.sonar.java.model.SyntacticEquivalence) Map(java.util.Map) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Collections(java.util.Collections) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) HashSet(java.util.HashSet)

Example 39 with StatementTree

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

the class MissingCurlyBracesCheck method checkIfStatement.

private void checkIfStatement(IfStatementTree ifStmt) {
    checkStatement(ifStmt.ifKeyword(), ifStmt.thenStatement());
    StatementTree elseStmt = ifStmt.elseStatement();
    if (elseStmt != null && !elseStmt.is(Tree.Kind.IF_STATEMENT)) {
        checkStatement(ifStmt.elseKeyword(), elseStmt);
    }
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree)

Example 40 with StatementTree

use of org.sonar.plugins.java.api.tree.StatementTree 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)

Aggregations

StatementTree (org.sonar.plugins.java.api.tree.StatementTree)53 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)27 Test (org.junit.Test)26 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)26 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)24 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)21 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)17 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)15 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)13 ForStatementTree (org.sonar.plugins.java.api.tree.ForStatementTree)9 WhileStatementTree (org.sonar.plugins.java.api.tree.WhileStatementTree)8 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)7 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)7 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)6 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)6 SwitchStatementTree (org.sonar.plugins.java.api.tree.SwitchStatementTree)6 ThrowStatementTree (org.sonar.plugins.java.api.tree.ThrowStatementTree)6 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)5 Tree (org.sonar.plugins.java.api.tree.Tree)5 TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)5