如何用ASM扫描字段&方法注解?

问题描述 投票:3回答:2

使用 ASM 在5.0.4版本中,我试图找到一个类的字段和方法的特定注解。 我喜欢避免加载类而不用担心依赖关系。到目前为止,我还不知道如何用这种方法来获取fieldmethod的注释。

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

但没有字段和方法的注释。

我如何才能获得字段和方法的注释?

java java-bytecode-asm
2个回答
5
投票

你将需要子类 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);
    }
}

然后将它们连接到你的 AnnotationScannervisitFieldvisitMethod. 例如,将你的代码改为

@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();
}

0
投票

这个例子 不显示如何访问 name 的价值 @MyMethod 注释 (name="a method"). 也可阅读 a method 值,你必须再加两个 访客:

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);
    }
}

并通过改变重新运行的方式插入它们。MethodAnnotationScannervisitMethod:


@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。

© www.soinside.com 2019 - 2024. All rights reserved.