java在运行时动态更改类字段的注释
假设我有一个类定义
@DynamoDBTable(tableName = "table_one")
public class ProcessedVideoItem {
@DynamoDBHashKey(attributeName = "hash_key")
private String id;
}
如何在运行时将@DynamoDBHashKey
注释中的atributeName
动态更改为类似actual_hash_key
的其他内容
我找到了在运行时更改类上注释的解决方案:
void setAnnotationN(Class< ? > clazzToLookFor, Class<? extends Annotation> annotationToAlter){
Method method = Class.class.getDeclaredMethod("annotationData", null);
method.setAccessible(true);
Object annotationData = method.invoke(clazzToLookFor);
Field annotations = annotationData.getClass().getDeclaredField("annotations");
annotations.setAccessible(true);
Map<Class<? extends Annotation>, Annotation> map =
(Map<Class<? extends Annotation>, Annotation>)annotations.get(annotationData);
map.put(annotationToAlter, annotationValue);
}
但这不适用于类字段上的注释。 我试着换衣服
Object annotationData = method.invoke(clazzToLookFor.getDeclaredField("id"));
但这是行不通的
共 (0) 个答案