Search in sources :

Example 16 with CompilationUnitTree

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);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) File(java.io.File)

Example 17 with CompilationUnitTree

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();
}
Also used : ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 18 with CompilationUnitTree

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);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Test(org.junit.Test)

Example 19 with CompilationUnitTree

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);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Example 20 with CompilationUnitTree

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);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) JavaSymbol(org.sonar.java.resolve.JavaSymbol) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Aggregations

CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)94 Test (org.junit.Test)46 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)42 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)35 File (java.io.File)26 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)25 Tree (org.sonar.plugins.java.api.tree.Tree)20 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)13 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)10 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)9 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)9 List (java.util.List)8 ClassTreeImpl (org.sonar.java.model.declaration.ClassTreeImpl)8 Type (org.sonar.plugins.java.api.semantic.Type)8 SemanticModel (org.sonar.java.resolve.SemanticModel)7 Collectors (java.util.stream.Collectors)6 Symbol (org.sonar.plugins.java.api.semantic.Symbol)6 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)6 ArrayList (java.util.ArrayList)5 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)5