I'd like to have a JUnit test that verifies a specific constant is a Compile-Time Constant. How would I go about doing that?
I found a solution for Scala, but I'd like on for plain Java.
Is there a way to test at compile-time that a constant is a compile-time constant?
The value for annotation attribute ApiModelProperty.allowableValues must be a constant expression
validateCompileTimeConstant(SomeClass.CONSTANT_VALUE, "Message Here!!");
Usage
@ApiModelProperty(name = "name", required = true, value = "Name", allowableValues=SomeClass.API_ALLOWABLE_VALUES, notes=SomeClass.API_NOTES)
private String name;
SomeClass
public enum SomeClass {
BOB(4, "Bob"),//
TED(9, "Ted"),//
NED(13, "Ned");
public static final String API_ALLOWABLE_VALUES = "4,9,13,16,21,26,27,170";
public static final String API_NOTES = "4 - Bob\n" +
"9 - Ted\n" +
"13 - Ned";
public int code;
public String desc;
private ContentCategoryCode(int code, String desc) {
this.code = code;
this.desc = desc;
}
public static final String apiAllowableValues() {
StringBuilder b = new StringBuilder();
for (ContentCategoryCode catCode : values()) {
b.append(catCode.code);
b.append(',');
}
b.setLength(b.length()-1);
return b.toString();
}
public static final String apiNotes() {
StringBuilder b = new StringBuilder();
for (ContentCategoryCode catCode : values()) {
b.append(catCode.code).append(" - ").append(catCode.desc);
b.append('\n');
}
b.setLength(b.length()-1);
return b.toString();
}
}