Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Copy over an annotation to generated code
Copy over an annotation to generated code [message #734198] Fri, 07 October 2011 01:54 Go to next message
Abbas  is currently offline Abbas Friend
Messages: 2
Registered: October 2011
Junior Member
Hi,
I am writing an AnnotationProcessor, which has a requirement to copy over an annotation from source being processed to the generated code.

For instance, lets say I have the following input:
@Foo
public interface MyType {
@Bar(name="test")
void hello();
}

I can get the com.sun.mirror.declaration.AnnotationMirror object for "@Bar", but finding it hard to get valid Java from AST representation. Is there any utility or API which can give well-formed Java from AnnotationMirror object? I already tried toString but that does not give correct Java syntax.

Thanks.
Re: Copy over an annotation to generated code [message #734448 is a reply to message #734198] Fri, 07 October 2011 20:08 Go to previous message
Abbas  is currently offline Abbas Friend
Messages: 2
Registered: October 2011
Junior Member
Here is what I ended up doing to implement this:

private void writeAnnotation(AnnotationMirror annotation) {
AnnotationTypeDeclaration annotationDeclaration = annotation.getAnnotationType().getDeclaration();
writer.print("@" + annotationDeclaration.getQualifiedName() + "(" );

Map<AnnotationTypeElementDeclaration, AnnotationValue> elementValues = annotation.getElementValues();
boolean isFirst = true;
for (Map.Entry<AnnotationTypeElementDeclaration, AnnotationValue> entry : elementValues.entrySet()) {
AnnotationTypeElementDeclaration elementKey = entry.getKey();
AnnotationValue elementValue = entry.getValue();

if (isFirst) {
isFirst = false;
} else {
writer.print(", ");
}
writer.print(elementKey.getSimpleName() + "=");
writeAnnotationValue(elementValue);
writer.println();
}
writer.println(")");

}

private void writeAnnotationValue(AnnotationValue annotationValue) {
Object value = annotationValue.getValue();
if (value instanceof String) {
String val = (String)value;
writer.print("\"");
writer.print(val);
writer.print("\"");
} else if (value instanceof AnnotationMirror) {
writeAnnotation((AnnotationMirror)value);
} else if (value instanceof TypeMirror) {

} else if (value instanceof EnumConstantDeclaration) {
EnumConstantDeclaration val = (EnumConstantDeclaration) value;
EnumDeclaration enumDecl = val.getDeclaringType();

String enumTypeName = enumDecl.getQualifiedName();
String simpleName = val.getSimpleName();

writer.print(enumTypeName + "." + simpleName);
} else if (value instanceof Collection) {
writer.print("{ ");
boolean isFirst = true;
for (AnnotationValue val: (Collection<AnnotationValue>)value) {
if (isFirst) {
isFirst = false;
} else {
writer.print(", ");
}
writeAnnotationValue(val);
}
writer.print(" }");
} else {
writer.print(value);
}
}
Previous Topic:Solving Jar Dependencies
Next Topic:Code works in BlueJ but not in Eclipse
Goto Forum:
  


Current Time: Sat Apr 27 00:22:10 GMT 2024

Powered by FUDForum. Page generated in 0.02383 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top