Me pregunto si hay alguna manera de que pueda obtener la información de anotación de una clase en tiempo de ejecución? Como quiero obtener las propiedades anotadas secuencialmente.obtener la información de la anotación en el tiempo de ejecución
Ejemplo:
class TestMain {
@Field(
store = Store.NO)
private String name;
private String password;
@Field(
store = Store.YES)
private int age;
//..........getter and setter
}
Las anotaciones provienen de la hibernación-búsqueda, y ahora lo que quiero es conseguir que la propiedad de la "TestMain" se anotado como un 'campo' (en el ejemplo, son [nombre, edad]), y que es 'almacenado (store = store.yes)' (en el ejemplo, son [edad]) en tiempo de ejecución.
¿Alguna idea?
Actualización:
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](impl=void, definition=), [email protected](impl=void, params=[]))
//how to get the parameter value of this an? using the string method?split?
}
}
}
return fieldList;
}
}
Es probable que obtenga una respuesta más rápida si agrega una etiqueta con el lenguaje de programación que está utilizando. –
Gracias, lo agrego – hguser