use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class MethodTooBigCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
BlockTree block = methodTree.block();
if (block != null) {
int lines = new LinesOfCodeVisitor().linesOfCode(block);
if (lines > max) {
reportIssue(methodTree.simpleName(), "This method has " + lines + " lines, which is greater than the " + max + " lines authorized. Split it into smaller methods.");
}
}
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class ObjectFinalizeCheck method isFinalizeMethodMember.
private static boolean isFinalizeMethodMember(MethodTree methodTree) {
Tree returnType = methodTree.returnType();
boolean returnVoid = returnType != null && returnType.is(Tree.Kind.PRIMITIVE_TYPE) && "void".equals(((PrimitiveTypeTree) returnType).keyword().text());
return returnVoid && "finalize".equals(methodTree.simpleName().name());
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class LiveVariablesTest method buildCFG.
private static CFG buildCFG(String methodCode) {
CompilationUnitTree cut = (CompilationUnitTree) PARSER.parse("class A { int field1; int field2; static int staticField; " + methodCode + " }");
SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
MethodTree tree = ((MethodTree) ((ClassTree) cut.types().get(0)).members().get(3));
return CFG.build(tree);
}
use of org.sonar.plugins.java.api.tree.MethodTree 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.MethodTree 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