How to Supply Value to an Annotation from a Constant Java

How to supply value to an annotation from a Constant java

Compile constants can only be primitives and Strings:

15.28. Constant Expressions

A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

  • Literals of primitive type and literals of type String
  • Casts to primitive types and casts to type String
  • [...] operators [...]
  • Parenthesized expressions whose contained expression is a constant expression.
  • Simple names that refer to constant variables.
  • Qualified names of the form TypeName . Identifier that refer to constant variables.

Actually in java there is no way to protect items in an array. At runtime someone can always do FieldValues.FIELD1[0]="value3", therefore the array cannot be really constant if we look deeper.

How to supply Enum value to an annotation from a Constant in Java

It seems to be defined in the JLS #9.7.1:

[...] The type of V is assignment compatible (§5.2) with T, and furthermore:

  • [...]
  • If T is an enum type, and V is an enum constant.

And an enum constant is defined as the actual enum constant (JLS #8.9.1), not a variable that points to that constant.

Bottom line: if you want to use an enum as a parameter for your annotation, you will need to give it an explicit MyEnum.XXXX value. If you want to use a variable, you will need to pick another type (not an enum).

One possible workaround is to use a String or int that you can then map to your enum - you will loose the type safety but the errors can be spotted easily at runtime (= during tests).

How can I supply a symbol to a Java annotation?

We all agree that a static import is a constant.

No, we don't.

A static import does nothing other than making a field/method/... accessible via a simple name as opposed to having to use the fully qualified name.

That simple name might be a reference to a constant field or it might be a reference to something else.

You've defined someFunction as

public static Function<Long, Long> someFunction = a -> b;

According to the rules of the Java language that is not a "constant expression" (emphasis mine):

A constant expression is an expression denoting a value of primitive type or a String [...]

Obviously a Function is neither a primitive value nor a String.

Additionally you've defined the symbol to take a Class<? extends Function>, which means you need to assign it a class that implements Function. someFunction is not a Class, it's an actual object that happens to implement Function.

Java Annotation to enforce a compile-time minimum value of a constant?

The Constant Value Checker of the Checker Framework has an @IntRange annotation that does what you want.

If you annotate the type of a variable like this:

@IntRange(from=0, to=11) int month;

then at compile time, the compiler will issue an error anywhere in the program that month might take on a value outside of the range 0..11. You can write just one of the bounds; in your case, you would use

public static final @IntRange(from=3) int BORDER=2;  // compile-time error

Get rid of The value for annotation attribute must be a constant expression message

The value for an annotation must be a compile time constant, so there is no simple way of doing what you are trying to do.

See also here: How to supply value to an annotation from a Constant java

It is possible to use some compile time tools (ant, maven?) to config it if the value is known before you try to run the program.



Related Topics



Leave a reply



Submit