Search in sources :

Example 11 with MarkerAnnotationExpr

use of com.github.javaparser.ast.expr.MarkerAnnotationExpr 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 MarkerAnnotationExpr

use of com.github.javaparser.ast.expr.MarkerAnnotationExpr 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)

Aggregations

MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)12 Test (org.junit.Test)6 SingleMemberAnnotationExpr (com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)5 MemberValuePair (com.github.javaparser.ast.expr.MemberValuePair)4 NormalAnnotationExpr (com.github.javaparser.ast.expr.NormalAnnotationExpr)4 NodeList (com.github.javaparser.ast.NodeList)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 Parameter (com.github.javaparser.ast.body.Parameter)2 Expression (com.github.javaparser.ast.expr.Expression)2 Name (com.github.javaparser.ast.expr.Name)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AnnotationBuilder (org.checkerframework.javacutil.AnnotationBuilder)2 JavaParser.parseClassOrInterfaceType (com.github.javaparser.JavaParser.parseClassOrInterfaceType)1 JavaParser.parseName (com.github.javaparser.JavaParser.parseName)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 IntegerLiteralExpr (com.github.javaparser.ast.expr.IntegerLiteralExpr)1 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)1 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)1