Search in sources :

Example 1 with AnnotationInstance

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();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl) AnnotationInstance(org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance) Test(org.junit.Test)

Example 2 with AnnotationInstance

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");
}
Also used : AnnotationInstance(org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance) Test(org.junit.Test)

Example 3 with AnnotationInstance

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

Example 4 with AnnotationInstance

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();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) AnnotationInstance(org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance) Test(org.junit.Test)

Example 5 with AnnotationInstance

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();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) MethodTreeImpl(org.sonar.java.model.declaration.MethodTreeImpl) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl) AnnotationInstance(org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 AnnotationInstance (org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance)5 ClassTreeImpl (org.sonar.java.model.declaration.ClassTreeImpl)4 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)4 MethodTreeImpl (org.sonar.java.model.declaration.MethodTreeImpl)2 VariableTreeImpl (org.sonar.java.model.declaration.VariableTreeImpl)2