Search in sources :

Example 31 with CompilationUnitTree

use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.

the class ClassTreeImplTest method getLine.

@Test
public void getLine() {
    CompilationUnitTree tree = createTree("class A {\n" + "A a = new A() {};" + "\n}");
    ClassTree classTree = (ClassTree) tree.types().get(0);
    assertThat(((JavaTree) classTree).getLine()).isEqualTo(1);
    // get line of anonymous class
    NewClassTree newClassTree = (NewClassTree) ((VariableTree) classTree.members().get(0)).initializer();
    assertThat(((JavaTree) newClassTree.classBody()).getLine()).isEqualTo(2);
}
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) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) JavaTree(org.sonar.java.model.JavaTree) Test(org.junit.Test)

Example 32 with CompilationUnitTree

use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.

the class MethodTreeImplTest method symbol_not_set_should_lead_to_null_result.

@Test
public void symbol_not_set_should_lead_to_null_result() throws Exception {
    CompilationUnitTree cut = (CompilationUnitTree) p.parse("class A { String toString(){return \"\";}}");
    MethodTreeImpl methodTree = (MethodTreeImpl) ((ClassTree) cut.types().get(0)).members().get(0);
    assertThat(methodTree.isOverriding()).isNull();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Example 33 with CompilationUnitTree

use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.

the class OpensDirectiveTreeImplTest method moduleDirective.

private OpensDirectiveTree moduleDirective(String exportsDirective) {
    CompilationUnitTree compilationUnitTree = createTree("module org.foo {\n  " + exportsDirective + "\n}");
    SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
    return (OpensDirectiveTree) compilationUnitTree.moduleDeclaration().moduleDirectives().get(0);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) OpensDirectiveTree(org.sonar.plugins.java.api.tree.OpensDirectiveTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 34 with CompilationUnitTree

use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.

the class MethodInvocationTreeImplTest method symbol_should_be_set.

@Test
public void symbol_should_be_set() {
    CompilationUnitTree cut = createTree("class A { void foo(){} void bar(){foo();} }");
    ClassTree classTree = (ClassTree) cut.types().get(0);
    Symbol.MethodSymbol declaration = ((MethodTree) classTree.members().get(0)).symbol();
    StatementTree statementTree = ((MethodTree) classTree.members().get(1)).block().body().get(0);
    MethodInvocationTree mit = (MethodInvocationTree) ((ExpressionStatementTree) statementTree).expression();
    assertThat(mit.symbol()).isSameAs(declaration);
    assertThat(mit.arguments()).isNotNull();
    assertThat(mit.arguments().openParenToken()).isNotNull();
    assertThat(mit.arguments().closeParenToken()).isNotNull();
}
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) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Example 35 with CompilationUnitTree

use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.

the class MethodInvocationTreeImplTest method createTree.

private CompilationUnitTree createTree(String code) {
    CompilationUnitTree compilationUnitTree = (CompilationUnitTree) p.parse(code);
    SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
    return compilationUnitTree;
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) 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