use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class OutputStreamOverrideWriteCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
ClassTree classTree = (ClassTree) tree;
Type superType = classTree.symbol().superClass();
IdentifierTree className = classTree.simpleName();
if (className == null || classTree.symbol().isAbstract() || superType == null || !(superType.is("java.io.OutputStream") || superType.is("java.io.FilterOutputStream"))) {
return;
}
Optional<MethodTree> writeByteIntInt = findMethod(classTree, WRITE_BYTES_INT_INT);
Optional<MethodTree> writeInt = findMethod(classTree, WRITE_INT);
if (!writeByteIntInt.isPresent()) {
String message = "Provide an override of \"write(byte[],int,int)\" for this class.";
if (writeInt.isPresent()) {
MethodTree writeIntTree = writeInt.get();
if (writeIntTree.block().body().isEmpty()) {
message = "Provide an empty override of \"write(byte[],int,int)\" for this class as well.";
}
}
reportIssue(className, message);
}
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class MethodWithExcessiveReturnsCheck method leaveNode.
@Override
public void leaveNode(Tree tree) {
Tree reportTree = null;
if (tree.is(Tree.Kind.METHOD)) {
reportTree = ((MethodTree) tree).simpleName();
} else if (tree.is(Tree.Kind.LAMBDA_EXPRESSION)) {
reportTree = ((LambdaExpressionTree) tree).arrowToken();
}
if (reportTree != null) {
int count = returnStatementCounter.count(tree);
if (count > max) {
reportIssue(reportTree, "Reduce the number of returns of this method " + count + ", down to the maximum allowed " + max + ".");
}
methods.pop();
}
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class LeastSpecificTypeCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
MethodTree methodTree = (MethodTree) tree;
Symbol.MethodSymbol methodSymbol = methodTree.symbol();
if (!methodSymbol.isPublic() || !Boolean.FALSE.equals(methodTree.isOverriding()) || isOverloaded(methodSymbol)) {
return;
}
boolean springInjectionAnnotated = isSpringInjectionAnnotated(methodSymbol.metadata());
methodTree.parameters().stream().map(VariableTree::symbol).filter(p -> p.type().isClass() && !p.type().symbol().isEnum() && !p.type().is("java.lang.String")).filter(p -> !(springInjectionAnnotated && p.type().is("java.util.Collection"))).forEach(p -> handleParameter(p, springInjectionAnnotated));
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class MainInServletCheck method visitNode.
@Override
public void visitNode(Tree tree) {
ClassTree node = (ClassTree) tree;
Symbol.TypeSymbol symbol = node.symbol();
if (isServletOrEjb(symbol)) {
for (Tree member : node.members()) {
if (member.is(Tree.Kind.METHOD) && ((MethodTreeImpl) member).isMainMethod()) {
reportIssue(((MethodTree) member).simpleName(), "Remove this unwanted \"main\" method.");
}
}
}
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class MethodComplexityCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
if (isExcluded(methodTree)) {
return;
}
List<Tree> complexity = context.getComplexityNodes(methodTree);
int size = complexity.size();
if (size > max) {
List<JavaFileScannerContext.Location> flow = new ArrayList<>();
for (Tree element : complexity) {
flow.add(new JavaFileScannerContext.Location("+1", element));
}
reportIssue(methodTree.simpleName(), "The Cyclomatic Complexity of this method \"" + methodTree.simpleName().name() + "\" is " + size + " which is greater than " + max + " authorized.", flow, size - max);
}
}
Aggregations