use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class SyntaxTreeNameFinderTest method testMemberSelectOnMethodInvocation.
@Test
public void testMemberSelectOnMethodInvocation() {
MethodTree tree = buildSyntaxTree("public void test() {int i = checkForNullMethod().length;}");
BlockTree block = tree.block();
StatementTree statementTree = block.body().get(0);
assertThat(SyntaxTreeNameFinder.getName(statementTree)).isEqualTo("checkForNullMethod");
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class SyntaxTreeNameFinderTest method testSwitch.
@Test
public void testSwitch() {
MethodTree tree = buildSyntaxTree("public void test() {int i; switch (i) { case 0: break;}}");
BlockTree block = tree.block();
StatementTree statementTree = block.body().get(1);
assertThat(SyntaxTreeNameFinder.getName(statementTree)).isEqualTo("i");
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class StaticFieldUpdateInConstructorCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
MethodTree constructor = (MethodTree) tree;
Symbol.TypeSymbol owner = constructor.symbol().enclosingClass();
Set<Symbol> staticFields = owner.memberSymbols().stream().filter(Symbol::isVariableSymbol).filter(Symbol::isStatic).collect(Collectors.toSet());
StaticFieldUpdateVisitor visitor = new StaticFieldUpdateVisitor(staticFields);
constructor.block().accept(visitor);
visitor.assignedStaticFields().forEach(identifierTree -> {
Symbol staticField = identifierTree.symbol();
reportIssue(identifierTree, "Remove this assignment of \"" + staticField.name() + "\".", Collections.singletonList(new JavaFileScannerContext.Location("", staticField.declaration())), null);
});
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class ThisExposedFromConstructorCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
MethodTree methodTree = (MethodTree) tree;
methodTree.block().accept(new ConstructorBodyVisitor(methodTree.symbol().owner()));
}
use of org.sonar.plugins.java.api.tree.MethodTree 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);
}
Aggregations