public class Test {
public static void main(String[] args) throws Exception {
javax.persistence.Table tableAnnotation = com.xxx.po.xxx.class.getAnnotation(javax.persistence.Table.class);
System.out.println("修改前:" + tableAnnotation.name());
setValueToAnnotate(tableAnnotation, "name", "tab_data202404");
System.out.println("修改after1:" + tableAnnotation.name());
setValueToAnnotate(tableAnnotation, "name", "tab_data202405");
System.out.println("修改after2:" + tableAnnotation.name());
}
/**
* 动态设置注解值
*/
public static <T extends Annotation> void setValueToAnnotate(T annotation, String valueName, Object value)
throws NoSuchFieldException, IllegalAccessException {
InvocationHandler invocationHandler = null;
if (Objects.nonNull(annotation)) {
invocationHandler = Proxy.getInvocationHandler(annotation);
Field nameField = invocationHandler.getClass().getDeclaredField("memberValues");
nameField.setAccessible(true);
Map<String, Object> memberValues = (Map<String, Object>) nameField.get(invocationHandler);
memberValues.put(valueName, value);
}
}
}