package hello.asm.my.readAnnotaion;
import java.util.ArrayList;
import java.util.List;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AnnotationNode;
public class ClassVisitorAnnotaion extends ClassVisitor{
private List tableAnnotaionList = new ArrayList();
private List fieldAnnotaionList = new ArrayList();
public ClassVisitorAnnotaion(int api) {
super(api);
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
AnnotationNode node = new AnnotationNode(desc);
tableAnnotaionList.add(node);
return node;
}
@Override
public FieldVisitor visitField(int access, String name, String desc,
String signature, Object value) {
return new FieldVisitor(Opcodes.ASM4) {
@Override
public AnnotationVisitor visitAnnotation(String desc,boolean visible) {
AnnotationNode node = new AnnotationNode(desc);
fieldAnnotaionList.add(node);
return node;
}
};
}
public List getTableAnnotaionList() {
return tableAnnotaionList;
}
public void setTableAnnotaionList(List tableAnnotaionList) {
this.tableAnnotaionList = tableAnnotaionList;
}
public List getFieldAnnotaionList() {
return fieldAnnotaionList;
}
public void setFieldAnnotaionList(List fieldAnnotaionList) {
this.fieldAnnotaionList = fieldAnnotaionList;
}
}
public static void main(String[] args) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, IOException {
String n = User.class.getName();
ClassReader cr = new ClassReader(n);
ClassVisitorAnnotaion annotaion = new ClassVisitorAnnotaion(ASM4);
cr.accept(annotaion, ClassReader.SKIP_CODE);
List tableANodes = annotaion.getTableAnnotaionList();
List fieldAnodes = annotaion.getFieldAnnotaionList();
for (AnnotationNode tANode : tableANodes) {
System.out.println(tANode.values.get(0) + " : " + tANode.values.get(1));
}
for (AnnotationNode fANode : fieldAnodes) {
System.out.println(fANode.values.get(0) + " : " + fANode.values.get(1));
}
}