Search in sources :

Example 71 with ClassTree

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

the class MethodTreeImplTest method override_without_annotation_should_be_detected.

@Test
public void override_without_annotation_should_be_detected() {
    CompilationUnitTree cut = createTree("interface T { int m(); } class A implements T { int m(){return 0;}}");
    ClassTree interfaze = (ClassTree) cut.types().get(0);
    MethodTreeImpl methodInterface = (MethodTreeImpl) interfaze.members().get(0);
    ClassTree clazz = (ClassTree) cut.types().get(1);
    MethodTreeImpl methodClazz = (MethodTreeImpl) clazz.members().get(0);
    assertThat(methodInterface.isOverriding()).isFalse();
    assertThat(methodClazz.isOverriding()).isTrue();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Example 72 with ClassTree

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

the class AnonymousClassShouldBeLambdaCheck method visitNewClass.

@Override
public void visitNewClass(NewClassTree tree) {
    super.visitNewClass(tree);
    ClassTree classBody = tree.classBody();
    if (classBody != null) {
        TypeTree identifier = tree.identifier();
        if (!useThisIdentifier(classBody) && !enumConstants.contains(identifier) && isSAM(classBody)) {
            context.reportIssue(this, identifier, "Make this anonymous inner class a lambda" + context.getJavaVersion().java8CompatibilityMessage());
        }
    }
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 73 with ClassTree

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

the class ChildClassShadowFieldCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    TypeTree superClass = ((ClassTree) tree).superClass();
    if (superClass != null) {
        Symbol.TypeSymbol superclassSymbol = superClass.symbolType().symbol();
        ((ClassTree) tree).members().stream().filter(m -> m.is(Kind.VARIABLE)).map(VariableTree.class::cast).map(VariableTree::simpleName).forEach(fieldSimpleName -> {
            if (!IGNORED_FIELDS.contains(fieldSimpleName.name())) {
                checkForIssue(superclassSymbol, fieldSimpleName);
            }
        });
    }
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ImmutableSet(com.google.common.collect.ImmutableSet) Kind(org.sonar.plugins.java.api.tree.Tree.Kind) Set(java.util.Set) Tree(org.sonar.plugins.java.api.tree.Tree) Type(org.sonar.plugins.java.api.semantic.Type) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) 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) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) CheckForNull(javax.annotation.CheckForNull) Symbol(org.sonar.plugins.java.api.semantic.Symbol) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 74 with ClassTree

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

the class ClassFieldCountCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    long fieldCount = classTree.members().stream().filter(member -> member.is(Tree.Kind.VARIABLE) && shouldBeCounted((VariableTree) member)).count();
    if (fieldCount > threshold) {
        String message = String.format("Refactor this class so it has no more than %d %sfields, rather than the %d it currently has.", threshold, countNonPublicFields ? "" : "public ", fieldCount);
        reportIssue(reportOnClassTree(classTree), message);
    }
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) RuleProperty(org.sonar.check.RuleProperty) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) ExpressionsHelper.reportOnClassTree(org.sonar.java.checks.helpers.ExpressionsHelper.reportOnClassTree) Rule(org.sonar.check.Rule) Tree(org.sonar.plugins.java.api.tree.Tree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ExpressionsHelper.reportOnClassTree(org.sonar.java.checks.helpers.ExpressionsHelper.reportOnClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 75 with ClassTree

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

the class ReassignmentFinderTest method classTree.

private ClassTree classTree(String classBody) {
    CompilationUnitTree compilationUnitTree = (CompilationUnitTree) p.parse(classBody);
    SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
    return (ClassTree) compilationUnitTree.types().get(0);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

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