具体实现:
public static <A extends Annotation> Class<?> changeAnnotationValue(String variableName,Class<?> clazz, Class<A> tClass, String filedName, Object value) {
try {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.getName().equals(variableName)){
A annotation = field.getAnnotation(tClass);
setAnnotationValue(annotation, filedName, value);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return clazz;
}
public static void setAnnotationValue( Annotation annotation, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException {
InvocationHandler handler = Proxy.getInvocationHandler(annotation);
Field field = handler.getClass().getDeclaredField("memberValues");
field.setAccessible(true);
Map memberValues = (Map) field.get(handler);
memberValues.put(fieldName, value);
}
具体应用
待修改字段:
@ExcelProperty(value = {"序号"} )
@ColumnWidth(30)
private String serialNumber;
执行修改方法修改字段value属性:
String[] strs = new String[]{"工程名称:"+form.getProjectName(),"序号"};
Util.changeAnnotationValue("serialNumber",POJO.class, ExcelProperty.class,"value",strs);