Search code examples
javareflectionannotationsconfigoval

Overwrite Annotations with Reflection?


I'm using Oval to do validations, so we have code that looks like this:

   @NotNull(errorCode = "NumberInvalid")
   @NotNegative(errorCode = "NumberInvalid")
   @Range(errorCode = "NumberInvalid", min=1, max=10)
   protected Integer dollarAmount;

I want to be able to update the range dynamically if needed, say if we have a config file that changes every month or so, I want to be able to reflect those changes in the validation.

Is there a way I can do this with reflection so that I don't have to subclass every annotation in Oval?


Solution

  • As far as I'm aware this is not possible. Assuming your annotation retention is set to RUNTIME (which it would have to be for the validation to work) then what you will effectively have is a proxy class that implements the annotation interface, you won't be able to amend the values through reflection.