在运行时修改方法注解参数 [英] Modify a method annotation parameter at runtime
问题描述
我发现了这个线程:如何在运行时使用反射?
我正在尝试更改方法注释,但 java.lang.reflect.Method 不包含任何像注释"这样的映射字段或像getDeclaredAnnotationMap"这样的方法
And I'm trying to change method annotation, but java.lang.reflect.Method does not contain any map-field like "annotations" or method like "getDeclaredAnnotationMap"
只有 private byte[] annotations
但是我可以用这个字节数组做什么?
There is only private byte[] annotations
but what can I do with this byte array?
那么,如何修改方法的注解?
So, how to modify annotation of method?
我创建的:http://pastebin.com/T2rewcwU
但是那只编辑这个方法实例,如果你取消注释33行代码,那么值将重置.
I created that: http://pastebin.com/T2rewcwU
But that only edit this instance of method, if you uncomment 33 line of code then value will reset.
推荐答案
我写了一个AnnotationUtil
类来解决seris需求.
I wrote a class AnnotationUtil
to resolve the seris requirements.
它可以在类/字段/方法实例上添加/删除/更改注释值.
It can add/remove/change annotation value on class/field/method instance.
注意在添加/删除注解时使用ReflectUtil
获取根字段/方法.
Note that use ReflectUtil
to get root field/method when add/remove annotation.
在github上看到它:
See it on github:
这篇关于在运行时修改方法注解参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!