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

Java 使用一种注释方法作为其他注释方法的默认值,java,annotations,Java,Annotations,注释中有两个属性,如果未指定一个属性,则希望使用另一个作为默认值。有没有办法做到这一点 或者我必须做以下检查 public @interface MyAnnotation{ public String someProperty(); //How should I achieve this? public String someOtherProperty() default someProperty(); } 您当前的场景是不可能的-注释属性的默认值必须是静态可解

注释中有两个属性,如果未指定一个属性,则希望使用另一个作为默认值。有没有办法做到这一点

或者我必须做以下检查

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
}

然后在注释处理器中,如果
someOtherProperty
为空,则为
someOtherProperty
使用
someProperty
的值。

@XaviLópez它不重复,因为此问题询问如何使用一个属性作为其他属性的默认值。您提供的链接讨论了如何使用默认值。来到这里,希望了解如何将注释与方法一起使用。提供的链接不包含该信息。
public @interface MyAnnotation{

    public String someProperty();

    public String someOtherProperty() default "";
}