1

Here's the code:

SomeFile.properties

string.regex="someRegex"

SomeActionClass.java

@Value("${string.regex}")
private String REGEX="";

@Validate(mask = REGEX)
private String stringToValidate;

The file SomeFile.properties is loaded on server startup and the value "someRegex" is stored somewhere in the session, so it is available.

The problem is that mask requires a constant value, and if I put final at REGEX="" I cannot overwrite the init value with the string.regex one; on the other hand if I put final but do not initialize the variable, java compiler complain about it.

Is there a way to use mask option with a value retrieved from file that I do not know?

Is it possible to do this directly on a jsp in a smart way, since I've have several fields to validate and I do not want to perform a check and write a message error for each one?

Dimitri Dewaele
  • 10,311
  • 21
  • 80
  • 127
lupin_85
  • 21
  • 4

1 Answers1

0

Annotation value can be changed at runtime using reflection.

Please take a look at this (see the post marked as answer)

Modify a class definition's annotation string parameter at runtime

Be careful with this approach, some of the caveats of it are also described. Also, for being able to chenge this value at runtime, the retention policy for the annotation has to be Runtime

It's not the solution to your particular question, but it has useful information on how ti perform this operation on annotations.

Community
  • 1
  • 1
Cristian Meneses
  • 4,013
  • 17
  • 32