Search in sources :

Example 1 with NormalAnnotationExpr

use of com.github.javaparser.ast.expr.NormalAnnotationExpr in project checker-framework by typetools.

the class StubParser method getAnnotation.

/**
 * Convert {@code annotation} into an AnnotationMirror. Returns null if the annotation isn't
 * supported by the checker or if some error occurred while converting it.
 */
private AnnotationMirror getAnnotation(AnnotationExpr annotation, Map<String, AnnotationMirror> allStubAnnotations) {
    AnnotationMirror annoMirror;
    if (annotation instanceof MarkerAnnotationExpr) {
        String annoName = ((MarkerAnnotationExpr) annotation).getNameAsString();
        annoMirror = allStubAnnotations.get(annoName);
    } else if (annotation instanceof NormalAnnotationExpr) {
        NormalAnnotationExpr nrmanno = (NormalAnnotationExpr) annotation;
        String annoName = nrmanno.getNameAsString();
        annoMirror = allStubAnnotations.get(annoName);
        if (annoMirror == null) {
            // Not a supported qualifier -> ignore
            return null;
        }
        AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoMirror);
        List<MemberValuePair> pairs = nrmanno.getPairs();
        if (pairs != null) {
            for (MemberValuePair mvp : pairs) {
                String member = mvp.getNameAsString();
                Expression exp = mvp.getValue();
                boolean success = handleExpr(builder, member, exp);
                if (!success) {
                    stubWarn("Annotation expression, %s, could not be processed for annotation: %s. ", exp, annotation);
                    return null;
                }
            }
        }
        return builder.build();
    } else if (annotation instanceof SingleMemberAnnotationExpr) {
        SingleMemberAnnotationExpr sglanno = (SingleMemberAnnotationExpr) annotation;
        String annoName = sglanno.getNameAsString();
        annoMirror = allStubAnnotations.get(annoName);
        if (annoMirror == null) {
            // Not a supported qualifier -> ignore
            return null;
        }
        AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoMirror);
        Expression valexpr = sglanno.getMemberValue();
        boolean success = handleExpr(builder, "value", valexpr);
        if (!success) {
            stubWarn("Annotation expression, %s, could not be processed for annotation: %s. ", valexpr, annotation);
            return null;
        }
        return builder.build();
    } else {
        ErrorReporter.errorAbort("StubParser: unknown annotation type: " + annotation);
        // dead code
        annoMirror = null;
    }
    return annoMirror;
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) AnnotationMirror(javax.lang.model.element.AnnotationMirror) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair) AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder) Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) ArrayList(java.util.ArrayList) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr)

Example 2 with NormalAnnotationExpr

use of com.github.javaparser.ast.expr.NormalAnnotationExpr in project javaparser by javaparser.

the class ArrayCreationLevelTransformationsTest method replacingAnnotation.

@Test
public void replacingAnnotation() throws IOException {
    ArrayCreationLevel it = consider("@myAnno []");
    it.getAnnotations().set(0, new NormalAnnotationExpr(new Name("myOtherAnno"), new NodeList<>()));
    assertTransformedToString("@myOtherAnno() []", it);
}
Also used : NodeList(com.github.javaparser.ast.NodeList) ArrayCreationLevel(com.github.javaparser.ast.ArrayCreationLevel) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) Name(com.github.javaparser.ast.expr.Name) Test(org.junit.Test) AbstractLexicalPreservingTest(com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest)

Example 3 with NormalAnnotationExpr

use of com.github.javaparser.ast.expr.NormalAnnotationExpr in project javaparser by javaparser.

the class NodeWithAnnotations method addAnnotation.

/**
 * Annotates this
 *
 * @param name the name of the annotation
 * @return the {@link NormalAnnotationExpr} added
 */
public default NormalAnnotationExpr addAnnotation(String name) {
    NormalAnnotationExpr normalAnnotationExpr = new NormalAnnotationExpr(name(name), null);
    getAnnotations().add(normalAnnotationExpr);
    normalAnnotationExpr.setParentNode((Node) this);
    return normalAnnotationExpr;
}
Also used : NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr)

Example 4 with NormalAnnotationExpr

use of com.github.javaparser.ast.expr.NormalAnnotationExpr in project javaparser by javaparser.

the class NodeWithAnnotationsBuildersTest method testGetAnnotationByName.

@Test
public void testGetAnnotationByName() {
    NormalAnnotationExpr annotation = testClass.addAndGetAnnotation(hey.class);
    assertEquals(annotation, testClass.getAnnotationByName("hey").get());
}
Also used : NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) Test(org.junit.Test)

Example 5 with NormalAnnotationExpr

use of com.github.javaparser.ast.expr.NormalAnnotationExpr in project javaparser by javaparser.

the class NodeWithAnnotationsBuildersTest method testGetAnnotationByClass.

@Test
public void testGetAnnotationByClass() {
    NormalAnnotationExpr annotation = testClass.addAndGetAnnotation(hey.class);
    assertEquals(annotation, testClass.getAnnotationByClass(hey.class).get());
}
Also used : NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) Test(org.junit.Test)

Aggregations

NormalAnnotationExpr (com.github.javaparser.ast.expr.NormalAnnotationExpr)12 Test (org.junit.Test)5 NodeList (com.github.javaparser.ast.NodeList)4 MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)4 MemberValuePair (com.github.javaparser.ast.expr.MemberValuePair)4 SingleMemberAnnotationExpr (com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)4 Name (com.github.javaparser.ast.expr.Name)3 Expression (com.github.javaparser.ast.expr.Expression)2 AbstractLexicalPreservingTest (com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AnnotationBuilder (org.checkerframework.javacutil.AnnotationBuilder)2 ArrayCreationLevel (com.github.javaparser.ast.ArrayCreationLevel)1 AnnotationMemberDeclaration (com.github.javaparser.ast.body.AnnotationMemberDeclaration)1 AssignmentTree (com.sun.source.tree.AssignmentTree)1 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)1 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 IdentifierTree (com.sun.source.tree.IdentifierTree)1 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)1