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());
}
}
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);
}
}
}
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()));
}
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.");
}
}
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();
}
Aggregations