英文:
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
。
如果我没有弄错的话,这意味着不可能修改 @ParamAnnotation
的 theKey
的值(除非在字节码中进行操作,但这可能比使用反射进行更糟糕的方式还要糟糕)。
另一方面,这似乎很奇怪,因为对于 @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.
专注分享java语言的经验与见解,让所有开发者获益!
评论