通過反射獲取子類和父類定義的屬
拋出問題
如何子類和父類中定義的所有(public/protected/dufault/private)屬性?
getDeclaredFields()
public Field[] getDeclaredFields() throws SecurityException
Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface declares no fields, or if this Class object represents a primitive type, an array class, or void.
See The Java Language Specification, sections 8.2 and 8.3.
Returns:
the array of Field objects representing all the declared fields of this class
Throws:
SecurityException - If a security manager, s, is present and any of the following conditions is met:
invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared fields within this class
the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation of s.checkPackageAccess() denies access to the package of this class
Since:
JDK1.1
getFields()
public Field[] getFields() throws SecurityException
Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface has no accessible public fields, or if it represents an array class, a primitive type, or void.
Specifically, if this Class object represents a class, this method returns the public fields of this class and of all its superclasses. If this Class object represents an interface, this method returns the fields of this interface and of all its superinterfaces.
The implicit length field for array class is not reflected by this method. User code should use the methods of class Array to manipulate arrays.
See The Java Language Specification, sections 8.2 and 8.3.
Returns:
the array of Field objects representing the public fields
Throws:
SecurityException - If a security manager, s, is present and any of the following conditions is met:
invocation of s.checkMemberAccess(this, Member.PUBLIC) denies access to the fields within this class
the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation of s.checkPackageAccess() denies access to the package of this class
Since:
JDK1.1
根據API可知
- getDeclaredFields() 方法只能獲取本類中定義的所有方法(public/protected/default/private),獲取不到父類中定義的方法,即使是product的方法也獲取不到
- getFields() 方法能獲取到本類及父類的方法,但是這些方法必須是public的,其他的都獲取不到。
那么問題來了,一般父類定義的屬性訪問權限為protected,這樣的話,根據以上兩個方法都獲取不到父類中的protected修飾的屬性了。那如何獲取父類定義的所有屬性呢?
可以采用 循環向上找父類,然后調用getDeclaredFields()方法。
代碼如下:
public static TreeMap<String, String> convertBeanToMap(Object bean) throws IllegalArgumentException,IllegalAccessException {
TreeMap<String, String> map = new TreeMap<String, String>();
Class<?> clazz = bean.getClass();
for(; clazz != Object.class;clazz = clazz.getSuperclass()){
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {// 獲取bean的屬性和值
field.setAccessible(true);
if (field.get(bean) != null) {
if (field.get(bean) instanceof Double) {
map.put(field.getName(), formatDb2Str((Double) field.get(bean)));
} else {
map.put(field.getName(), field.get(bean).toString());
}
}
}
}
return map;
}
