Search in sources :

Example 1 with SingleMemberAnnotationExpr

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

use of com.github.javaparser.ast.expr.SingleMemberAnnotationExpr in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitAnnotation.

@Override
public Void visitAnnotation(AnnotationTree javacTree, Node javaParserNode) {
    // as @MyAnno(value="myArg") which has a single element argument list with an assignment.
    if (javaParserNode instanceof MarkerAnnotationExpr) {
        processAnnotation(javacTree, (MarkerAnnotationExpr) javaParserNode);
    } else if (javaParserNode instanceof SingleMemberAnnotationExpr) {
        SingleMemberAnnotationExpr node = (SingleMemberAnnotationExpr) javaParserNode;
        processAnnotation(javacTree, node);
        assert javacTree.getArguments().size() == 1;
        ExpressionTree value = javacTree.getArguments().get(0);
        assert value instanceof AssignmentTree;
        AssignmentTree assignment = (AssignmentTree) value;
        assert assignment.getVariable().getKind() == Tree.Kind.IDENTIFIER;
        assert ((IdentifierTree) assignment.getVariable()).getName().contentEquals("value");
        assignment.getExpression().accept(this, node.getMemberValue());
    } else if (javaParserNode instanceof NormalAnnotationExpr) {
        NormalAnnotationExpr node = (NormalAnnotationExpr) javaParserNode;
        processAnnotation(javacTree, node);
        assert javacTree.getArguments().size() == node.getPairs().size();
        Iterator<MemberValuePair> argIter = node.getPairs().iterator();
        for (ExpressionTree arg : javacTree.getArguments()) {
            assert arg instanceof AssignmentTree;
            AssignmentTree assignment = (AssignmentTree) arg;
            IdentifierTree memberName = (IdentifierTree) assignment.getVariable();
            MemberValuePair javaParserArg = argIter.next();
            assert memberName.getName().contentEquals(javaParserArg.getNameAsString());
            assignment.getExpression().accept(this, javaParserArg.getValue());
        }
    } else {
        throwUnexpectedNodeType(javacTree, javaParserNode);
    }
    return null;
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) IdentifierTree(com.sun.source.tree.IdentifierTree) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AssignmentTree(com.sun.source.tree.AssignmentTree) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr)

Example 3 with SingleMemberAnnotationExpr

use of com.github.javaparser.ast.expr.SingleMemberAnnotationExpr in project checker-framework by typetools.

the class DoubleJavaParserVisitor method visit.

@Override
public void visit(final SingleMemberAnnotationExpr node1, final Node other) {
    SingleMemberAnnotationExpr node2 = (SingleMemberAnnotationExpr) other;
    defaultAction(node1, node2);
    node1.getMemberValue().accept(this, node2.getMemberValue());
    node1.getName().accept(this, node2.getName());
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)

Example 4 with SingleMemberAnnotationExpr

use of com.github.javaparser.ast.expr.SingleMemberAnnotationExpr in project activityinfo by bedatadriven.

the class DefaultUpdatingVisitor method updateDefaultStringAnnotation.

private void updateDefaultStringAnnotation(MethodDeclaration decl, String translated) {
    for (AnnotationExpr annotation : decl.getAnnotations()) {
        if (annotation.getName().getName().equals(defaultAnnotationName)) {
            updateAnnotationValue(annotation, translated);
            return;
        }
    }
    // If there is no annotation, add one
    SingleMemberAnnotationExpr defaultExpr = new SingleMemberAnnotationExpr(new NameExpr(defaultAnnotationName), new StringLiteralExpr(translated));
    decl.getAnnotations().add(defaultExpr);
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr)

Example 5 with SingleMemberAnnotationExpr

use of com.github.javaparser.ast.expr.SingleMemberAnnotationExpr in project activityinfo by bedatadriven.

the class DefaultUpdatingVisitor method updateAnnotationValue.

private void updateAnnotationValue(AnnotationExpr annotation, String literalValue) {
    List<Node> children = annotation.getChildrenNodes();
    if (children.size() == 2 && children.get(0) instanceof NameExpr && children.get(1) instanceof StringLiteralExpr) {
        StringLiteralExpr literal = (StringLiteralExpr) children.get(1);
        if (!literal.getValue().equals(literalValue)) {
            literal.setValue(literalValue);
            dirty = true;
        }
    } else if (annotation instanceof SingleMemberAnnotationExpr) {
        // Annotations can contain more complex ASTs, for example "part of a string" + "part of another string"
        // In this case, replace wholesale
        SingleMemberAnnotationExpr smae = (SingleMemberAnnotationExpr) annotation;
        smae.setMemberValue(new StringLiteralExpr(literalValue));
    } else {
        throw new RuntimeException("Expected @" + defaultAnnotationName + " to be of type " + SingleMemberAnnotationExpr.class.getName() + ", found:  " + annotation.getClass().getName());
    }
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) Node(com.github.javaparser.ast.Node) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr)

Aggregations

SingleMemberAnnotationExpr (com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)9 MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)5 MemberValuePair (com.github.javaparser.ast.expr.MemberValuePair)4 NormalAnnotationExpr (com.github.javaparser.ast.expr.NormalAnnotationExpr)4 NodeList (com.github.javaparser.ast.NodeList)2 Expression (com.github.javaparser.ast.expr.Expression)2 Name (com.github.javaparser.ast.expr.Name)2 NameExpr (com.github.javaparser.ast.expr.NameExpr)2 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)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 Node (com.github.javaparser.ast.Node)1 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)1 IntegerLiteralExpr (com.github.javaparser.ast.expr.IntegerLiteralExpr)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 AssignmentTree (com.sun.source.tree.AssignmentTree)1