如何使用ASM扫描字段和方法注释?

时间:2015-12-24 19:08:46

标签: java java-bytecode-asm

使用ASM 5.0.4,我正在尝试查找具有特定注释的类'字段和方法。我喜欢避免加载类而不用担心依赖项。 到目前为止,我无法弄清楚如何使用这种方法获得字段/方法注释:

class AnnotationScanner extends ClassVisitor{

    public AnnotationVisitor visitAnnotation(String desc, boolean visible){
        System.out.println("visitAnnotation: desc="+desc+" visible="+visible);
        return super.visitAnnotation(desc,visible);
    }

    public void visitAttribute(Attribute attr){
        System.out.println("visitAttribute: attr="+attr);
        super.visitAttribute(attr);
    }

    public FieldVisitor visitField(int access, String name, String desc, String signature, Object value){
        System.out.println("visitField: access="+access+" name="+name+" desc="+desc+" signature="+signature+" value="+value);
        return super.visitField(access,name,desc,signature,value);
    }

    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions){
        System.out.println("visitMethod: access="+access+" name="+name+" desc="+desc+" signature="+signature+" exceptions="+exceptions);
        return super.visitMethod(access,name,desc,signature,exceptions);
    }

    public static void main(String[] args) throws Exception{
        for (String arg : args){
            FileInputStream in = new FileInputStream(new File(arg));
            ClassReader cr = new ClassReader(in);
            cr.accept(new AnnotationScanner(Opcodes.ASM4),0);
        }
    }
}

整个示例项目位于GitHub

针对一个简单的类

运行它
@MyClass(name="annotation scanner")
public class Scannee{
    @MyField(name="a string field") public String aStringField;
    @MyMethod(name="a method")      public void aMethod(){}
}

给出

visitAnnotation: desc=Lorg/springdot/sandbox/asm/MyClass; visible=true
visitField: access=1 name=aStringField desc=Ljava/lang/String; signature=null value=null
visitMethod: access=1 name=<init> desc=()V signature=null exceptions=null
visitMethod: access=1 name=aMethod desc=()V signature=null exceptions=null

但不是该字段和方法的注释。

我怎样才能获得字段和方法注释?

2 个答案:

答案 0 :(得分:3)

您需要继承FieldVisitorMethodVisitor,并覆盖方法visitAnnotation,类似于您的ClassVisitor子类。例如,

class FieldAnnotationScanner extends FieldVisitor {
    public FieldAnnotationScanner() {
        super(Opcodes.ASM5);
    }

    @Override
    public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
        System.out.println("visitAnnotation: desc="+desc+" visible="+visible);
        return super.visitAnnotation(desc, visible);
    }
}

class MethodAnnotationScanner extends MethodVisitor {
    public MethodAnnotationScanner() {
        super(Opcodes.ASM5);
    }

    @Override
    public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
        System.out.println("visitAnnotation: desc="+desc+" visible="+visible);
        return super.visitAnnotation(desc, visible);
    }
}

然后将它们与AnnotationScannervisitField中的visitMethod相关联。例如,将代码更改为

@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value){
    System.out.println("visitField: access="+access+" name="+name+" desc="+desc+" signature="+signature+" value="+value);
    return new FieldAnnotationScanner();
}

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions){
    System.out.println("visitMethod: access="+access+" name="+name+" desc="+desc+" signature="+signature+" exceptions="+exceptions);
    return new MethodAnnotationScanner();
}

答案 1 :(得分:0)

The example没有显示如何访问name批注(@MyMethod)的name="a method"值。要同时读取a method值,您必须再添加两个 Visitors

static class MethodAnnotationScanner extends MethodVisitor {
        MethodAnnotationScanner() { 
          super(Opcodes.ASM8); 
    }

    @Override
    public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
        System.out.println("visitMethodAnnotation: type= " + desc);
        return new MyAnnotationVisitor();
    }
}
static class MethodAnnotationValueScanner extends AnnotationVisitor {
    MethodAnnotationValueScanner() { 
        super(Opcodes.ASM8); 
    }

    @Override
    public void visit(String name, Object value) {
        System.out.println("visitMethodAnnotationValue: " + name + " = " + value);
        super.visit(name, value);
    }
}

并通过更改将MethodAnnotationScanner转换为visitMethod来插入它们:


@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    System.out.println("visitMethod: access="+access+" name="+name+" desc="+desc+" signature="+signature+" exceptions="+exceptions);
    return new MethodAnnotationScanner();
}

您可以在此处查看完整的更新示例:how-to-read-a-java-class-method-annotation-value-with-asm