How to supply value to an annotation from a Constant java

后端 未结 6 658
日久生厌
日久生厌 2020-11-22 15:01

I am thinking this may not be possible in Java because annotation and its parameters are resolved at compile time. I have an interface as follows,

public int         


        
6条回答
  •  我在风中等你
    2020-11-22 16:05

    You're not supplying it with an array in your example. The following compiles fine:

    public @interface SampleAnnotation {
        String[] sampleValues();
    }
    
    public class Values {
        public static final String val0 = "A";
        public static final String val1 = "B";
    
        @SampleAnnotation(sampleValues={ val0, val1 })
        public void foo() {
        }
    }
    

提交回复
热议问题