use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class TooManyMethodsCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
ClassTree classTree = (ClassTree) tree;
List<Tree> methods = classTree.members().stream().filter(member -> member.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR) && (countNonPublic || ((MethodTree) member).symbol().isPublic())).collect(Collectors.toList());
if (shouldNotReportIssue(classTree, methods)) {
return;
}
List<JavaFileScannerContext.Location> secondary = methods.stream().map(element -> new JavaFileScannerContext.Location("Method + 1", element)).collect(Collectors.toList());
String classDescription;
if (classTree.simpleName() == null) {
classDescription = "Anonymous class \"" + ((NewClassTree) classTree.parent()).identifier().symbolType().name() + "\"";
} else {
classDescription = classTree.declarationKeyword().text() + " \"" + classTree.simpleName() + "\"";
}
reportIssue(ExpressionsHelper.reportOnClassTree(classTree), String.format("%s has %d%s methods, which is greater than the %d authorized. Split it into smaller classes.", classDescription, methods.size(), countNonPublic ? "" : " public", maximumMethodThreshold), secondary, null);
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class SpringConstructorInjectionCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
ClassTree classTree = (ClassTree) tree;
if (isClassTreeAnnotatedWith(classTree, "org.springframework.stereotype.Controller", "org.springframework.stereotype.Repository", "org.springframework.stereotype.Service")) {
List<Tree> toReport = classTree.members().stream().filter(SpringConstructorInjectionCheck::isMemberAutowired).map(SpringConstructorInjectionCheck::toReportTree).collect(Collectors.toList());
if (!toReport.isEmpty()) {
int cost = toReport.size();
// find constructor
classTree.members().stream().filter(m -> m.is(Kind.CONSTRUCTOR)).map(m -> ((MethodTree) m).simpleName()).findFirst().ifPresent(toReport::add);
reportIssue(toReport.get(0), "Remove this annotation and use constructor injection instead.", toReport.stream().skip(1).map(i -> new JavaFileScannerContext.Location("", i)).collect(Collectors.toList()), cost);
}
}
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class JavaParserTest method receiver_type_should_be_parsed.
@Test
public void receiver_type_should_be_parsed() throws Exception {
try {
String code = "class Main { class Inner { Inner(Main Main.this) {}};}";
CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse(code);
Tree constructor = ((ClassTree) ((ClassTree) cut.types().get(0)).members().get(0)).members().get(0);
assertThat(constructor).isInstanceOf(MethodTree.class);
assertThat(((MethodTree) constructor).parameters().get(0).simpleName().name()).isEqualTo("this");
} catch (Exception ex) {
fail("Receiver type of inner classes should be parsed correctly", ex);
}
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class SimpleClassNameCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
CompilationUnitTree cut = (CompilationUnitTree) tree;
cut.types().stream().filter(NOT_EMPTY_STATEMENT).map(t -> ((ClassTree) t).symbol()).forEach(this::checkSymbol);
List<ImportTree> imports = cut.imports().stream().filter(NOT_EMPTY_STATEMENT).map(t -> (ImportTree) t).collect(Collectors.toList());
boolean fileContainsStarImport = imports.stream().filter(it -> it.qualifiedIdentifier().is(Kind.MEMBER_SELECT)).map(it -> ((MemberSelectExpressionTree) it.qualifiedIdentifier()).identifier()).anyMatch(i -> "*".equals(i.name()));
if (!fileContainsStarImport) {
checkImports(imports);
}
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class StandardFunctionalInterfaceCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
ClassTree classTree = (ClassTree) tree;
// classTree.simpleName() never null for Tree.Kind.INTERFACE
IdentifierTree issueLocation = classTree.simpleName();
// The question "Why we raise issue only for interface annotated with @FunctionalInterface?"
// is discussed in comments of https://jira.sonarsource.com/browse/SONARJAVA-504
Optional.of(classTree).filter(StandardFunctionalInterfaceCheck::isFunctionalInterface).filter(StandardFunctionalInterfaceCheck::isNonStandardFunctionalInterface).filter(StandardFunctionalInterfaceCheck::hasNoExtension).flatMap(StandardFunctionalInterfaceCheck::lookupFunctionalMethod).flatMap(StandardFunctionalInterfaceCheck::lookupMatchingStandardInterface).ifPresent(standardInterface -> reportIssue(issueLocation, buildIssueMessage(classTree, standardInterface)));
}
Aggregations