反映我自己班级实例的注释

时间:2016-10-07 13:34:42

标签: java reflection annotations runtime java-annotations

我有几个类A1,A2,A3,它们扩展了抽象类myA。这些类具有类B的x字段。类B的字段使用注释Test进行注释。(测试在运行时可用)如何从类B的方法中获取注释Test及其值。

public class A1 extends myA{

    @Test("all")
    private B b1;

    @Test("none")
    private B b2;

    @Test("none")
    private B b3;

    //....

    public void interact(){
        b2.doSomethingBasedOnMyAnnotation();
    }

}
public class A2 extends myA{

    @Test("none")
    private B b;

    //....
}

public class B{

     public void doSomethingBasedOnMyAnnotation(){
        // How to reach the Annotation from here ?
     }

}

@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.FIELD)
public @interface Test{
    String value() default "all";
}

1 个答案:

答案 0 :(得分:0)

当您对变量放置注释时,它将成为该变量的静态属性,而不是您可以通过该变量访问的对象。考虑:

public class A1 extends myA{

    @Test("all")
    private B b1=new B();

    @Test("none")
    private B b2=b1;

    //....

    public void interact(){
        // this
        b2.doSomethingBasedOnMyAnnotation();
        // is exactly the same as
        b1.doSomethingBasedOnMyAnnotation();
    }
}

假设涉及带注释的变量甚至无效。那怎么样? new B().doSomethingBasedOnMyAnnotation()

由于字段是在编译时解析的,因此无论如何都不会在所需的操作中进行抽象。如果您知道要调用b2.doSomethingBasedOnMyAnnotation();,那么您已经知道正在使用哪个字段,并且将b2的注释值作为参数提供给调用的方法没有问题,而不是期望接收器神奇地发现。 E.g。

public class B{
    public void doSomething(String arg){
    }
}
public class A1 extends myA{

    @Test("all")
    private B b1;

    @Test("none")
    private B b2;

    //....

    public void interact(){
        b1.doSomething(get("b1"));
        b2.doSomething(get("b2"));
    }
    static String get(String fieldName) {
        try {
            return A1.class.getDeclaredField(fieldName)
                .getAnnotation(Test.class).value();
        } catch(NoSuchFieldException ex) {
            throw new IllegalStateException(ex);
        }
    }
}

虽然我们可以在没有反思的情况下愉快地工作:

public class A1 extends myA{

    static final String TEST_B1="all";

    @Test(TEST_B1)
    private B b1;

    static final String TEST_B2="none";

    @Test(TEST_B2)
    private B b2;

    static final String TEST_B3="none";

    @Test(TEST_B3)
    private B b3;

    //....

    public void interact(){
        b1.doSomething(TEST_B1);
        b2.doSomething(TEST_B2);
    }
}

如果您想确保调用者无法意外传递错误的参数,请改用封装:

public final class EncapsulatedB {
    final String testValue;
    B b;
    EncapsulatedB(String value) {
        this(value, null);
    }
    EncapsulatedB(String value, B initialB) {
        testValue=value;
        b=initialB;
    }
    public B getB() {
        return b;
    }
    public void setB(B b) {
        this.b = b;
    }
    public void doSomething() {
        b.doSomething(testValue);
    }
}

public class A1 extends myA{
    private final EncapsulatedB b1=new EncapsulatedB("all");
    private final EncapsulatedB b2=new EncapsulatedB("none");
    private final EncapsulatedB b3=new EncapsulatedB("none");

    //....

    public void interact(){
        b1.doSomething();
        b2.doSomething();
    }
}