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