How to supply value to an annotation from a Constant java

后端 未结 6 653
日久生厌
日久生厌 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 15:45

    Compile constants can only be primitives and Strings:

    15.28. Constant Expressions

    A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

    • Literals of primitive type and literals of type String
    • Casts to primitive types and casts to type String
    • [...] operators [...]
    • Parenthesized expressions whose contained expression is a constant expression.
    • Simple names that refer to constant variables.
    • Qualified names of the form TypeName . Identifier that refer to constant variables.

    Actually in java there is no way to protect items in an array. At runtime someone can always do FieldValues.FIELD1[0]="value3", therefore the array cannot be really constant if we look deeper.

提交回复
热议问题