Search in sources :

Example 1 with ResolvedAnnotationDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration in project javaparser by javaparser.

the class AnnotationsResolutionTest method solveJavassistNormalAnnotation.

@Test
void solveJavassistNormalAnnotation() throws IOException {
    // parse compilation unit and get annotation expression
    CompilationUnit cu = parseSample("Annotations");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "CD");
    MethodDeclaration method = Navigator.demandMethod(clazz, "testSomethingElse");
    NormalAnnotationExpr annotationExpr = (NormalAnnotationExpr) method.getAnnotation(0);
    // resolve annotation expression
    ResolvedAnnotationDeclaration resolved = annotationExpr.resolve();
    // check that the expected annotation declaration equals the resolved annotation declaration
    assertEquals("org.junit.Test", resolved.getQualifiedName());
    assertEquals("org.junit", resolved.getPackageName());
    assertEquals("Test", resolved.getName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ResolvedAnnotationDeclaration(com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) Test(org.junit.jupiter.api.Test)

Example 2 with ResolvedAnnotationDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration in project javaparser by javaparser.

the class AnnotationsResolutionTest method solveJavaParserMarkerAnnotation.

@Test
void solveJavaParserMarkerAnnotation() {
    // parse compilation unit and get annotation expression
    CompilationUnit cu = parseSample("Annotations");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "CA");
    MarkerAnnotationExpr annotationExpr = (MarkerAnnotationExpr) clazz.getAnnotation(0);
    // resolve annotation expression
    ResolvedAnnotationDeclaration resolved = annotationExpr.resolve();
    // check that the expected annotation declaration equals the resolved annotation declaration
    assertEquals("foo.bar.MyAnnotation", resolved.getQualifiedName());
    assertEquals("foo.bar", resolved.getPackageName());
    assertEquals("MyAnnotation", resolved.getName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ResolvedAnnotationDeclaration(com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) Test(org.junit.jupiter.api.Test)

Example 3 with ResolvedAnnotationDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration in project javaparser by javaparser.

the class AnnotationsResolutionTest method solveReflectionSingleMemberAnnotation.

@Test
void solveReflectionSingleMemberAnnotation() {
    // parse compilation unit and get annotation expression
    CompilationUnit cu = parseSample("Annotations");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "CC");
    MethodDeclaration method = Navigator.demandMethod(clazz, "foo");
    SingleMemberAnnotationExpr annotationExpr = (SingleMemberAnnotationExpr) method.getBody().get().getStatement(0).asExpressionStmt().getExpression().asVariableDeclarationExpr().getAnnotation(0);
    // resolve annotation expression
    ResolvedAnnotationDeclaration resolved = annotationExpr.resolve();
    // check that the expected annotation declaration equals the resolved annotation declaration
    assertEquals("java.lang.SuppressWarnings", resolved.getQualifiedName());
    assertEquals("java.lang", resolved.getPackageName());
    assertEquals("SuppressWarnings", resolved.getName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ResolvedAnnotationDeclaration(com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration) Test(org.junit.jupiter.api.Test)

Example 4 with ResolvedAnnotationDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration in project javaparser by javaparser.

the class AnnotationsResolutionTest method solveJavaParserSingleMemberAnnotation.

@Test
void solveJavaParserSingleMemberAnnotation() {
    // parse compilation unit and get annotation expression
    CompilationUnit cu = parseSample("Annotations");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "CC");
    SingleMemberAnnotationExpr annotationExpr = (SingleMemberAnnotationExpr) clazz.getAnnotation(0);
    // resolve annotation expression
    ResolvedAnnotationDeclaration resolved = annotationExpr.resolve();
    // check that the expected annotation declaration equals the resolved annotation declaration
    assertEquals("foo.bar.MyAnnotationWithSingleValue", resolved.getQualifiedName());
    assertEquals("foo.bar", resolved.getPackageName());
    assertEquals("MyAnnotationWithSingleValue", resolved.getName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ResolvedAnnotationDeclaration(com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration) Test(org.junit.jupiter.api.Test)

Example 5 with ResolvedAnnotationDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration in project javaparser by javaparser.

the class AnnotationsResolutionTest method solveJavassistNormalAnnotationWithDefault.

@Test
void solveJavassistNormalAnnotationWithDefault() throws IOException {
    // parse compilation unit and get annotation expression
    CompilationUnit cu = parseSample("Annotations");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "CG");
    MethodDeclaration method = Navigator.demandMethod(clazz, "testSomething");
    MarkerAnnotationExpr annotationExpr = (MarkerAnnotationExpr) method.getAnnotation(0);
    // resolve annotation expression
    ResolvedAnnotationDeclaration resolved = annotationExpr.resolve();
    // check that the expected annotation declaration equals the resolved annotation declaration
    assertEquals("org.junit.Ignore", resolved.getQualifiedName());
    Expression memberValue = resolved.getAnnotationMembers().get(0).getDefaultValue();
    assertEquals(StringLiteralExpr.class, memberValue.getClass());
    ResolvedType rt = resolved.getAnnotationMembers().get(0).getType();
    assertEquals("java.lang.String", rt.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ResolvedAnnotationDeclaration(com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.jupiter.api.Test)

Aggregations

CompilationUnit (com.github.javaparser.ast.CompilationUnit)12 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)12 ResolvedAnnotationDeclaration (com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration)12 Test (org.junit.jupiter.api.Test)12 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)7 MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)6 NormalAnnotationExpr (com.github.javaparser.ast.expr.NormalAnnotationExpr)4 SingleMemberAnnotationExpr (com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)4 Expression (com.github.javaparser.ast.expr.Expression)3 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)2 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)1