使用一个注释方法作为其他注释的默认值

时间:2013-02-01 13:27:19

标签: java annotations

public @interface MyAnnotation{

    public String someProperty();

    //How should I achieve this?
    public String someOtherProperty() default someProperty();


}

我在注释中有两个属性,当没有指定一个属性时,我想将另一个用作默认值。有没有办法做到这一点?

或者我必须做以下检查

if(myAnnotation.someOtherProperty() == null){
    //Use the value of someProperty
}

1 个答案:

答案 0 :(得分:4)

您当前的场景根本不可能 - 注释属性的默认值必须是静态可解析的。现在,您正尝试将默认值定义为在实际使用注释(也称为动态)之前不会设置的属性。

您可以做的是定义您的注释:

public @interface MyAnnotation{

    public String someProperty();

    public String someOtherProperty() default "";
}

然后在您的注释处理器中,如果someProperty为空,请使用someOtherProperty的{​​{1}}值。

相关问题