Search in sources :

Example 61 with CompilationUnitTree

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

the class BytecodeSECheckTest method initializeWalker.

@BeforeClass
public static void initializeWalker() {
    List<File> files = new ArrayList<>();
    files.add(new File("target/test-classes"));
    squidClassLoader = new SquidClassLoader(files);
    CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse("class A {}");
    semanticModel = SemanticModel.createFor(tree, squidClassLoader);
    walker = getEGWalker();
}
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)

Example 62 with CompilationUnitTree

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

the class CFGTest method build_partial_cfg.

private void build_partial_cfg(String breakOrContinue) {
    String methodCode = "void meth(){ try {fun(); } catch ( Exception e) {e.printStackTrace(); " + breakOrContinue + "; } }";
    CompilationUnitTree cut = (CompilationUnitTree) parser.parse("class A {" + methodCode + "}");
    SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
    MethodTree methodTree = (MethodTree) ((ClassTree) cut.types().get(0)).members().get(0);
    List<StatementTree> body = methodTree.block().body();
    CFG cfg = CFG.buildCFG(body, true);
    cfg.setMethodSymbol(methodTree.symbol());
    assertThat(cfg.blocks()).hasSize(5);
    assertThat(cfg.methodSymbol()).isSameAs(methodTree.symbol());
    try {
        CFG.buildCFG(body, false);
        fail("IllegalStateException should have been thrown");
    } catch (IllegalStateException iae) {
        assertThat(iae).hasMessage("'" + breakOrContinue + "' statement not in loop or switch statement");
    }
}
Also used : 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) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 63 with CompilationUnitTree

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

the class LeastUpperBoundTest method lub_of_one_element_is_itself.

@Test
public void lub_of_one_element_is_itself() {
    CompilationUnitTree cut = treeOf("class A<T> { A<String> var; }");
    ClassTree classA = (ClassTree) cut.types().get(0);
    Type varType = ((VariableTree) classA.members().get(0)).type().symbolType();
    Type a = classA.symbol().type();
    assertThat(leastUpperBound(a)).isSameAs(a);
    assertThat(leastUpperBound(varType)).isSameAs(varType);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Type(org.sonar.plugins.java.api.semantic.Type) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Example 64 with CompilationUnitTree

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

the class TypeAndReferenceSolverTest method assertUnary.

private void assertUnary(String input, Type.Primitives expectedPrimitive) {
    CompilationUnitTree compilationUnit = treeOf(input);
    ClassTreeImpl clazz = (ClassTreeImpl) compilationUnit.types().get(0);
    Type type = ((ReturnStatementTree) ((MethodTree) clazz.members().get(1)).block().body().get(0)).expression().symbolType();
    assertThat(type.isPrimitive(expectedPrimitive)).isTrue();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) Type(org.sonar.plugins.java.api.semantic.Type) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 65 with CompilationUnitTree

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

the class TypeAndReferenceSolverTest method annotation_on_method.

@Test
public void annotation_on_method() {
    CompilationUnitTree compilationUnit = treeOf("@interface MyAnnotation { } class Class { @MyAnnotation void method() { } }");
    ClassTreeImpl annotation = (ClassTreeImpl) compilationUnit.types().get(0);
    ClassTreeImpl clazz = (ClassTreeImpl) compilationUnit.types().get(1);
    MethodTreeImpl method = (MethodTreeImpl) clazz.members().get(0);
    List<AnnotationInstance> annotations = ((JavaSymbol.MethodJavaSymbol) method.symbol()).metadata().annotations();
    assertThat(annotations.size()).isEqualTo(1);
    assertThat(annotations.get(0).symbol().type().is(annotation.symbol().name())).isTrue();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) MethodTreeImpl(org.sonar.java.model.declaration.MethodTreeImpl) AnnotationInstance(org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance) 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