Search in sources :

Example 11 with NormalAnnotationExpr

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

the class AnnotationMirrorToAnnotationExprConversion method annotationMirrorToAnnotationExpr.

/**
 * Converts an AnnotationMirror into a JavaParser {@code AnnotationExpr}.
 *
 * @param annotation the annotation to convert
 * @return a JavaParser {@code AnnotationExpr} representing the same annotation with the same
 *     element values. The converted annotation will contain the annotation's fully qualified
 *     name.
 */
public static AnnotationExpr annotationMirrorToAnnotationExpr(AnnotationMirror annotation) {
    Map<? extends ExecutableElement, ? extends AnnotationValue> values = annotation.getElementValues();
    Name name = createQualifiedName(AnnotationUtils.annotationName(annotation));
    if (values.isEmpty()) {
        return new MarkerAnnotationExpr(name);
    }
    NodeList<MemberValuePair> convertedValues = convertAnnotationValues(values);
    if (convertedValues.size() == 1 && convertedValues.get(0).getName().asString().equals("value")) {
        return new SingleMemberAnnotationExpr(name, convertedValues.get(0).getValue());
    }
    return new NormalAnnotationExpr(name, convertedValues);
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) Name(com.github.javaparser.ast.expr.Name)

Example 12 with NormalAnnotationExpr

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

the class GeneratedClassDeclaration method addAnnotationToMethodDeclaration.

private void addAnnotationToMethodDeclaration(MethodDeclaration setter, AnnotationDefinition a) {
    NormalAnnotationExpr annotation = new NormalAnnotationExpr();
    annotation.setName(a.getName());
    a.getValueMap().forEach(annotation::addPair);
    setter.addAnnotation(annotation);
}
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