Use one annotation method as default for others

public @interface MyAnnotation{

    public String someProperty();

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


}

      

I have two properties in the annotation, and when one property is not specified, I want to use a different default. Is there a way to do this?

Or should I do the following check

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

      

+3


source to share


1 answer


Your current scenario is simply not possible - the default value of the annotation attribute must be statically resolvable. Right now you are trying to define a default as a property that will not be set until the annotation is used (aka, dynamically).

What you can do is define your annotation as such:



public @interface MyAnnotation{

    public String someProperty();

    public String someOtherProperty() default "";
}

      

Then, in your annotation processor, use the value someProperty

for someOtherProperty

if someOtherProperty

empty.

+4


source







All Articles