use of com.github.javaparser.ast.expr.NormalAnnotationExpr in project javaparser by javaparser.
the class AnnotationMemberDeclarationTransformationsTest method replacingAnnotation.
@Test
public void replacingAnnotation() {
AnnotationMemberDeclaration it = consider("@myAnno int foo();");
it.getAnnotations().set(0, new NormalAnnotationExpr(new Name("myOtherAnno"), new NodeList<>()));
assertTransformedToString("@myOtherAnno() int foo();", it);
}
use of com.github.javaparser.ast.expr.NormalAnnotationExpr 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.NormalAnnotationExpr in project javaparser by javaparser.
the class NodeWithAnnotationsBuildersTest method testAddAnnotation.
@Test
public void testAddAnnotation() {
NormalAnnotationExpr annotation = testClass.addAndGetAnnotation(hey.class);
assertEquals("import com.github.javaparser.builders.NodeWithAnnotationsBuildersTest.hey;", cu.getImport(0).toString().trim());
assertEquals(1, testClass.getAnnotations().size());
assertEquals(annotation, testClass.getAnnotation(0));
assertEquals(NormalAnnotationExpr.class, testClass.getAnnotation(0).getClass());
}
use of com.github.javaparser.ast.expr.NormalAnnotationExpr in project checker-framework by typetools.
the class AnnotationFileParser 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.
*
* @param annotation syntax tree for an annotation
* @param allAnnotations map from simple name to annotation definition; side-effected by this
* method
* @return the AnnotationMirror for the annotation, or null if it cannot be built
*/
@Nullable
private AnnotationMirror getAnnotation(AnnotationExpr annotation, Map<String, TypeElement> allAnnotations) {
// https://tinyurl.com/cfissue/3094
@SuppressWarnings("signature") @FullyQualifiedName String annoNameFq = annotation.getNameAsString();
TypeElement annoTypeElt = allAnnotations.get(annoNameFq);
if (annoTypeElt == null) {
// If the annotation was not imported, then #getImportedAnnotations did not add it to the
// allAnnotations field. This code adds the annotation when it is encountered (i.e. here).
// Note that this does not call AnnotationFileParser#getTypeElement to avoid a spurious
// diagnostic if the annotation is actually unknown.
annoTypeElt = elements.getTypeElement(annoNameFq);
if (annoTypeElt == null) {
// Not a supported annotation -> ignore
return null;
}
putAllNew(allAnnotations, createNameToAnnotationMap(Collections.singletonList(annoTypeElt)));
}
// not anonymous, so name is not empty
@SuppressWarnings("signature") @CanonicalName String annoName = annoTypeElt.getQualifiedName().toString();
if (annotation instanceof MarkerAnnotationExpr) {
return AnnotationBuilder.fromName(elements, annoName);
} else if (annotation instanceof NormalAnnotationExpr) {
NormalAnnotationExpr nrmanno = (NormalAnnotationExpr) annotation;
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoName);
List<MemberValuePair> pairs = nrmanno.getPairs();
if (pairs != null) {
for (MemberValuePair mvp : pairs) {
String member = mvp.getNameAsString();
Expression exp = mvp.getValue();
try {
builderAddElement(builder, member, exp);
} catch (AnnotationFileParserException e) {
warn(exp, "For annotation %s, could not add %s=%s because %s", annotation, member, exp, e.getMessage());
return null;
}
}
}
return builder.build();
} else if (annotation instanceof SingleMemberAnnotationExpr) {
SingleMemberAnnotationExpr sglanno = (SingleMemberAnnotationExpr) annotation;
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoName);
Expression valExpr = sglanno.getMemberValue();
try {
builderAddElement(builder, "value", valExpr);
} catch (AnnotationFileParserException e) {
warn(valExpr, "For annotation %s, could not add value=%s because %s", annotation, valExpr, e.getMessage());
return null;
}
return builder.build();
} else {
throw new BugInCF("AnnotationFileParser: unknown annotation type: " + annotation);
}
}
use of com.github.javaparser.ast.expr.NormalAnnotationExpr in project checker-framework by typetools.
the class DoubleJavaParserVisitor method visit.
@Override
public void visit(final NormalAnnotationExpr node1, final Node other) {
NormalAnnotationExpr node2 = (NormalAnnotationExpr) other;
defaultAction(node1, node2);
visitLists(node1.getPairs(), node2.getPairs());
node1.getName().accept(this, node2.getName());
}
Aggregations