views:

459

answers:

4

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?

+2  A: 

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.

Kevin
+1  A: 

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

Andrzej Doyle
Tough Luck for me ! Oh yah I was able to pass Strings/Numbers but not arrays. I will spend a bit more time on this and if nothing works out will accept the answer :)
Calm Storm
Yeah, my guess would be that the mutability of the FIELD1 array is the issue here. You're allowed to declare the array with an array initializer because nothing else can have access to that array and so it cannot be changed later.
ColinD
+1  A: 

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 v1 = "A";
        public static final String v2 = "B";

        @SampleAnnotation(sampleValues = { v1, v2 })
        public void foo() {
        }
    }
Steve B.
It is being supplied with an array in the example, just not one that is created directly in the annotation declaration.
ColinD
+4  A: 

Compile constants can only be primitives and Strings:

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#5313

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.

irreputable
+1 a good, concise explanation
skaffman
This looks like the explanation that I was after :) Thanks
Calm Storm