use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class SETestUtils method getSemanticModel.
public static SemanticModel getSemanticModel(String filename) {
File file = new File(filename);
CompilationUnitTree cut = (CompilationUnitTree) PARSER.parse(file);
return SemanticModel.createFor(cut, CLASSLOADER);
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class JavaPropertiesHelperTest method firstExpression.
private ExpressionTree firstExpression(String code) {
CompilationUnitTree compilationUnitTree = (CompilationUnitTree) p.parse("class A { " + code + "}");
SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
ClassTree firstType = (ClassTree) compilationUnitTree.types().get(0);
StatementTree firstStatement = ((MethodTree) firstType.members().get(0)).block().body().get(0);
return ((ExpressionStatementTree) firstStatement).expression();
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method compilation_unit.
@Test
public void compilation_unit() {
CompilationUnitTree tree = compilationUnit("import foo; import bar; class Foo {} class Bar {}");
assertThat(tree.is(Tree.Kind.COMPILATION_UNIT)).isTrue();
assertThat(tree.packageDeclaration()).isNull();
assertThat(tree.imports()).hasSize(2);
assertThat(tree.types()).hasSize(2);
assertThatChildrenIteratorHasSize(tree, 5);
tree = compilationUnit("package pkg; import foo; import bar; class Foo {} class Bar {}");
assertThat(tree.is(Tree.Kind.COMPILATION_UNIT)).isTrue();
assertThat(tree.packageDeclaration()).isNotNull();
assertThat(tree.imports()).hasSize(2);
assertThat(tree.types()).hasSize(2);
assertThatChildrenIteratorHasSize(tree, 6);
tree = compilationUnit("import foo; ; import bar; class Foo {} class Bar {}");
assertThat(tree.is(Tree.Kind.COMPILATION_UNIT)).isTrue();
assertThat(tree.packageDeclaration()).isNull();
assertThat(tree.imports()).hasSize(3);
assertThat(tree.imports().get(1).is(Kind.EMPTY_STATEMENT)).isTrue();
assertThat(tree.types()).hasSize(2);
assertThatChildrenIteratorHasSize(tree, 6);
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method line_of_tree.
@Test
public void line_of_tree() throws Exception {
CompilationUnitTree empty = compilationUnit("");
assertThat(((JavaTree) empty).getLine()).isEqualTo(1);
ClassTree classTree = firstType("class A {}");
assertThat(((JavaTree) classTree).getLine()).isEqualTo(1);
assertThat(((JavaTree) classTree.modifiers()).getLine()).isEqualTo(-1);
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class BytecodeCFGBuilderTest method getCFGForMethod.
private BytecodeCFG getCFGForMethod(String methodName) {
SquidClassLoader squidClassLoader = new SquidClassLoader(Lists.newArrayList(new File("target/test-classes"), new File("target/classes")));
File file = new File("src/test/java/org/sonar/java/bytecode/cfg/BytecodeCFGBuilderTest.java");
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
SemanticModel.createFor(tree, squidClassLoader);
Symbol.TypeSymbol innerClass = ((Symbol.TypeSymbol) ((ClassTree) tree.types().get(0)).symbol().lookupSymbols("InnerClass").iterator().next());
Symbol.MethodSymbol symbol = (Symbol.MethodSymbol) innerClass.lookupSymbols(methodName).iterator().next();
return SETestUtils.bytecodeCFG(((JavaSymbol.MethodJavaSymbol) symbol).completeSignature(), squidClassLoader);
}
Aggregations