Search in sources :

Example 56 with ClassTree

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

the class SAMAnnotatedCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    if (hasOneAbstractMethod(classTree.symbol()) && !isAnnotated(classTree)) {
        IdentifierTree simpleName = classTree.simpleName();
        reportIssue(simpleName, "Annotate the \"" + simpleName.name() + "\" interface with the @FunctionalInterface annotation" + context.getJavaVersion().java8CompatibilityMessage());
    }
}
Also used : ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 57 with ClassTree

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

the class RedundantModifierCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree classTree = (ClassTree) tree;
    for (Tree member : classTree.members()) {
        if (member.is(Tree.Kind.METHOD)) {
            MethodTree methodTree = (MethodTree) member;
            ModifiersTree modifiers = methodTree.modifiers();
            if (isInterfaceOrAnnotation(tree)) {
                checkRedundantModifier(modifiers, Modifier.ABSTRACT);
                checkRedundantModifier(modifiers, Modifier.PUBLIC);
            } else if (ModifiersUtils.hasModifier(classTree.modifiers(), Modifier.FINAL)) {
                checkRedundantModifier(modifiers, Modifier.FINAL);
            }
        } else if (member.is(Tree.Kind.VARIABLE) && isInterfaceOrAnnotation(tree)) {
            VariableTree variableTree = (VariableTree) member;
            ModifiersTree modifiers = variableTree.modifiers();
            checkRedundantModifier(modifiers, Modifier.PUBLIC);
            checkRedundantModifier(modifiers, Modifier.STATIC);
            checkRedundantModifier(modifiers, Modifier.FINAL);
        } else if (member.is(Kind.CONSTRUCTOR) && tree.is(Kind.ENUM)) {
            checkRedundantModifier(((MethodTree) member).modifiers(), Modifier.PRIVATE);
        }
    }
}
Also used : ModifiersTree(org.sonar.plugins.java.api.tree.ModifiersTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ModifierKeywordTree(org.sonar.plugins.java.api.tree.ModifierKeywordTree) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ModifiersTree(org.sonar.plugins.java.api.tree.ModifiersTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 58 with ClassTree

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

the class ThreadLocalWithInitialCheck method onConstructorFound.

@Override
protected void onConstructorFound(NewClassTree newClassTree) {
    ClassTree classTree = newClassTree.classBody();
    if (classTree == null) {
        return;
    }
    List<Tree> members = classTree.members();
    if (members.size() != 1) {
        return;
    }
    members.stream().filter(tree -> tree.is(Tree.Kind.METHOD)).map(t -> ((MethodTree) t)).filter(t -> "initialValue".equals(t.simpleName().name())).filter(t -> t.parameters().isEmpty()).findFirst().ifPresent(t -> reportIssue(newClassTree.identifier(), "Replace this anonymous class with a call to \"ThreadLocal.withInitial\"." + context.getJavaVersion().java8CompatibilityMessage()));
}
Also used : JavaVersionAwareVisitor(org.sonar.java.JavaVersionAwareVisitor) JavaVersion(org.sonar.plugins.java.api.JavaVersion) List(java.util.List) AbstractMethodDetection(org.sonar.java.checks.methods.AbstractMethodDetection) MethodMatcher(org.sonar.java.matcher.MethodMatcher) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Rule(org.sonar.check.Rule) Tree(org.sonar.plugins.java.api.tree.Tree) Collections(java.util.Collections) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 59 with ClassTree

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

the class ThreadOverridesRunCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree classTree = (ClassTree) tree;
    Symbol.TypeSymbol classSymbol = classTree.symbol();
    if (classSymbol != null && isDirectSubtypeOfThread(classSymbol) && !overridesRunMethod(classSymbol) && !hasConstructorCallingSuperWithRunnable(classTree)) {
        Tree report = classTree.simpleName();
        Tree parent = classTree.parent();
        if (parent.is(Tree.Kind.NEW_CLASS)) {
            NewClassTree newClassTree = (NewClassTree) parent;
            if (hasRunnableArgument(newClassTree.arguments())) {
                // will call the super constructor setting a runnable which will be executed by the run() method
                return;
            }
            report = newClassTree.identifier();
        }
        reportIssue(report, "Don't extend \"Thread\", since the \"run\" method is not overridden.");
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 60 with ClassTree

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

the class JavaParserTest method parent_link_should_be_computed.

@Test
public void parent_link_should_be_computed() {
    CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse("class A { void foo() {} }");
    ClassTree classTree = (ClassTree) cut.types().get(0);
    MethodTree method = (MethodTree) classTree.members().get(0);
    assertThat(method.parent()).isSameAs(classTree);
    assertThat(classTree.parent()).isSameAs(cut);
    assertThat(cut.parent()).isNull();
}
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) Test(org.junit.Test)

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