I would like to use constants for annotation values.
interface Client { @Retention(RUNTIME) @Target(METHOD) @interface SomeAnnotation { String[] values(); } interface Info { String A = "a"; String B = "b"; String[] AB = new String[] { A, B }; } @SomeAnnotation(values = { Info.A, Info.B }) void works(); @SomeAnnotation(values = Info.AB) void doesNotWork(); }
The constants Info.A
and Info.B
can be used in the annotation but not the array Info.AB
as it has to be an array initializer in this place. Annotation values are restricted to values that could be inlined into the byte code of a class. This is not possible for the array constant as it has to be constructed when Info
is loaded. Is there a workaround for this problem?
No, there is no workaround.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With