Search in sources :

Example 6 with NormalAnnotationExpr

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

the class AnnotationMemberDeclarationTransformationsTest method replacingAnnotation.

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

Example 7 with NormalAnnotationExpr

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

the class JointJavacJavaParserVisitor method visitAnnotation.

@Override
public Void visitAnnotation(AnnotationTree javacTree, Node javaParserNode) {
    // as @MyAnno(value="myArg") which has a single element argument list with an assignment.
    if (javaParserNode instanceof MarkerAnnotationExpr) {
        processAnnotation(javacTree, (MarkerAnnotationExpr) javaParserNode);
    } else if (javaParserNode instanceof SingleMemberAnnotationExpr) {
        SingleMemberAnnotationExpr node = (SingleMemberAnnotationExpr) javaParserNode;
        processAnnotation(javacTree, node);
        assert javacTree.getArguments().size() == 1;
        ExpressionTree value = javacTree.getArguments().get(0);
        assert value instanceof AssignmentTree;
        AssignmentTree assignment = (AssignmentTree) value;
        assert assignment.getVariable().getKind() == Tree.Kind.IDENTIFIER;
        assert ((IdentifierTree) assignment.getVariable()).getName().contentEquals("value");
        assignment.getExpression().accept(this, node.getMemberValue());
    } else if (javaParserNode instanceof NormalAnnotationExpr) {
        NormalAnnotationExpr node = (NormalAnnotationExpr) javaParserNode;
        processAnnotation(javacTree, node);
        assert javacTree.getArguments().size() == node.getPairs().size();
        Iterator<MemberValuePair> argIter = node.getPairs().iterator();
        for (ExpressionTree arg : javacTree.getArguments()) {
            assert arg instanceof AssignmentTree;
            AssignmentTree assignment = (AssignmentTree) arg;
            IdentifierTree memberName = (IdentifierTree) assignment.getVariable();
            MemberValuePair javaParserArg = argIter.next();
            assert memberName.getName().contentEquals(javaParserArg.getNameAsString());
            assignment.getExpression().accept(this, javaParserArg.getValue());
        }
    } else {
        throwUnexpectedNodeType(javacTree, javaParserNode);
    }
    return null;
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AssignmentTree(com.sun.source.tree.AssignmentTree) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr)

Example 8 with NormalAnnotationExpr

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

the class NodeWithAnnotationsBuildersTest method testAddAnnotation.

@Test
public void testAddAnnotation() {
    NormalAnnotationExpr annotation = testClass.addAndGetAnnotation(hey.class);
    assertEquals("import com.github.javaparser.builders.NodeWithAnnotationsBuildersTest.hey;", cu.getImport(0).toString().trim());
    assertEquals(1, testClass.getAnnotations().size());
    assertEquals(annotation, testClass.getAnnotation(0));
    assertEquals(NormalAnnotationExpr.class, testClass.getAnnotation(0).getClass());
}
Also used : NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) Test(org.junit.Test)

Example 9 with NormalAnnotationExpr

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

the class AnnotationFileParser 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.
 *
 * @param annotation syntax tree for an annotation
 * @param allAnnotations map from simple name to annotation definition; side-effected by this
 *     method
 * @return the AnnotationMirror for the annotation, or null if it cannot be built
 */
@Nullable
private AnnotationMirror getAnnotation(AnnotationExpr annotation, Map<String, TypeElement> allAnnotations) {
    // https://tinyurl.com/cfissue/3094
    @SuppressWarnings("signature") @FullyQualifiedName String annoNameFq = annotation.getNameAsString();
    TypeElement annoTypeElt = allAnnotations.get(annoNameFq);
    if (annoTypeElt == null) {
        // If the annotation was not imported, then #getImportedAnnotations did not add it to the
        // allAnnotations field. This code adds the annotation when it is encountered (i.e. here).
        // Note that this does not call AnnotationFileParser#getTypeElement to avoid a spurious
        // diagnostic if the annotation is actually unknown.
        annoTypeElt = elements.getTypeElement(annoNameFq);
        if (annoTypeElt == null) {
            // Not a supported annotation -> ignore
            return null;
        }
        putAllNew(allAnnotations, createNameToAnnotationMap(Collections.singletonList(annoTypeElt)));
    }
    // not anonymous, so name is not empty
    @SuppressWarnings("signature") @CanonicalName String annoName = annoTypeElt.getQualifiedName().toString();
    if (annotation instanceof MarkerAnnotationExpr) {
        return AnnotationBuilder.fromName(elements, annoName);
    } else if (annotation instanceof NormalAnnotationExpr) {
        NormalAnnotationExpr nrmanno = (NormalAnnotationExpr) annotation;
        AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoName);
        List<MemberValuePair> pairs = nrmanno.getPairs();
        if (pairs != null) {
            for (MemberValuePair mvp : pairs) {
                String member = mvp.getNameAsString();
                Expression exp = mvp.getValue();
                try {
                    builderAddElement(builder, member, exp);
                } catch (AnnotationFileParserException e) {
                    warn(exp, "For annotation %s, could not add  %s=%s  because %s", annotation, member, exp, e.getMessage());
                    return null;
                }
            }
        }
        return builder.build();
    } else if (annotation instanceof SingleMemberAnnotationExpr) {
        SingleMemberAnnotationExpr sglanno = (SingleMemberAnnotationExpr) annotation;
        AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoName);
        Expression valExpr = sglanno.getMemberValue();
        try {
            builderAddElement(builder, "value", valExpr);
        } catch (AnnotationFileParserException e) {
            warn(valExpr, "For annotation %s, could not add  value=%s  because %s", annotation, valExpr, e.getMessage());
            return null;
        }
        return builder.build();
    } else {
        throw new BugInCF("AnnotationFileParser: unknown annotation type: " + annotation);
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) FullyQualifiedName(org.checkerframework.checker.signature.qual.FullyQualifiedName) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) BugInCF(org.checkerframework.javacutil.BugInCF) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair) AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder) Expression(com.github.javaparser.ast.expr.Expression) ArrayList(java.util.ArrayList) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) CanonicalName(org.checkerframework.checker.signature.qual.CanonicalName) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 10 with NormalAnnotationExpr

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

the class DoubleJavaParserVisitor method visit.

@Override
public void visit(final NormalAnnotationExpr node1, final Node other) {
    NormalAnnotationExpr node2 = (NormalAnnotationExpr) other;
    defaultAction(node1, node2);
    visitLists(node1.getPairs(), node2.getPairs());
    node1.getName().accept(this, node2.getName());
}
Also used : NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr)

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