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);
}
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);
}
}
Aggregations