添加刷新注解的属性值的方法

This commit is contained in:
kfyty 2020-11-25 18:39:19 +08:00
parent 05586769c9
commit de0ef4e5ff

View File

@ -12,7 +12,10 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;
@ -200,4 +203,22 @@ public class AnnotationUtil {
public static boolean isInherited(Class<? extends Annotation> annotationType) {
return annotationType.isAnnotationPresent(Inherited.class);
}
/**
* 刷新注解的属性值
* @param annotation 注解对象
* @param annotationField 注解属性名称
* @param value 要更新的属性值
* @throws Exception 可能出现的异常(一般情况下不会发生)
*/
public static void refreshAnnotationValue(Annotation annotation, String annotationField, Object value) throws Exception {
InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
Field field = invocationHandler.getClass().getDeclaredField("memberValues");
boolean accessible = field.isAccessible();
field.setAccessible(true);
@SuppressWarnings("unchecked")
Map<Object, Object> memberValues = (Map<Object, Object>) field.get(invocationHandler);
memberValues.put(annotationField, value);
field.setAccessible(accessible);
}
}