use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolverTest method annotation_on_type.
@Test
public void annotation_on_type() {
CompilationUnitTree compilationUnit = treeOf("@interface MyAnnotation { } @MyAnnotation class Class { }");
ClassTreeImpl annotation = (ClassTreeImpl) compilationUnit.types().get(0);
ClassTreeImpl clazz = (ClassTreeImpl) compilationUnit.types().get(1);
List<AnnotationInstance> annotations = ((JavaSymbol.TypeJavaSymbol) clazz.symbol()).metadata().annotations();
assertThat(annotations.size()).isEqualTo(1);
assertThat(annotations.get(0).symbol().type().is(annotation.symbol().name())).isTrue();
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolverTest method parametrized_constructor.
@Test
public void parametrized_constructor() {
CompilationUnitTree compilationUnit = treeOf("class A { <T> A(T t) {} void test() { new A(\"hello\"); } }");
MethodTree testMethod = (MethodTree) ((ClassTreeImpl) compilationUnit.types().get(0)).members().get(1);
NewClassTree newClassTree = (NewClassTree) ((ExpressionStatementTree) testMethod.block().body().get(0)).expression();
Type identifierType = newClassTree.identifier().symbolType();
assertThat(identifierType).isInstanceOf(MethodJavaType.class);
assertThat(((MethodJavaType) identifierType).argTypes.get(0).is("java.lang.String")).isTrue();
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolverTest method returnTypeOf.
private Type returnTypeOf(String method, String invocation) {
CompilationUnitTree compilationUnit = treeOf("class A { " + method + " void test() { " + invocation + " } }");
MethodTree testMethod = (MethodTree) ((ClassTreeImpl) compilationUnit.types().get(0)).members().get(1);
ExpressionTree methodInvocation = ((ExpressionStatementTree) testMethod.block().body().get(0)).expression();
return methodInvocation.symbolType();
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolverTest method annotation_on_method_parameter.
@Test
public void annotation_on_method_parameter() {
CompilationUnitTree compilationUnit = treeOf("@interface MyAnnotation { } class Class { void method(@MyAnnotation int a) { } }");
ClassTreeImpl annotation = (ClassTreeImpl) compilationUnit.types().get(0);
ClassTreeImpl clazz = (ClassTreeImpl) compilationUnit.types().get(1);
MethodTreeImpl method = (MethodTreeImpl) clazz.members().get(0);
VariableTreeImpl parameter = (VariableTreeImpl) method.parameters().get(0);
List<AnnotationInstance> annotations = parameter.getSymbol().metadata().annotations();
assertThat(annotations.size()).isEqualTo(1);
assertThat(annotations.get(0).symbol().type().is(annotation.symbol().name())).isTrue();
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolverTest method treeOf.
private CompilationUnitTree treeOf(String input) {
CompilationUnitTree tree = parse(input);
SemanticModel.createFor(tree, new SquidClassLoader(Collections.emptyList()));
return tree;
}
Aggregations