Search in sources :

Example 51 with ClassTree

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

the class TooManyMethodsCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    List<Tree> methods = classTree.members().stream().filter(member -> member.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR) && (countNonPublic || ((MethodTree) member).symbol().isPublic())).collect(Collectors.toList());
    if (shouldNotReportIssue(classTree, methods)) {
        return;
    }
    List<JavaFileScannerContext.Location> secondary = methods.stream().map(element -> new JavaFileScannerContext.Location("Method + 1", element)).collect(Collectors.toList());
    String classDescription;
    if (classTree.simpleName() == null) {
        classDescription = "Anonymous class \"" + ((NewClassTree) classTree.parent()).identifier().symbolType().name() + "\"";
    } else {
        classDescription = classTree.declarationKeyword().text() + " \"" + classTree.simpleName() + "\"";
    }
    reportIssue(ExpressionsHelper.reportOnClassTree(classTree), String.format("%s has %d%s methods, which is greater than the %d authorized. Split it into smaller classes.", classDescription, methods.size(), countNonPublic ? "" : " public", maximumMethodThreshold), secondary, null);
}
Also used : RuleProperty(org.sonar.check.RuleProperty) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ExpressionsHelper(org.sonar.java.checks.helpers.ExpressionsHelper) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 52 with ClassTree

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

the class SpringConstructorInjectionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    if (isClassTreeAnnotatedWith(classTree, "org.springframework.stereotype.Controller", "org.springframework.stereotype.Repository", "org.springframework.stereotype.Service")) {
        List<Tree> toReport = classTree.members().stream().filter(SpringConstructorInjectionCheck::isMemberAutowired).map(SpringConstructorInjectionCheck::toReportTree).collect(Collectors.toList());
        if (!toReport.isEmpty()) {
            int cost = toReport.size();
            // find constructor
            classTree.members().stream().filter(m -> m.is(Kind.CONSTRUCTOR)).map(m -> ((MethodTree) m).simpleName()).findFirst().ifPresent(toReport::add);
            reportIssue(toReport.get(0), "Remove this annotation and use constructor injection instead.", toReport.stream().skip(1).map(i -> new JavaFileScannerContext.Location("", i)).collect(Collectors.toList()), cost);
        }
    }
}
Also used : Arrays(java.util.Arrays) Kind(org.sonar.plugins.java.api.tree.Tree.Kind) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) Stream(java.util.stream.Stream) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) Collections(java.util.Collections) Symbol(org.sonar.plugins.java.api.semantic.Symbol) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 53 with ClassTree

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

the class JavaParserTest method receiver_type_should_be_parsed.

@Test
public void receiver_type_should_be_parsed() throws Exception {
    try {
        String code = "class Main { class Inner { Inner(Main Main.this) {}};}";
        CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse(code);
        Tree constructor = ((ClassTree) ((ClassTree) cut.types().get(0)).members().get(0)).members().get(0);
        assertThat(constructor).isInstanceOf(MethodTree.class);
        assertThat(((MethodTree) constructor).parameters().get(0).simpleName().name()).isEqualTo("this");
    } catch (Exception ex) {
        fail("Receiver type of inner classes should be parsed correctly", ex);
    }
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 54 with ClassTree

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

the class SimpleClassNameCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    CompilationUnitTree cut = (CompilationUnitTree) tree;
    cut.types().stream().filter(NOT_EMPTY_STATEMENT).map(t -> ((ClassTree) t).symbol()).forEach(this::checkSymbol);
    List<ImportTree> imports = cut.imports().stream().filter(NOT_EMPTY_STATEMENT).map(t -> (ImportTree) t).collect(Collectors.toList());
    boolean fileContainsStarImport = imports.stream().filter(it -> it.qualifiedIdentifier().is(Kind.MEMBER_SELECT)).map(it -> ((MemberSelectExpressionTree) it.qualifiedIdentifier()).identifier()).anyMatch(i -> "*".equals(i.name()));
    if (!fileContainsStarImport) {
        checkImports(imports);
    }
}
Also used : Kind(org.sonar.plugins.java.api.tree.Tree.Kind) ImportTree(org.sonar.plugins.java.api.tree.ImportTree) Predicate(java.util.function.Predicate) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SemanticModel(org.sonar.java.resolve.SemanticModel) 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) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ImportTree(org.sonar.plugins.java.api.tree.ImportTree)

Example 55 with ClassTree

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

the class StandardFunctionalInterfaceCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    // classTree.simpleName() never null for Tree.Kind.INTERFACE
    IdentifierTree issueLocation = classTree.simpleName();
    // The question "Why we raise issue only for interface annotated with @FunctionalInterface?"
    // is discussed in comments of https://jira.sonarsource.com/browse/SONARJAVA-504
    Optional.of(classTree).filter(StandardFunctionalInterfaceCheck::isFunctionalInterface).filter(StandardFunctionalInterfaceCheck::isNonStandardFunctionalInterface).filter(StandardFunctionalInterfaceCheck::hasNoExtension).flatMap(StandardFunctionalInterfaceCheck::lookupFunctionalMethod).flatMap(StandardFunctionalInterfaceCheck::lookupMatchingStandardInterface).ifPresent(standardInterface -> reportIssue(issueLocation, buildIssueMessage(classTree, standardInterface)));
}
Also used : ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

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