diff --git a/hutool-core/src/main/java/cn/hutool/core/annotation/AnnotationUtil.java b/hutool-core/src/main/java/cn/hutool/core/annotation/AnnotationUtil.java index ad07b7f73..32e3571c0 100644 --- a/hutool-core/src/main/java/cn/hutool/core/annotation/AnnotationUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/annotation/AnnotationUtil.java @@ -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 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 memberValues = (Map) field.get(invocationHandler); + memberValues.put(annotationField, value); + field.setAccessible(accessible); + } }