Search in sources :

Example 1 with MarkerAnnotationExpr

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;
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) AnnotationMirror(javax.lang.model.element.AnnotationMirror) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair) AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder) Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) ArrayList(java.util.ArrayList) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr)

Example 2 with MarkerAnnotationExpr

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;
}
Also used : MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr)

Example 3 with MarkerAnnotationExpr

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);
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Parameter(com.github.javaparser.ast.body.Parameter) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) Test(org.junit.Test)

Example 4 with MarkerAnnotationExpr

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));
}
Also used : Parameter(com.github.javaparser.ast.body.Parameter) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) Test(org.junit.Test)

Example 5 with MarkerAnnotationExpr

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);
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) Test(org.junit.Test)

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