use of com.github.javaparser.ast.expr.MarkerAnnotationExpr 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.MarkerAnnotationExpr in project javaparser by javaparser.
the class NodeWithAnnotations method addMarkerAnnotation.
/**
* Annotates this with a marker annotation
*
* @param name the name of the annotation
* @return this
*/
@SuppressWarnings("unchecked")
public default T addMarkerAnnotation(String name) {
MarkerAnnotationExpr markerAnnotationExpr = new MarkerAnnotationExpr(name(name));
getAnnotations().add(markerAnnotationExpr);
markerAnnotationExpr.setParentNode((Node) this);
return (T) this;
}
use of com.github.javaparser.ast.expr.MarkerAnnotationExpr in project javaparser by javaparser.
the class ArrayTypeTest method getParameterWithArrays.
@Test
public void getParameterWithArrays() {
MethodDeclaration methodDeclaration = parseBodyDeclaration("void a(@C int @A[] a @B[]) {}").asMethodDeclaration();
Parameter parameter = methodDeclaration.getParameter(0);
ArrayType outerArrayType = parameter.getType().asArrayType();
ArrayType innerArrayType = outerArrayType.getComponentType().asArrayType();
PrimitiveType elementType = innerArrayType.getComponentType().asPrimitiveType();
assertThat(elementType).isInstanceOf(PrimitiveType.class);
assertThat(outerArrayType.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("A")));
assertThat(innerArrayType.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("B")));
assertThat(parameter.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("C")));
assertThat(parameter.getType().getParentNode().get()).isSameAs(parameter);
}
use of com.github.javaparser.ast.expr.MarkerAnnotationExpr in project javaparser by javaparser.
the class ArrayTypeTest method ellipsisCanHaveAnnotationsToo.
@Test
public void ellipsisCanHaveAnnotationsToo() {
Parameter p = parseParameter("int[]@X...a[]");
assertThat(p.getVarArgsAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("X")));
assertEquals("int[][]@X ... a", p.toString());
assertEquals("int[][]@X... a", ConcreteSyntaxModel.genericPrettyPrint(p));
}
use of com.github.javaparser.ast.expr.MarkerAnnotationExpr in project javaparser by javaparser.
the class ArrayTypeTest method getMethodDeclarationWithArrays.
@Test
public void getMethodDeclarationWithArrays() {
MethodDeclaration methodDeclaration = parseBodyDeclaration("@C int @A[] a() @B[] {}").asMethodDeclaration();
ArrayType arrayType1 = methodDeclaration.getType().asArrayType();
ArrayType arrayType2 = arrayType1.getComponentType().asArrayType();
Type elementType = arrayType2.getComponentType();
assertThat(elementType).isInstanceOf(PrimitiveType.class);
assertThat(arrayType1.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("A")));
assertThat(arrayType2.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("B")));
assertThat(methodDeclaration.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("C")));
assertThat(methodDeclaration.getType().getParentNode().get()).isSameAs(methodDeclaration);
}
Aggregations