Je me demande si je peux obtenir les informations d'annotation d'une classe à l'exécution? Puisque je veux obtenir les propriétés qui ont été annotées.obtenir les informations d'annotation à l'exécution
Exemple:
class TestMain {
@Field(
store = Store.NO)
private String name;
private String password;
@Field(
store = Store.YES)
private int age;
//..........getter and setter
}
Les annotations proviennent de la mise en veille prolongée à la recherche d', et maintenant ce que je veux est d'obtenir que la propriété du « TestMain » est annotés comme un « champ » (dans l'exemple, ils sont [nom, âge]), et qui est 'stocké (store = store.yes)' (dans l'exemple, ils sont [age]) lors de l'exécution.
Des idées?
Mise à jour:
public class FieldUtil {
public static List<String> getAllFieldsByClass(Class<?> clazz) {
Field[] fields = clazz.getDeclaredFields();
ArrayList<String> fieldList = new ArrayList<String>();
ArrayList<String> storedList=new ArrayList<String>();
String tmp;
for (int i = 0; i < fields.length; i++) {
Field fi = fields[i];
tmp = fi.getName();
if (tmp.equalsIgnoreCase("serialVersionUID"))
continue;
if (fi.isAnnotationPresent(org.hibernate.search.annotations.Field.class)) {
//it is a "field",add it to list.
fieldList.add(tmp);
//make sure if it is stored also
Annotation[] ans = fi.getAnnotations();
for (Annotation an : ans) {
//here,how to get the detail annotation information
//I print the value of an,it is something like this:
//@org.hibernate.search.annotations.Field(termVector=NO, index=UN_TOKENIZED, store=NO, name=, [email protected](value=1.0), [email protected]Analyzer(impl=void, definition=), [email protected](impl=void, params=[]))
//how to get the parameter value of this an? using the string method?split?
}
}
}
return fieldList;
}
}
Vous obtiendrez probablement une réponse plus rapidement si vous ajoutez un tag au langage de programmation que vous utilisez. –
Merci, je l'ajoute – hguser