我是ASM的新手。我有一个类文件,其中我有方法的运行时可见注释。我想解析这个类文件,并根据特定条件选择注释。我查看了ASM的文档,并尝试使用visibleAnnotation。我似乎无法打印我在类文件中可以看到的方法注释列表。
我的代码是
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.ClassReader;
public class ByteCodeParser {
public static void main(String[] args) throws Exception{
InputStream in=new FileInputStream("sample.class");
ClassReader cr=new ClassReader(in);
ClassNode classNode=new ClassNode();
//ClassNode is a ClassVisitor
cr.accept(classNode, 0);
//
Iterator<MethodNode> i = classNode.methods.iterator();
while(i.hasNext()){
MethodNode mn = i.next();
System.out.println(mn.name+ "" + mn.desc);
System.out.println(mn.visibleAnnotations);
}
}
}
输出结果为:
<clinit>()V
null
<init>()V
null
MyRandomFunction1()V
[org.objectweb.asm.tree.AnnotationNode@5674cd4d]
MyRandomFunction2()V
[org.objectweb.asm.tree.AnnotationNode@63961c42]
我的RandomFunction 1&amp; 2有注释,但我似乎无法理解[org.objectweb.asm.tree.AnnotationNode@5674cd4d]。
答案 0 :(得分:0)
我自己解决了这个问题,我不得不重复我最初没有意识到的注释。
if (mn.visibleAnnotations != null) {
Iterator<AnnotationNode>j=mn.visibleAnnotations.iterator();
while (j.hasNext()) {
AnnotationNode an=j.next();
System.out.println(an.values);
}
}