use of org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance in project sonar-java by SonarSource.
the class TypeAndReferenceSolverTest method annotation_on_variable.
@Test
public void annotation_on_variable() {
CompilationUnitTree compilationUnit = treeOf("@interface MyAnnotation { } class Class { @MyAnnotation Object field; }");
ClassTreeImpl annotation = (ClassTreeImpl) compilationUnit.types().get(0);
ClassTreeImpl clazz = (ClassTreeImpl) compilationUnit.types().get(1);
VariableTreeImpl variable = (VariableTreeImpl) clazz.members().get(0);
List<AnnotationInstance> annotations = variable.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.semantic.SymbolMetadata.AnnotationInstance in project sonar-java by SonarSource.
the class TypeAndReferenceSolverTest method annotation_completion.
@Test
public void annotation_completion() {
AnnotationInstance annotation1 = extractFirstAnnotationInstance("@interface MyAnnotation { } @MyAnnotation() class Class { }");
assertThat(annotation1.values()).isEmpty();
AnnotationInstance annotation2 = extractFirstAnnotationInstance("@interface MyAnnotation { } @MyAnnotation(expr) class Class { }");
assertThat(annotation2.values().size()).isEqualTo(1);
assertThat(annotation2.values().get(0).name()).isEqualTo("");
AnnotationInstance annotation3 = extractFirstAnnotationInstance("@interface MyAnnotation { public static final String field; String value(); } @MyAnnotation(expr) class Class { }");
assertThat(annotation3.values().size()).isEqualTo(1);
assertThat(annotation3.values().get(0).name()).isEqualTo("value");
AnnotationInstance annotation4 = extractFirstAnnotationInstance("@interface MyAnnotation { } @MyAnnotation(expr = val) class Class { }");
assertThat(annotation4.values().size()).isEqualTo(1);
assertThat(annotation4.values().get(0).name()).isEqualTo("expr");
AnnotationInstance annotation5 = extractFirstAnnotationInstance("@interface MyAnnotation { } @MyAnnotation(expr1 = val1, expr2 = val2) class Class { }");
assertThat(annotation5.values().size()).isEqualTo(2);
assertThat(annotation5.values().get(0).name()).isEqualTo("expr1");
assertThat(annotation5.values().get(1).name()).isEqualTo("expr2");
}
use of org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance 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();
}
use of org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance 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.semantic.SymbolMetadata.AnnotationInstance 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();
}
Aggregations