I'm trying to read class annotations using this code:
JavaClass jclas = new ClassParser("src\\test\\org\\poc\\TargetHello.class").parse();
ClassGen cg = new ClassGen(jclas);
Attribute[] attributes = cg.getAttributes();
for (Attribute attribute : attributes) {
if (attribute instanceof Annotations) {
Annotations annotations = (Annotations) attribute;
AnnotationEntry[] entries= annotations.getAnnotationEntries();
}
}
But for this code attribute instanceof Annotations I get error: Inconvertible types; cannot cast 'com.sun.org.apache.bcel.internal.classfile.Attribute' to 'org.apache.bcel.classfile.Annotations'
Do you know how I can solve this issue?
This works for me. You didn't give a complete compilable example nor say what commands you ran. Here is what I did.
File
Hello.java:File
AttributeAnnotations.java:Commands to run:
All the commands complete without errors.