通过反射获取子类和父类定义的属性


通过反射获取子类和父类定义的属


抛出问题

如何子类和父类中定义的所有(public/protected/dufault/private)属性?

getDeclaredFields()

 
 
 
  1. public Field[] getDeclaredFields() throws SecurityException
  2. 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.
  3. See The Java Language Specification, sections 8.2 and 8.3.
  4. Returns:
  5. the array of Field objects representing all the declared fields of this class
  6. Throws:
  7. SecurityException - If a security manager, s, is present and any of the following conditions is met:
  8. invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared fields within this class
  9. 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
  10. Since:
  11. JDK1.1

getFields()

 
 
 
  1. public Field[] getFields() throws SecurityException
  2. 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.
  3. 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.
  4. 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.
  5. See The Java Language Specification, sections 8.2 and 8.3.
  6. Returns:
  7. the array of Field objects representing the public fields
  8. Throws:
  9. SecurityException - If a security manager, s, is present and any of the following conditions is met:
  10. invocation of s.checkMemberAccess(this, Member.PUBLIC) denies access to the fields within this class
  11. 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
  12. Since:
  13. JDK1.1

根据API可知

  1. getDeclaredFields() 方法只能获取本类中定义的所有方法(public/protected/default/private),获取不到父类中定义的方法,即使是product的方法也获取不到
  2. getFields() 方法能获取到本类及父类的方法,但是这些方法必须是public的,其他的都获取不到。

那么问题来了,一般父类定义的属性访问权限为protected,这样的话,根据以上两个方法都获取不到父类中的protected修饰的属性了。那如何获取父类定义的所有属性呢?
可以采用 循环向上找父类,然后调用getDeclaredFields()方法。
代码如下:

 
 
 
  1. public static TreeMap<String, String> convertBeanToMap(Object bean) throws IllegalArgumentException,IllegalAccessException {
  2. TreeMap<String, String> map = new TreeMap<String, String>();
  3. Class<?> clazz = bean.getClass();
  4. for(; clazz != Object.class;clazz = clazz.getSuperclass()){
  5. Field[] fields = clazz.getDeclaredFields();
  6. for (Field field : fields) {// 获取bean的属性和值
  7. field.setAccessible(true);
  8. if (field.get(bean) != null) {
  9. if (field.get(bean) instanceof Double) {
  10. map.put(field.getName(), formatDb2Str((Double) field.get(bean)));
  11. } else {
  12. map.put(field.getName(), field.get(bean).toString());
  13. }
  14. }
  15. }
  16. }
  17. return map;
  18. }





免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM