Search in sources :

Example 71 with CompilationUnitTree

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

the class TypeAndReferenceSolverTest method typeOf.

private JavaType typeOf(String input) {
    SemanticModel semanticModel = mock(SemanticModel.class);
    when(semanticModel.getEnv(any(Tree.class))).thenReturn(env);
    TypeAndReferenceSolver visitor = new TypeAndReferenceSolver(semanticModel, symbols, new Resolve(symbols, bytecodeCompleter, parametrizedTypeCache), parametrizedTypeCache);
    String p = "class Test { void wrapperMethod() { " + input + "; } }";
    CompilationUnitTree tree = parse(p);
    tree.accept(visitor);
    TestedNodeExtractor testedNodeExtractor = new TestedNodeExtractor(false);
    testedNodeExtractor.visitCompilationUnit(tree);
    return visitor.getType(((ExpressionStatementTree) testedNodeExtractor.testedNode).expression());
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 72 with CompilationUnitTree

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

the class ComplexityVisitorTest method lambda_complexity.

@Test
public void lambda_complexity() throws Exception {
    CompilationUnitTree cut = (CompilationUnitTree) p.parse("class A { Function f = s -> {if(s.isEmpty()) return s; return new MyClass(){ void foo(){if(a) return;} };};}");
    ExpressionTree lambda = ((VariableTree) ((ClassTree) cut.types().get(0)).members().get(0)).initializer();
    List<Tree> nodes = new ComplexityVisitor().getNodes(lambda);
    assertThat(nodes).hasSize(2);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 73 with CompilationUnitTree

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

the class ComplexityVisitorTest method method_complexity.

@Test
public void method_complexity() throws Exception {
    CompilationUnitTree cut = (CompilationUnitTree) p.parse("class A {" + " Object foo(){" + " if(a) { " + "    return new MyClass(){ " + "        void foo(){" + "            if(a) {return;} " + "        } " + "    };" + " } " + "}}");
    MethodTree methodTree = (MethodTree) ((ClassTree) cut.types().get(0)).members().get(0);
    List<Tree> nodes = new ComplexityVisitor().getNodes(methodTree);
    assertThat(nodes).hasSize(2);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 74 with CompilationUnitTree

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

the class ExportsDirectiveTreeImplTest method test_BaseTreeVisitor.

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

Example 75 with CompilationUnitTree

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

the class ModuleDeclarationTreeImplTest method no_module.

@Test
public void no_module() {
    CompilationUnitTree cut = createTree("package org.foo;");
    assertThat(cut.moduleDeclaration()).isNull();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Test(org.junit.Test)

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