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