Search in sources :

Example 56 with MethodTree

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

the class MethodTooBigCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodTree methodTree = (MethodTree) tree;
    BlockTree block = methodTree.block();
    if (block != null) {
        int lines = new LinesOfCodeVisitor().linesOfCode(block);
        if (lines > max) {
            reportIssue(methodTree.simpleName(), "This method has " + lines + " lines, which is greater than the " + max + " lines authorized. Split it into smaller methods.");
        }
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) LinesOfCodeVisitor(org.sonar.java.ast.visitors.LinesOfCodeVisitor)

Example 57 with MethodTree

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

the class ObjectFinalizeCheck method isFinalizeMethodMember.

private static boolean isFinalizeMethodMember(MethodTree methodTree) {
    Tree returnType = methodTree.returnType();
    boolean returnVoid = returnType != null && returnType.is(Tree.Kind.PRIMITIVE_TYPE) && "void".equals(((PrimitiveTypeTree) returnType).keyword().text());
    return returnVoid && "finalize".equals(methodTree.simpleName().name());
}
Also used : PrimitiveTypeTree(org.sonar.plugins.java.api.tree.PrimitiveTypeTree) Tree(org.sonar.plugins.java.api.tree.Tree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 58 with MethodTree

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

the class LiveVariablesTest method buildCFG.

private static CFG buildCFG(String methodCode) {
    CompilationUnitTree cut = (CompilationUnitTree) PARSER.parse("class A { int field1; int field2; static int staticField; " + methodCode + " }");
    SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
    MethodTree tree = ((MethodTree) ((ClassTree) cut.types().get(0)).members().get(3));
    return CFG.build(tree);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 59 with MethodTree

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

the class ComplexityVisitorTest method switch_handling.

@Test
public void switch_handling() throws Exception {
    CompilationUnitTree cut = (CompilationUnitTree) p.parse("class A {" + "  String foo(int a) {" + "    switch (a) {" + "      case 0:" + "        return \"none\";" + "      case 1:" + "        return \"one\";" + "      case 2:" + "        return \"many\";" + "      default:" + "        return \"it's complicated\";" + "    }" + "  }" + "}");
    MethodTree methodTree = (MethodTree) ((ClassTree) cut.types().get(0)).members().get(0);
    List<Tree> nodes = new ComplexityVisitor().getNodes(methodTree);
    // default case does not count.
    assertThat(nodes).hasSize(4);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 60 with MethodTree

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

the class PublicApiCheckerTest method getPublicApiVisitor.

private SubscriptionVisitor getPublicApiVisitor() {
    return new SubscriptionVisitor() {

        private final Deque<ClassTree> classTrees = Lists.newLinkedList();

        private final Deque<MethodTree> methodTrees = Lists.newLinkedList();

        @Override
        public List<Tree.Kind> nodesToVisit() {
            return Arrays.asList(Tree.Kind.values());
        }

        @Override
        public void visitNode(Tree tree) {
            if (tree.is(Tree.Kind.VARIABLE)) {
                VariableTree variableTree = (VariableTree) tree;
                String name = variableTree.simpleName().name();
                Tree parent = classTrees.peek();
                if (!methodTrees.isEmpty()) {
                    parent = methodTrees.peek();
                }
                assertThat(PublicApiChecker.isPublicApi(parent, tree)).as(name).isEqualTo(name.endsWith("Public"));
            } else if (tree.is(PublicApiChecker.methodKinds())) {
                MethodTree methodTree = (MethodTree) tree;
                methodTrees.push(methodTree);
                String name = methodTree.simpleName().name();
                // getters and setters are included in the public API
                assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).as(name).isEqualTo(name.endsWith("Public") || name.contains("GetSet"));
            } else if (tree.is(PublicApiChecker.classKinds())) {
                IdentifierTree className = ((ClassTree) tree).simpleName();
                if (className == null) {
                    assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).isFalse();
                } else {
                    assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).as(className.name()).isEqualTo(className != null && className.name().endsWith("Public"));
                }
                classTrees.push((ClassTree) tree);
            } else {
                assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).isFalse();
            }
        }

        @Override
        public void leaveNode(Tree tree) {
            if (tree.is(PublicApiChecker.classKinds())) {
                classTrees.pop();
            } else if (tree.is(PublicApiChecker.methodKinds())) {
                methodTrees.pop();
            }
        }
    };
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Deque(java.util.Deque)

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