Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an array constant in an annotation

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?

like image 734
Thomas Jung Avatar asked Sep 07 '09 07:09

Thomas Jung


1 Answers

No, there is no workaround.

like image 118
Thomas Jung Avatar answered Sep 19 '22 21:09

Thomas Jung