How to supply value to an annotation from a Constant java

后端 未结 6 596
日久生厌
日久生厌 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.

    0 讨论(0)
  • 2020-11-22 15:45

    You can use enum and refer that enum in annotation field

    0 讨论(0)
  • 2020-11-22 15:46

    I am thinking this may not be possible in Java because annotation and its parameters are resolved at compile time.

    With Seam 2 http://seamframework.org/ you were able to resolve annotation parameters at runtime, with expression language inside double quotes.

    In Seam 3 http://seamframework.org/Seam3/Solder, this feature is the module Seam Solder

    0 讨论(0)
  • 2020-11-22 15:52

    You can use a constant (i.e. a static, final variable) as the parameter for an annotation. As a quick example, I use something like this fairly often:

    import org.junit.Test;
    import static org.junit.Assert.*;
    
    public class MyTestClass
    {
        private static final int TEST_TIMEOUT = 60000; // one minute per test
    
        @Test(timeout=TEST_TIMEOUT)
        public void testJDK()
        {
            assertTrue("Something is very wrong", Boolean.TRUE);
        }
    }
    

    Note that it's possible to pass the TEST_TIMEOUT constant straight into the annotation.

    Offhand, I don't recall ever having tried this with an array, so you may be running into some issues with slight differences in how arrays are represented as annotation parameters compared to Java variables? But as for the other part of your question, you could definitely use a constant String without any problems.

    EDIT: I've just tried this with a String array, and didn't run into the problem you mentioned - however the compiler did tell me that the "attribute value must be constant" despite the array being defined as public static final String[]. Perhaps it doesn't like the fact that arrays are mutable? Hmm...

    0 讨论(0)
  • 2020-11-22 16:00

    Does someone know how I can use a String constant or String[] constant to supply value to an annotation?

    Unfortunately, you can't do this with arrays. With non-array variables, the value must be final static.

    0 讨论(0)
  • 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() {
        }
    }
    
    0 讨论(0)
提交回复
热议问题