Tôi tự hỏi liệu có cách nào để có thể nhận thông tin chú thích của một lớp khi chạy không? Vì tôi muốn nhận được các thuộc tính được chú thích một cách cẩn thận.lấy thông tin chú thích khi chạy
Ví dụ:
class TestMain {
@Field(
store = Store.NO)
private String name;
private String password;
@Field(
store = Store.YES)
private int age;
//..........getter and setter
}
Các chú thích xuất phát từ hibernate-tìm kiếm, và bây giờ những gì tôi muốn là được mà tài sản của "TestMain" được chú thích như một 'trường' (trong ví dụ này, chúng là [tên, tuổi]) và được lưu trữ (lưu trữ = store.yes) '(trong ví dụ, chúng là [tuổi]) khi chạy.
Bất kỳ ý tưởng nào?
Cập nhật:
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;
}
}
Bạn có thể nhận được phản hồi nhanh hơn nếu bạn thêm thẻ bằng ngôn ngữ lập trình bạn đang sử dụng. –
Xin cảm ơn, tôi đã thêm nó – hguser