Reputation: 1682
I'm using this code to get Java Class annotation:
JavaClass jclas = new ClassParser("src\\test\\org\\poc\\TestClass.class").parse();
ClassGen cg = new ClassGen(jclas);
AnnotationEntryGen[] attributes = cg.getAnnotationEntries();
for (AnnotationEntryGen attribute : attributes) {
System.out.println("attribute: " + attribute);
if(attribute.getTypeName().contains("Fix")) {
// Do something
}
}
But when I print attribute.getTypeName()
I get Lorg/annotations/Fix
. The annotation name is @Fix(..)
Do you know how I can get only the name?
Upvotes: 0
Views: 143
Reputation: 8147
A string like `"Lorg/annotations/Fix" is a field descriptor. Java has many different formats for class names.
You can convert among the different formats for a name using the Signatures
class of the reflection-util package.
The annotation name is
@Fix(..)
.
That terminology is incorrect; you have shown how the full annotation is written in Java source code, but that is not its name. getTypeName()
really did return its name (in field descriptor format).
If you want the annotation's elements (which appear between the parentheses when the annotation is written in Java source code), use getValues()
. You might also find toString()
useful.
Upvotes: 0