Search in sources :

Example 66 with MethodTree

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

the class CompareToResultTestCheck method isIdentifierContainingCompareToResult.

private static boolean isIdentifierContainingCompareToResult(IdentifierTree identifier) {
    Symbol variableSymbol = identifier.symbol();
    if (!variableSymbol.isVariableSymbol()) {
        return false;
    }
    VariableTree variableDefinition = ((Symbol.VariableSymbol) variableSymbol).declaration();
    if (variableDefinition != null) {
        ExpressionTree initializer = variableDefinition.initializer();
        if (initializer != null && initializer.is(Tree.Kind.METHOD_INVOCATION) && variableSymbol.owner().isMethodSymbol()) {
            MethodTree method = ((Symbol.MethodSymbol) variableSymbol.owner()).declaration();
            return method != null && COMPARE_TO.matches((MethodInvocationTree) initializer) && !isReassigned(variableSymbol, method);
        }
    }
    return false;
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 67 with MethodTree

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

Example 68 with MethodTree

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

the class CognitiveComplexityMethodCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodTree method = (MethodTree) tree;
    CognitiveComplexityVisitor.Result result = CognitiveComplexityVisitor.methodComplexity(method);
    int total = result.complexity;
    if (total > max) {
        reportIssue(method.simpleName(), "Refactor this method to reduce its Cognitive Complexity from " + total + " to the " + max + " allowed.", result.locations, total - max);
    }
}
Also used : CognitiveComplexityVisitor(org.sonar.java.ast.visitors.CognitiveComplexityVisitor) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 69 with MethodTree

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

the class CallSuperInTestCaseCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodTree methodTree = (MethodTree) tree;
    Symbol.MethodSymbol methodSymbol = methodTree.symbol();
    boolean isMethodInJunit3 = isWithinJunit3TestCase(methodSymbol) && isSetUpOrTearDown(methodSymbol);
    if (isMethodInJunit3 && requiresSuperCall(methodSymbol) && !callSuperOnOverride(methodTree.block(), methodSymbol)) {
        reportIssue(methodTree.simpleName(), String.format("Add a \"super.%s()\" call to this method.", methodSymbol.name()));
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 70 with MethodTree

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

the class SyntaxTreeNameFinderTest method testCatchParameter.

@Test
public void testCatchParameter() {
    MethodTree tree = buildSyntaxTree("public void test() {try {} catch (Exception ex) {} }");
    assertThat(SyntaxTreeNameFinder.getName(tree)).isEqualTo("ex");
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Aggregations

MethodTree (org.sonar.plugins.java.api.tree.MethodTree)143 Test (org.junit.Test)59 Tree (org.sonar.plugins.java.api.tree.Tree)43 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)39 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)34 Symbol (org.sonar.plugins.java.api.semantic.Symbol)31 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)30 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)27 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)23 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)20 Type (org.sonar.plugins.java.api.semantic.Type)19 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)19 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)18 List (java.util.List)16 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)16 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)15 ArrayList (java.util.ArrayList)14 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)14 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)14 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)12