use of org.sonar.plugins.java.api.tree.ClassTree 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.ClassTree 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.ClassTree in project sonar-java by SonarSource.
the class NoTestInTestClassCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (hasSemantic()) {
resetAnnotationCache();
CompilationUnitTree cut = (CompilationUnitTree) tree;
cut.types().stream().filter(typeTree -> typeTree.is(Kind.CLASS)).forEach(typeTree -> checkClass((ClassTree) typeTree));
}
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class ComplexityVisitorTest method switch_handling.
@Test
public void switch_handling() throws Exception {
CompilationUnitTree cut = (CompilationUnitTree) p.parse("class A {" + " String foo(int a) {" + " switch (a) {" + " case 0:" + " return \"none\";" + " case 1:" + " return \"one\";" + " case 2:" + " return \"many\";" + " default:" + " return \"it's complicated\";" + " }" + " }" + "}");
MethodTree methodTree = (MethodTree) ((ClassTree) cut.types().get(0)).members().get(0);
List<Tree> nodes = new ComplexityVisitor().getNodes(methodTree);
// default case does not count.
assertThat(nodes).hasSize(4);
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class PublicApiCheckerTest method getPublicApiVisitor.
private SubscriptionVisitor getPublicApiVisitor() {
return new SubscriptionVisitor() {
private final Deque<ClassTree> classTrees = Lists.newLinkedList();
private final Deque<MethodTree> methodTrees = Lists.newLinkedList();
@Override
public List<Tree.Kind> nodesToVisit() {
return Arrays.asList(Tree.Kind.values());
}
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) tree;
String name = variableTree.simpleName().name();
Tree parent = classTrees.peek();
if (!methodTrees.isEmpty()) {
parent = methodTrees.peek();
}
assertThat(PublicApiChecker.isPublicApi(parent, tree)).as(name).isEqualTo(name.endsWith("Public"));
} else if (tree.is(PublicApiChecker.methodKinds())) {
MethodTree methodTree = (MethodTree) tree;
methodTrees.push(methodTree);
String name = methodTree.simpleName().name();
// getters and setters are included in the public API
assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).as(name).isEqualTo(name.endsWith("Public") || name.contains("GetSet"));
} else if (tree.is(PublicApiChecker.classKinds())) {
IdentifierTree className = ((ClassTree) tree).simpleName();
if (className == null) {
assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).isFalse();
} else {
assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).as(className.name()).isEqualTo(className != null && className.name().endsWith("Public"));
}
classTrees.push((ClassTree) tree);
} else {
assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).isFalse();
}
}
@Override
public void leaveNode(Tree tree) {
if (tree.is(PublicApiChecker.classKinds())) {
classTrees.pop();
} else if (tree.is(PublicApiChecker.methodKinds())) {
methodTrees.pop();
}
}
};
}
Aggregations