Reputation: 3826
I have an annotation in my java project which has some default strings in it:
public @interface MyInterface {
String message() default "Dependency for field; must be set here";
// ...
}
How can I do internationalization here? In my classes I would load the string via a ResourceManager
public class ValidationDocument {
private String message = ResourceManager.findLiteral("ValidationDocument", "default.message");
// ...
}
I can't load the ResourceManager in the annotation definition. What would be a good way to do the internationalization here?
Upvotes: 2
Views: 72
Reputation: 15029
You're right, you cannot do it, because annotations are evaluated at compile time and thus you can only use constants, or expressions that only involve constants. Information that can only by available at run-time, such as one retrieved by calling methods, even static ones, therefore cannot be assigned in annotations.
Annotations are not designed to be dynamically modified at run-time, so you will need to change your approach.
I could only suggest to do something like:
public @interface MyInterface {
String messageKey() default "myinterface.mykey";
// ...
}
Then, your code that actually references the @MyInterface
annotation instance, would use the messageKey
to look-up the message in the ResourceManager
. Might work depending on what you're trying to achieve with it.
Upvotes: 1