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();
}
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");
}
}
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);
}
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();
}
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();
}
Aggregations