Get all reflected java class member variables (in this class and the base classes)

We know that in Java's reflection mechanism, the core of a class is the class Class.

Class class provides two common methods of obtaining class member variables.

METHOD 1 getFields ()

/**
 * Returns an array containing {@code Field} objects reflecting all
 * the accessible public fields of the class or interface represented by
 * this {@code Class} object.
 *
 * <p> If this {@code Class} object represents a class or interface with no
 * no accessible public fields, then this method returns an array of length
 * 0.
 *
 * <p> If this {@code Class} object represents a class, then this method
 * returns the public fields of the class and of all its superclasses.
 *
 * <p> If this {@code Class} object represents an interface, then this
 * method returns the fields of the interface and of all its
 * Superinterface.
 *
 * <p> If this {@code Class} object represents an array type, a primitive
 * type, or void, then this method returns an array of length 0.
 *
 * <p> The elements in the returned array are not sorted and are not in any
 * particular order.
 *
 * @return the array of {@code Field} objects representing the
 *         public fields
 * @throws SecurityException
 *         If a security manager, <i>s</i>, is present and
 *         the caller's class loader is not the same as or an
 *         ancestor of the class loader for the current class and
 *         invocation of {@link SecurityManager#checkPackageAccess
 *         s.checkPackageAccess()} denies access to the package
 *         of this class.
 *
 * @since JDK1.1
 * @jls 8.2 Class Members
 * @jls 8.3 Field Declarations
 */
@CallerSensitive
public Field[] getFields() throws SecurityException {
    checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
    return copyFields(privateGetPublicFields(null));
}

Can be seen from the comments, this method is used to obtain a class and all of its parent class modifiers modified public member variables.

METHOD 2 getDeclaredFields ()

/**
 * Returns an array of {@code Field} objects reflecting all the fields
 * declared by the class or interface represented by this
 * {@code Class} object. This includes public, protected, default
 * (package) access, and private fields, but excludes inherited fields.
 *
 * <p> If this {@code Class} object represents a class or interface with no
 * declared fields, then this method returns an array of length 0.
 *
 * <p> If this {@code Class} object represents an array type, a primitive
 * type, or void, then this method returns an array of length 0.
 *
 * <p> The elements in the returned array are not sorted and are not in any
 * particular order.
 *
 * @return  the array of {@code Field} objects representing all the
 *          declared fields of this class
 * @throws  SecurityException
 *          If a security manager, <i>s</i>, is present and any of the
 *          following conditions is met:
 *
 *          <ul>
 *
 *          <li> the caller's class loader is not the same as the
 *          class loader of this class and invocation of
 *          {@link SecurityManager#checkPermission
 *          s.checkPermission} method with
 *          {@code RuntimePermission("accessDeclaredMembers")}
 *          denies access to the declared fields within this class
 *
 *          <li> the caller's class loader is not the same as or an
 *          ancestor of the class loader for the current class and
 *          invocation of {@link SecurityManager#checkPackageAccess
 *          s.checkPackageAccess()} denies access to the package
 *          of this class
 *
 *          </ul>
 *
 * @since JDK1.1
 * @jls 8.2 Class Members
 * @jls 8.3 Field Declarations
 */
@CallerSensitive
public Field[] getDeclaredFields() throws SecurityException {
    checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
    return copyFields(privateGetDeclaredFields(false));
}

Can be seen from the annotation, this method is used to get all members of a class of variables, i.e. comprising a modified public, protected, defautl all member variables and private modifiers.

But this method, a base class member variables that will not get them.

Take this class and the base classes for all member variables

All members of this class of variables to take the base class and two classes Class provided obtaining a class member variables can not be directly implemented method of this requirement, but may be realized by a simple while loop.

// Take all fields (including the base class field) 
Field, [] = allFields clazz.getDeclaredFields ();
Class superClass = clazz.getSuperclass();
while (superClass != null) {
  Field[] superFileds = superClass.getDeclaredFields();
  allfields = ArrayUtils.addAll (allfields, superFileds);
  superClass = superClass.getSuperclass();
}

This takes into account all class member variables include member variables of the base class.

 

"Fatherly silent, but it has been in."

Guess you like

Origin www.cnblogs.com/yanggb/p/11848524.html