use of org.sonar.plugins.java.api.tree.CompilationUnitTree 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.CompilationUnitTree in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method athrow_should_not_be_linked_to_next_label.
@Test
public void athrow_should_not_be_linked_to_next_label() throws Exception {
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse("class A {int field;}");
SquidClassLoader classLoader = new SquidClassLoader(Collections.singletonList(new File("src/test/JsrRet")));
SemanticModel semanticModel = SemanticModel.createFor(tree, classLoader);
BehaviorCache behaviorCache = new BehaviorCache(classLoader);
behaviorCache.setFileContext(null, semanticModel);
BytecodeEGWalker walker = new BytecodeEGWalker(behaviorCache, semanticModel);
MethodBehavior methodBehavior = walker.getMethodBehavior("org.apache.commons.io.FileUtils#readFileToString(Ljava/io/File;)Ljava/lang/String;", classLoader);
assertThat(methodBehavior.happyPathYields().collect(Collectors.toList())).hasSize(1);
assertThat(methodBehavior.exceptionalPathYields().collect(Collectors.toList())).hasSize(2);
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree 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.CompilationUnitTree 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.CompilationUnitTree in project sonar-java by SonarSource.
the class AnalyzerMessageTest method textSpanForTrees.
@Test
public void textSpanForTrees() {
CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse("class A {\n}\n");
ClassTree classTree = (ClassTree) cut.types().get(0);
TextSpan textSpan;
textSpan = AnalyzerMessage.textSpanFor(classTree);
assertThat(textSpan.startLine).isEqualTo(1);
assertThat(textSpan.startCharacter).isEqualTo(0);
assertThat(textSpan.endLine).isEqualTo(2);
assertThat(textSpan.endCharacter).isEqualTo(1);
textSpan = AnalyzerMessage.textSpanBetween(classTree.declarationKeyword(), classTree.openBraceToken());
assertThat(textSpan.startLine).isEqualTo(1);
assertThat(textSpan.startCharacter).isEqualTo(0);
assertThat(textSpan.endLine).isEqualTo(1);
assertThat(textSpan.endCharacter).isEqualTo(9);
}
Aggregations