Reputation: 28188
Why does the following code compile:
final String name = "works";
@Provides @Named(name) String provideAboutTitle() {
return "ABC";
}
But the following code fails (at least with Eclipse's compiler):
final String name = UUID.randomUUID().toString();
@Provides @Named(name) String provideAboutTitle() {
return "ABC";
}
Eclipse's compiler returns the following error:
The value for annotation attribute Named.value must be a constant expression
Upvotes: 4
Views: 1482
Reputation: 28188
The constant expression Eclipse demands in the error message is a compile-time constant expression (not just a final variable) and the method call UUID.randomUUID().toString();
needs to be evaluated at run-time.
While you can write dynamic annotation values using JavaAssist at runtime, you will lose "easy to read" feature of the annotations.
Upvotes: 4