Search in sources :

Example 1 with ReflectionAnnotationDeclaration

use of com.github.javaparser.symbolsolver.reflectionmodel.ReflectionAnnotationDeclaration in project javaparser by javaparser.

the class AnnotationsResolutionTest method solveReflectionMetaAnnotations.

@Test
void solveReflectionMetaAnnotations() {
    // parse compilation unit and get annotation expression
    CompilationUnit cu = parseSample("Annotations");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "CA");
    MethodDeclaration method = Navigator.demandMethod(clazz, "equals");
    MarkerAnnotationExpr annotationExpr = (MarkerAnnotationExpr) method.getAnnotation(0);
    // resolve annotation expression @Override
    ReflectionAnnotationDeclaration resolved = (ReflectionAnnotationDeclaration) annotationExpr.resolve();
    // check that the annotation @Override has the annotations @Target and @Retention, but not @Documented
    assertEquals("java.lang.Override", resolved.getQualifiedName());
    assertTrue(resolved.hasDirectlyAnnotation("java.lang.annotation.Target"));
    assertTrue(resolved.hasDirectlyAnnotation("java.lang.annotation.Retention"));
    assertFalse(resolved.hasDirectlyAnnotation("java.lang.annotation.Documented"));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ReflectionAnnotationDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionAnnotationDeclaration) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) Test(org.junit.jupiter.api.Test)

Aggregations

CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)1 ReflectionAnnotationDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionAnnotationDeclaration)1 Test (org.junit.jupiter.api.Test)1