Reputation: 7502
I created an annotation like below
@Retention(RetentionPolicy.RUNTIME)
public @interface TestData
{
String[] values();
}
I use it in my class like below
@TestData({"test1","test2","test3"})
public void testMethod(String data) {
//Some code here
}
What I'd like to do is.. For the TestData annotation I'd like to dynamically generate a few values and pass it on. For Eg:
Let's say I have a method called getData();
public string[] getData() {
//Code to return an array
return array[];
}
Now I want to be able to pass the values of getData() method to my @TestData annotation. Is that possible?
Upvotes: 2
Views: 291
Reputation: 206816
No, that is not possible, the values that are on the annotation have to be known at compile time, so you cannot use the result of a method, because that won't be known until you run the program.
This is explained in JLS 9.7.1 (the description of the ElementValue, V). This paragraph says:
String
it must be a constant expression (i.e. a compile-time constant)Class
it must be a class literalThe return value of a method is not a compile-time constant.
Upvotes: 1