我發現了另一個問題的答案。這是我正在尋找的。
// Put in Reflector.java
/**
* Changes the annotation value for the given key of the given annotation to newValue and returns
* the previous value.
*/
@SuppressWarnings("unchecked")
public static Object changeAnnotationValue(Annotation annotation, String key, Object newValue){
Object handler = Proxy.getInvocationHandler(annotation);
Field f;
try {
f = handler.getClass().getDeclaredField("memberValues");
} catch (NoSuchFieldException | SecurityException e) {
throw new IllegalStateException(e);
}
f.setAccessible(true);
Map<String, Object> memberValues;
try {
memberValues = (Map<String, Object>) f.get(handler);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
Object oldValue = memberValues.get(key);
if (oldValue == null || oldValue.getClass() != newValue.getClass()) {
throw new IllegalArgumentException();
}
memberValues.put(key,newValue);
return oldValue;
}
一旦我有了這個,我可以在構造函數中調用下面的方法來改變我使用的註釋。
// Put in Service Class
public void modifySearchable(Class<?> clazz) {
for(Field f : clazz.getDeclaredFields()){
CrudSearchable[] searchableArray = f.getDeclaredAnnotationsByType(CrudSearchable.class);
for(CrudSearchable searchable : searchableArray){
if(searchable == null){
continue;
}
Reflector.alterAnnotation(searchable, "attribute", f.getName());
Reflector.alterAnnotation(searchable, "parentClass", clazz);
if(!(searchable.mappedAttribute().equals(""))){
String mappedGetter = "get" +
searchable.mappedAttribute().substring(0, 1).toUpperCase() +
searchable.mappedAttribute().substring(1);
Reflector.alterAnnotation(searchable, "mappedClass", f.getType());
Reflector.alterAnnotation(searchable, "mappedGetter", mappedGetter);
}
}
}
}
// Changed Bean
@Id
@GeneratedValue
@Column(name = "MOCK_USER_ID")
private Long id;
@Column(name = "MOCK_USER_NAME")
@CrudSearchable
private String name;
@ManyToOne
@JoinColumn(name = "MOCK_GROUP", nullable = false)
@CrudSearchable(mappedAttribute = "name")
private MockGroup group;
public MockUser(){
super();
new Searchable<>().modifySearchable(this.getClass());
}
好像很多改變,而不是讓用戶定義他們的價值觀,但我相信,這將使得代碼更人性化。
希望這可以幫助別人。我在這篇文章中找到了答案:Modify a class definition's annotation string parameter at runtime。一探究竟!
感謝您的回覆,但我已經能夠訪問元素,一旦我硬編碼的值,但如果你看到的豆我重申從類可見的信息。我想從註釋中抓住這個。即g @CrudSearchable(attribute =「name」,parentClass = MockUser.class)字符串名稱;應該用@CrudSearchable字符串名稱替換;我從註釋代碼中推斷出屬性名稱和類別。這可能嗎? – jDub9