Search in sources :

Example 51 with MethodTree

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

the class OutputStreamOverrideWriteCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    Type superType = classTree.symbol().superClass();
    IdentifierTree className = classTree.simpleName();
    if (className == null || classTree.symbol().isAbstract() || superType == null || !(superType.is("java.io.OutputStream") || superType.is("java.io.FilterOutputStream"))) {
        return;
    }
    Optional<MethodTree> writeByteIntInt = findMethod(classTree, WRITE_BYTES_INT_INT);
    Optional<MethodTree> writeInt = findMethod(classTree, WRITE_INT);
    if (!writeByteIntInt.isPresent()) {
        String message = "Provide an override of \"write(byte[],int,int)\" for this class.";
        if (writeInt.isPresent()) {
            MethodTree writeIntTree = writeInt.get();
            if (writeIntTree.block().body().isEmpty()) {
                message = "Provide an empty override of \"write(byte[],int,int)\" for this class as well.";
            }
        }
        reportIssue(className, message);
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 52 with MethodTree

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

the class MethodWithExcessiveReturnsCheck method leaveNode.

@Override
public void leaveNode(Tree tree) {
    Tree reportTree = null;
    if (tree.is(Tree.Kind.METHOD)) {
        reportTree = ((MethodTree) tree).simpleName();
    } else if (tree.is(Tree.Kind.LAMBDA_EXPRESSION)) {
        reportTree = ((LambdaExpressionTree) tree).arrowToken();
    }
    if (reportTree != null) {
        int count = returnStatementCounter.count(tree);
        if (count > max) {
            reportIssue(reportTree, "Reduce the number of returns of this method " + count + ", down to the maximum allowed " + max + ".");
        }
        methods.pop();
    }
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 53 with MethodTree

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

the class LeastSpecificTypeCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree methodTree = (MethodTree) tree;
    Symbol.MethodSymbol methodSymbol = methodTree.symbol();
    if (!methodSymbol.isPublic() || !Boolean.FALSE.equals(methodTree.isOverriding()) || isOverloaded(methodSymbol)) {
        return;
    }
    boolean springInjectionAnnotated = isSpringInjectionAnnotated(methodSymbol.metadata());
    methodTree.parameters().stream().map(VariableTree::symbol).filter(p -> p.type().isClass() && !p.type().symbol().isEnum() && !p.type().is("java.lang.String")).filter(p -> !(springInjectionAnnotated && p.type().is("java.util.Collection"))).forEach(p -> handleParameter(p, springInjectionAnnotated));
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) Iterator(java.util.Iterator) Set(java.util.Set) Tree(org.sonar.plugins.java.api.tree.Tree) Type(org.sonar.plugins.java.api.semantic.Type) Collectors(java.util.stream.Collectors) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) BooleanUtils(org.apache.commons.lang.BooleanUtils) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) JavaType(org.sonar.java.resolve.JavaType) Lists(com.google.common.collect.Lists) ClassJavaType(org.sonar.java.resolve.ClassJavaType) SymbolMetadata(org.sonar.plugins.java.api.semantic.SymbolMetadata) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Optional(java.util.Optional) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) Comparator(java.util.Comparator) 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) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 54 with MethodTree

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

the class MainInServletCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree node = (ClassTree) tree;
    Symbol.TypeSymbol symbol = node.symbol();
    if (isServletOrEjb(symbol)) {
        for (Tree member : node.members()) {
            if (member.is(Tree.Kind.METHOD) && ((MethodTreeImpl) member).isMainMethod()) {
                reportIssue(((MethodTree) member).simpleName(), "Remove this unwanted \"main\" method.");
            }
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 55 with MethodTree

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

the class MethodComplexityCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodTree methodTree = (MethodTree) tree;
    if (isExcluded(methodTree)) {
        return;
    }
    List<Tree> complexity = context.getComplexityNodes(methodTree);
    int size = complexity.size();
    if (size > max) {
        List<JavaFileScannerContext.Location> flow = new ArrayList<>();
        for (Tree element : complexity) {
            flow.add(new JavaFileScannerContext.Location("+1", element));
        }
        reportIssue(methodTree.simpleName(), "The Cyclomatic Complexity of this method \"" + methodTree.simpleName().name() + "\" is " + size + " which is greater than " + max + " authorized.", flow, size - max);
    }
}
Also used : JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ArrayList(java.util.ArrayList) Tree(org.sonar.plugins.java.api.tree.Tree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

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