改变参数注释的值

huangapple 未分类评论52阅读模式
英文:

Change the value of a parameter annotation

问题

在Java中有没有一种方法可以修改方法参数的注解?

在这种情况下,我想修改theKey的内容(当前为:CHANGE_ME)为新的内容(例如I'M CHANGED)。

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

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

}

据我所了解,通过运行时调试,getAnnotations()Parameter.java 最终会导致 Executable.java 中的 sharedGetParameterAnnotations(),该方法解析一些 parameterAnnotations 字节数组,然后构造一个 Annotation

如果我没有弄错的话,这意味着不可能修改 @ParamAnnotationtheKey 的值(除非在字节码中进行操作,但这可能比使用反射进行更糟糕的方式还要糟糕)。

另一方面,这似乎很奇怪,因为对于 @ClassAnnotation@MethodAnnotation 中的 foo 进行更改是可以正常工作的。

英文:

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.

huangapple
  • 本文由 发表于 2020年6月5日 20:30:35
  • 转载请务必保留本文链接:https://java.coder-hub.com/62215421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定