CCC
CCC

Reputation: 2761

constants in java annotations

I've got the following annotation: @javax.ws.rs.DefaultValue("212") Long bits

I want to reuse the "final static" values that are already defined as the argument for the annotation. Like this one:

final static final long PERMISSIONS = PERMISSION_A | PERMISSION_B; //this is 212

But since annotations require a "constant expression", it's not possible to go:

@DefaultValue(String.valueof(PERMISSIONS)) Long bits

Is there any way to accomplish this?

Upvotes: 2

Views: 716

Answers (1)

Mark Peters
Mark Peters

Reputation: 81074

String.valueOf(PERMISSIONS)

is not recognized as a compiler-time constant expression, but this is:

"" + PERMISSIONS

So use:

@DefaultValue("" + PERMISSIONS)

Obviously it's no general case solution. There just happens to be a compile-time constant way to construct a String from a constant numeric type. Say instead you had a String constant representing an integer, and wanted to convert it to an int to pass to an annotation, then you'd be out of luck.

Upvotes: 1

Related Questions