use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class UCFGJavaVisitorTest method getCompilationUnitTreeWithSemantics.
private CompilationUnitTree getCompilationUnitTreeWithSemantics(String source) {
CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse(source);
SemanticModel.createFor(cut, squidClassLoader);
return cut;
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class JavaParserTest method parent_link_should_be_computed.
@Test
public void parent_link_should_be_computed() {
CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse("class A { void foo() {} }");
ClassTree classTree = (ClassTree) cut.types().get(0);
MethodTree method = (MethodTree) classTree.members().get(0);
assertThat(method.parent()).isSameAs(classTree);
assertThat(classTree.parent()).isSameAs(cut);
assertThat(cut.parent()).isNull();
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class AnalyzerMessageTest method shouldFailOnEmptyTrees.
@Test
public void shouldFailOnEmptyTrees() {
CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse("class A {\n}\n");
try {
AnalyzerMessage.textSpanFor(cut.eofToken());
Fail.fail("Should have failed on empty issue location");
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalStateException.class);
assertThat(e.getMessage()).isEqualTo("Invalid issue location: Text span is empty when trying reporting on (l:3, c:0).");
}
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class BytecodeCFGBuilderTest method getBytecodeCFG.
public static BytecodeCFG getBytecodeCFG(String methodName, String filename) {
SquidClassLoader squidClassLoader = new SquidClassLoader(Lists.newArrayList(new File("target/test-classes"), new File("target/classes")));
File file = new File(filename);
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
SemanticModel.createFor(tree, squidClassLoader);
List<Tree> classMembers = ((ClassTree) tree.types().get(0)).members();
Symbol.MethodSymbol symbol = classMembers.stream().filter(m -> m instanceof MethodTree).map(m -> ((MethodTree) m).symbol()).filter(s -> methodName.equals(s.name())).findFirst().orElseThrow(IllegalStateException::new);
return SETestUtils.bytecodeCFG(((JavaSymbol.MethodJavaSymbol) symbol).completeSignature(), squidClassLoader);
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class VariableReadExtractorTest method buildMethodTree.
private static MethodTree buildMethodTree(String methodCode) {
CompilationUnitTree cut = (CompilationUnitTree) PARSER.parse("class A { int field1; int field2; " + methodCode + " }");
SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
return (MethodTree) ((ClassTree) cut.types().get(0)).members().get(2);
}
Aggregations