How to supply value to an annotation from a Constant java

JavaArraysAnnotations

Java Problem Overview


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 interface FieldValues {
   String[] FIELD1 = new String[]{"value1", "value2"};
}

and another class as,

@SomeAnnotation(locations = {"value1", "value2"})
public class MyClass {
   ....
}

I mark many classes with the annotation and I would like to know if I can avoid specifying the strings in every annotation I would instead prefer to use

@SomeAnnotation(locations = FieldValues.FIELD1)
public class MyClass {
   ....
}

However this gives compilation errors like annotation value should be an array initializer etc. Does someone know how I can use a String constant or String[] constant to supply value to an annotation?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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...

Solution 3 - Java

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() {
    }
}

Solution 4 - Java

> 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.

Solution 5 - Java

>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

Solution 6 - Java

You can use enum and refer that enum in annotation field

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKannan EkanathView Question on Stackoverflow
Solution 1 - JavairreputableView Answer on Stackoverflow
Solution 2 - JavaAndrzej DoyleView Answer on Stackoverflow
Solution 3 - JavaSteve B.View Answer on Stackoverflow
Solution 4 - JavaKevinView Answer on Stackoverflow
Solution 5 - JavajorgwelView Answer on Stackoverflow
Solution 6 - JavaKaustubh ThawariView Answer on Stackoverflow