use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class RedundantThrowsDeclarationCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
ListTree<TypeTree> thrownList = methodTree.throwsClauses();
Set<Type> thrownExceptions = thrownExceptionsFromBody(methodTree);
boolean hasTryWithResourceInBody = hasTryWithResourceInBody(methodTree);
Set<String> reported = new HashSet<>();
for (TypeTree typeTree : thrownList) {
Type exceptionType = typeTree.symbolType();
if (hasTryWithResourceInBody && (exceptionType.is("java.io.IOException") || exceptionType.is("java.lang.Exception"))) {
// method 'close()' from 'java.io.Closeable' interface throws 'java.io.IOException"
continue;
}
String fullyQualifiedName = exceptionType.fullyQualifiedName();
if (!reported.contains(fullyQualifiedName)) {
String superTypeName = isSubclassOfAny(exceptionType, thrownList);
if (superTypeName != null) {
reportIssue(typeTree, String.format("Remove the declaration of thrown exception '%s' which is a subclass of '%s'.", fullyQualifiedName, superTypeName));
} else if (exceptionType.isSubtypeOf("java.lang.RuntimeException")) {
reportIssue(typeTree, String.format("Remove the declaration of thrown exception '%s' which is a runtime exception.", fullyQualifiedName));
} else if (declaredMoreThanOnce(fullyQualifiedName, thrownList)) {
reportIssue(typeTree, String.format("Remove the redundant '%s' thrown exception declaration(s).", fullyQualifiedName));
} else if (canNotBeThrown(methodTree, exceptionType, thrownExceptions)) {
reportIssue(typeTree, String.format("Remove the declaration of thrown exception '%s', as it cannot be thrown from %s's body.", fullyQualifiedName, methodTreeType(methodTree)));
}
reported.add(fullyQualifiedName);
}
}
}
use of org.sonar.plugins.java.api.tree.MethodTree 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.MethodTree 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.MethodTree in project sonar-java by SonarSource.
the class ThreadRunCheck method onMethodInvocationFound.
@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
Tree parent = mit.parent();
while (parent != null && !parent.is(Tree.Kind.METHOD)) {
parent = parent.parent();
}
if (parent != null && THREAD_RUN_METHOD_MATCHER.matches((MethodTree) parent)) {
return;
}
reportIssue(ExpressionUtils.methodName(mit), "Call the method Thread.start() to execute the content of the run() method in a dedicated thread.");
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class SyncGetterAndSetterCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
MethodTree methodTree = (MethodTree) tree;
checkMethodTree(methodTree, GETTER, SETTER);
checkMethodTree(methodTree, GETTER_BOOLEAN, SETTER);
checkMethodTree(methodTree, SETTER, GETTER);
checkMethodTree(methodTree, SETTER, GETTER_BOOLEAN);
}
Aggregations