/**
* 修改元素注解属性值
*
* @param element 被注解元素
* @param annotationClass 注解类
* @param key 注解属性key
* @param value 注解属性value
*/
public static void modifyAnnotationAttributeValue(AnnotatedElement element,
Class<? extends Annotation> annotationClass,
String key, Object value) throws NoSuchFieldException, IllegalAccessException {
Annotation annotationToBeModified = element.getAnnotation(annotationClass);
if (annotationToBeModified == null) return;
InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotationToBeModified);
Field memberValuesField = invocationHandler.getClass().getDeclaredField("memberValues");
memberValuesField.setAccessible(true);
Map<String, Object> memberValues = (Map<String, Object>) memberValuesField.get(invocationHandler);
memberValues.put(key, value);
}
public static void main(String[] args) throws Exception {
ScoreResultResDTO obj = new ScoreResultResDTO(null, 100.0);
Field field = obj.getClass().getDeclaredField("satisfactionSubtotal");
ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
System.out.println("format: " + annotation.format());
modifyAnnotationAttributeValue(field, ExcelProperty.class, "format", "222");
System.out.println("format: " + annotation.format());
}
java修改元素注解属性值
最新推荐文章于 2024-02-04 10:01:28 发布