Search in sources :

Example 36 with ClassTree

use of org.sonar.plugins.java.api.tree.ClassTree 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 37 with ClassTree

use of org.sonar.plugins.java.api.tree.ClassTree 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 38 with ClassTree

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

the class NoTestInTestClassCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        resetAnnotationCache();
        CompilationUnitTree cut = (CompilationUnitTree) tree;
        cut.types().stream().filter(typeTree -> typeTree.is(Kind.CLASS)).forEach(typeTree -> checkClass((ClassTree) typeTree));
    }
}
Also used : Modifier(org.sonar.plugins.java.api.tree.Modifier) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Kind(org.sonar.plugins.java.api.tree.Tree.Kind) Set(java.util.Set) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) ModifiersUtils(org.sonar.java.model.ModifiersUtils) Type(org.sonar.plugins.java.api.semantic.Type) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) HashSet(java.util.HashSet) ExpressionsHelper(org.sonar.java.checks.helpers.ExpressionsHelper) List(java.util.List) Stream(java.util.stream.Stream) ImmutableList(com.google.common.collect.ImmutableList) SymbolMetadata(org.sonar.plugins.java.api.semantic.SymbolMetadata) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 39 with ClassTree

use of org.sonar.plugins.java.api.tree.ClassTree 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 40 with ClassTree

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

ClassTree (org.sonar.plugins.java.api.tree.ClassTree)116 Tree (org.sonar.plugins.java.api.tree.Tree)53 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)47 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)45 Test (org.junit.Test)41 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)37 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)32 Symbol (org.sonar.plugins.java.api.semantic.Symbol)31 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)23 List (java.util.List)19 Type (org.sonar.plugins.java.api.semantic.Type)18 File (java.io.File)14 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)13 Rule (org.sonar.check.Rule)12 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)12 Collectors (java.util.stream.Collectors)10 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)10 IssuableSubscriptionVisitor (org.sonar.plugins.java.api.IssuableSubscriptionVisitor)10 ImmutableList (com.google.common.collect.ImmutableList)9 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)9