0

Is there any way in Java to modify an annotation of a method parameter?

In this case, I would like modify the content of theKey (currently: CHANGE_ME) to something new (e.g. I'M CHANGED).

@ClassAnnotation(foo = "bar")
public interface SomeInterface {

    @MethodAnnotation(foo = "bar")
    void getDomains(@ParamAnnotation(theKey = "CHANGE_ME") String theString);

}

As far as I found out with runtime debugging, getAnnotations() in Parameter.java finally leads to sharedGetParameterAnnotations() in Executable.java which parses some parameterAnnotations ByteArray and then constructs an Annotation.

If I'm not mistaken, this would mean, that modifying the value of @ParamAnnotation's theKey is not possible (unless messing around with the byte code - which is probably even more ugly then changing stuff with Reflection).

On the other side, this seems to be strange as changing values for foo in @ClassAnnotation and @MethodAnnotation works fine.

debuglevel
  • 434
  • 5
  • 10
  • 1
    What's your end goal? Can't you do this in the thing processing the annotation? – Sotirios Delimanolis Jun 05 '20 at 12:31
  • @SotiriosDelimanolis The thing processing the annoation is actually JAX-WS. So, unfortunately, no :-) – debuglevel Jun 05 '20 at 12:34
  • jax-ws only processes your class once, doesn't it and that it on application start. So you'd need to change that annotation *before* jax-ws is started – Lino Jun 05 '20 at 12:36
  • Let's ignore JAX-WS for now, and focus on the question: Is there a way to modify a **parameter** annotation? Unfortunately, the linked question does not answer this, but only how to modify a **class** annotation (also works for **method** annotations, but not for **parameter annotations**) – debuglevel Jun 05 '20 at 12:40
  • All annotations behave the same. It's unclear what you mean by _works fine_. Please provide an example of what you did for those that didn't work for the parameter annotation. – Sotirios Delimanolis Jun 05 '20 at 12:42

0 Answers0