Search in sources :

Example 56 with CompilationUnitTree

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

the class OpensDirectiveTreeImplTest method createTree.

private CompilationUnitTree createTree(String... lines) {
    CompilationUnitTree compilationUnitTree = (CompilationUnitTree) p.parse(Arrays.stream(lines).collect(Collectors.joining("\n")));
    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)

Example 57 with CompilationUnitTree

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

the class OpensDirectiveTreeImplTest method test_BaseTreeVisitor.

@Test
public void test_BaseTreeVisitor() {
    CompilationUnitTree cut = createTree("@org.foo.Bar", "open module com.greetings {", "  exports foo;", "  requires org.gul;", "  opens org.bar to com.module1, module2;", "}");
    OpensDirectiveVisitor moduleDeclarationVisitor = new OpensDirectiveVisitor();
    cut.accept(moduleDeclarationVisitor);
    assertThat(moduleDeclarationVisitor.visited).isTrue();
    assertThat(moduleDeclarationVisitor.directives).hasSize(1);
    assertThat(moduleDeclarationVisitor.identifiers).containsExactly("com", "greetings", "com", "module1", "module2");
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Test(org.junit.Test)

Example 58 with CompilationUnitTree

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

the class ReassignmentFinderTest method classTree.

private ClassTree classTree(String classBody) {
    CompilationUnitTree compilationUnitTree = (CompilationUnitTree) p.parse(classBody);
    SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
    return (ClassTree) compilationUnitTree.types().get(0);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 59 with CompilationUnitTree

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

the class ValueBasedUtilsTest method testIsValueBased.

@Test
public void testIsValueBased() throws Exception {
    File file = new File("src/test/files/checks/helpers/ValueBasedUtilsTest.java");
    CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
    SemanticModel.createFor(tree, new SquidClassLoader(Collections.emptyList()));
    List<Tree> members = ((ClassTree) tree.types().get(0)).members();
    members.stream().forEach(member -> checkMember((VariableTree) member));
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Test(org.junit.Test)

Example 60 with CompilationUnitTree

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

the class BytecodeEGWalkerExecuteTest method initializeClassLoaderAndSemanticModel.

@BeforeClass
public static void initializeClassLoaderAndSemanticModel() {
    List<File> files = new ArrayList<>(FileUtils.listFiles(new File("target/test-jars"), new String[] { "jar", "zip" }, true));
    files.add(new File("target/classes"));
    files.add(new File("target/test-classes"));
    squidClassLoader = new SquidClassLoader(files);
    File file = new File("src/test/java/org/sonar/java/bytecode/se/BytecodeEGWalkerExecuteTest.java");
    CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
    semanticModel = SemanticModel.createFor(tree, squidClassLoader);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ArrayList(java.util.ArrayList) File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) BeforeClass(org.junit.BeforeClass)

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