I would like to specify a javax constraint annotation message as an enum. Since the javax constraint message needs to be a String, I tried using enum#name() as follows:
import javax.validation.constraints.NotEmpty;
public class Test
{
enum MessageKey {
KEY_1,
KEY_2
}
@NotEmpty(message = MessageKey.KEY_1.name())
private String name;
}
However, this failed to compile:
C:\Users\GeoffAlexander\Documents\Java>javac -cp .\validation-api-2.0.1.Final.jar Test.java Test.java:10: error: element value must be a constant expression @NotEmpty(message = MessageKey.KEY_1.name())
Since enum#name() is declared as final
, I expected the compiler to process MessageKey.KEY_1.name()
as a constant. But it seems that it does not. Is there any way I can specify the enum constant as the message parameter (I don't need a non-enum workaround as I already know how to do that)?