From de0ef4e5ff31dd167f04e4dc29442e582042053f Mon Sep 17 00:00:00 2001 From: kfyty Date: Wed, 25 Nov 2020 18:39:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=B7=E6=96=B0=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3=E7=9A=84=E5=B1=9E=E6=80=A7=E5=80=BC=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/annotation/AnnotationUtil.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) 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); + } }