public class FieldUtils extends Object
Fields by reflection. Adapted and refactored from the dormant [reflect] Commons
 sandbox component.
 The ability is provided to break the scoping restrictions coded by the programmer. This can allow fields to be changed that shouldn't be. This facility should be used with care.
| Constructor and Description | 
|---|
| FieldUtils()FieldUtilsinstances should NOT be constructed in standard programming. | 
| Modifier and Type | Method and Description | 
|---|---|
| static Field[] | getAllFields(Class<?> cls)Gets all fields of the given class and its parents (if any). | 
| static List<Field> | getAllFieldsList(Class<?> cls)Gets all fields of the given class and its parents (if any). | 
| static Field | getDeclaredField(Class<?> cls,
                String fieldName)Gets an accessible  Fieldby name respecting scope. | 
| static Field | getDeclaredField(Class<?> cls,
                String fieldName,
                boolean forceAccess)Gets an accessible  Fieldby name, breaking scope if requested. | 
| static Field | getField(Class<?> cls,
        String fieldName)Gets an accessible  Fieldby name respecting scope. | 
| static Field | getField(Class<?> cls,
        String fieldName,
        boolean forceAccess)Gets an accessible  Fieldby name, breaking scope if requested. | 
| static List<Field> | getFieldsListWithAnnotation(Class<?> cls,
                           Class<? extends Annotation> annotationCls)Gets all fields of the given class and its parents (if any) that are annotated with the given annotation. | 
| static Field[] | getFieldsWithAnnotation(Class<?> cls,
                       Class<? extends Annotation> annotationCls)Gets all fields of the given class and its parents (if any) that are annotated with the given annotation. | 
| static Object | readDeclaredField(Object target,
                 String fieldName)Reads the named  publicField. | 
| static Object | readDeclaredField(Object target,
                 String fieldName,
                 boolean forceAccess)Gets a  Fieldvalue by name. | 
| static Object | readDeclaredStaticField(Class<?> cls,
                       String fieldName)Gets the value of a  staticFieldby name. | 
| static Object | readDeclaredStaticField(Class<?> cls,
                       String fieldName,
                       boolean forceAccess)Gets the value of a  staticFieldby name. | 
| static Object | readField(Field field,
         Object target)Reads an accessible  Field. | 
| static Object | readField(Field field,
         Object target,
         boolean forceAccess)Reads a  Field. | 
| static Object | readField(Object target,
         String fieldName)Reads the named  publicField. | 
| static Object | readField(Object target,
         String fieldName,
         boolean forceAccess)Reads the named  Field. | 
| static Object | readStaticField(Class<?> cls,
               String fieldName)Reads the named  public staticField. | 
| static Object | readStaticField(Class<?> cls,
               String fieldName,
               boolean forceAccess)Reads the named  staticField. | 
| static Object | readStaticField(Field field)Reads an accessible  staticField. | 
| static Object | readStaticField(Field field,
               boolean forceAccess)Reads a static  Field. | 
| static void | removeFinalModifier(Field field)Removes the final modifier from a  Field. | 
| static void | removeFinalModifier(Field field,
                   boolean forceAccess)Removes the final modifier from a  Field. | 
| static void | writeDeclaredField(Object target,
                  String fieldName,
                  Object value)Writes a  publicField. | 
| static void | writeDeclaredField(Object target,
                  String fieldName,
                  Object value,
                  boolean forceAccess)Writes a  publicField. | 
| static void | writeDeclaredStaticField(Class<?> cls,
                        String fieldName,
                        Object value)Writes a named  public staticField. | 
| static void | writeDeclaredStaticField(Class<?> cls,
                        String fieldName,
                        Object value,
                        boolean forceAccess)Writes a named  staticField. | 
| static void | writeField(Field field,
          Object target,
          Object value)Writes an accessible  Field. | 
| static void | writeField(Field field,
          Object target,
          Object value,
          boolean forceAccess)Writes a  Field. | 
| static void | writeField(Object target,
          String fieldName,
          Object value)Writes a  publicField. | 
| static void | writeField(Object target,
          String fieldName,
          Object value,
          boolean forceAccess)Writes a  Field. | 
| static void | writeStaticField(Class<?> cls,
                String fieldName,
                Object value)Writes a named  public staticField. | 
| static void | writeStaticField(Class<?> cls,
                String fieldName,
                Object value,
                boolean forceAccess)Writes a named  staticField. | 
| static void | writeStaticField(Field field,
                Object value)Writes a  public staticField. | 
| static void | writeStaticField(Field field,
                Object value,
                boolean forceAccess)Writes a static  Field. | 
public FieldUtils()
FieldUtils instances should NOT be constructed in standard programming.
 
 This constructor is public to permit tools that require a JavaBean instance to operate.
 
public static Field getField(Class<?> cls, String fieldName)
Field by name respecting scope. Superclasses/interfaces will be considered.cls - the Class to reflect, must not be nullfieldName - the field name to obtainIllegalArgumentException - if the class is null, or the field name is blank or emptypublic static Field getField(Class<?> cls, String fieldName, boolean forceAccess)
Field by name, breaking scope if requested. Superclasses/interfaces will be
 considered.cls - the Class to reflect, must not be nullfieldName - the field name to obtainforceAccess - whether to break scope restrictions using the
            AccessibleObject.setAccessible(boolean) method. false will only
            match public fields.IllegalArgumentException - if the class is null, or the field name is blank or empty or is matched at multiple places
             in the inheritance hierarchypublic static Field getDeclaredField(Class<?> cls, String fieldName)
Field by name respecting scope. Only the specified class will be considered.cls - the Class to reflect, must not be nullfieldName - the field name to obtainIllegalArgumentException - if the class is null, or the field name is blank or emptypublic static Field getDeclaredField(Class<?> cls, String fieldName, boolean forceAccess)
Field by name, breaking scope if requested. Only the specified class will be
 considered.cls - the Class to reflect, must not be nullfieldName - the field name to obtainforceAccess - whether to break scope restrictions using the
            AccessibleObject.setAccessible(boolean) method. false will only
            match public fields.IllegalArgumentException - if the class is null, or the field name is blank or emptypublic static Field[] getAllFields(Class<?> cls)
cls - the Class to queryIllegalArgumentException - if the class is nullpublic static List<Field> getAllFieldsList(Class<?> cls)
cls - the Class to queryIllegalArgumentException - if the class is nullpublic static Field[] getFieldsWithAnnotation(Class<?> cls, Class<? extends Annotation> annotationCls)
cls - the Class to queryannotationCls - the Annotation that must be present on a field to be matchedIllegalArgumentException - if the class or annotation are nullpublic static List<Field> getFieldsListWithAnnotation(Class<?> cls, Class<? extends Annotation> annotationCls)
cls - the Class to queryannotationCls - the Annotation that must be present on a field to be matchedIllegalArgumentException - if the class or annotation are nullpublic static Object readStaticField(Field field) throws IllegalAccessException
static Field.field - to readIllegalArgumentException - if the field is null, or not staticIllegalAccessException - if the field is not accessiblepublic static Object readStaticField(Field field, boolean forceAccess) throws IllegalAccessException
Field.field - to readforceAccess - whether to break scope restrictions using the
            AccessibleObject.setAccessible(boolean) method.IllegalArgumentException - if the field is null or not staticIllegalAccessException - if the field is not made accessiblepublic static Object readStaticField(Class<?> cls, String fieldName) throws IllegalAccessException
public static Field. Superclasses will be considered.cls - the Class to reflect, must not be nullfieldName - the field name to obtainIllegalArgumentException - if the class is null, or the field name is blank or empty, is not static, or could
             not be foundIllegalAccessException - if the field is not accessiblepublic static Object readStaticField(Class<?> cls, String fieldName, boolean forceAccess) throws IllegalAccessException
static Field. Superclasses will be considered.cls - the Class to reflect, must not be nullfieldName - the field name to obtainforceAccess - whether to break scope restrictions using the
            AccessibleObject.setAccessible(boolean) method. false will only
            match public fields.IllegalArgumentException - if the class is null, or the field name is blank or empty, is not static, or could
             not be foundIllegalAccessException - if the field is not made accessiblepublic static Object readDeclaredStaticField(Class<?> cls, String fieldName) throws IllegalAccessException
static Field by name. The field must be public. Only the specified
 class will be considered.cls - the Class to reflect, must not be nullfieldName - the field name to obtainIllegalArgumentException - if the class is null, or the field name is blank or empty, is not static, or could
             not be foundIllegalAccessException - if the field is not accessiblepublic static Object readDeclaredStaticField(Class<?> cls, String fieldName, boolean forceAccess) throws IllegalAccessException
static Field by name. Only the specified class will be considered.cls - the Class to reflect, must not be nullfieldName - the field name to obtainforceAccess - whether to break scope restrictions using the
            AccessibleObject.setAccessible(boolean) method. false will only
            match public fields.IllegalArgumentException - if the class is null, or the field name is blank or empty, is not static, or could
             not be foundIllegalAccessException - if the field is not made accessiblepublic static Object readField(Field field, Object target) throws IllegalAccessException
Field.field - the field to usetarget - the object to call on, may be null for static fieldsIllegalArgumentException - if the field is nullIllegalAccessException - if the field is not accessiblepublic static Object readField(Field field, Object target, boolean forceAccess) throws IllegalAccessException
Field.field - the field to usetarget - the object to call on, may be null for static fieldsforceAccess - whether to break scope restrictions using the
            AccessibleObject.setAccessible(boolean) method.IllegalArgumentException - if the field is nullIllegalAccessException - if the field is not made accessiblepublic static Object readField(Object target, String fieldName) throws IllegalAccessException
public Field. Superclasses will be considered.target - the object to reflect, must not be nullfieldName - the field name to obtainIllegalArgumentException - if the class is null, or the field name is blank or empty or could not be foundIllegalAccessException - if the named field is not publicpublic static Object readField(Object target, String fieldName, boolean forceAccess) throws IllegalAccessException
Field. Superclasses will be considered.target - the object to reflect, must not be nullfieldName - the field name to obtainforceAccess - whether to break scope restrictions using the
            AccessibleObject.setAccessible(boolean) method. false will only
            match public fields.IllegalArgumentException - if target is null, or the field name is blank or empty or could not be foundIllegalAccessException - if the named field is not made accessiblepublic static Object readDeclaredField(Object target, String fieldName) throws IllegalAccessException
public Field. Only the class of the specified object will be considered.target - the object to reflect, must not be nullfieldName - the field name to obtainIllegalArgumentException - if target is null, or the field name is blank or empty or could not be foundIllegalAccessException - if the named field is not publicpublic static Object readDeclaredField(Object target, String fieldName, boolean forceAccess) throws IllegalAccessException
Field value by name. Only the class of the specified object will be considered.target - the object to reflect, must not be nullfieldName - the field name to obtainforceAccess - whether to break scope restrictions using the
            AccessibleObject.setAccessible(boolean) method. false will only
            match public fields.IllegalArgumentException - if target is null, or the field name is blank or empty or could not be foundIllegalAccessException - if the field is not made accessiblepublic static void writeStaticField(Field field, Object value) throws IllegalAccessException
public static Field.field - to writevalue - to setIllegalArgumentException - if the field is null or not static, or value is not assignableIllegalAccessException - if the field is not public or is finalpublic static void writeStaticField(Field field, Object value, boolean forceAccess) throws IllegalAccessException
Field.field - to writevalue - to setforceAccess - whether to break scope restrictions using the
            AccessibleObject.setAccessible(boolean) method. false will only
            match public fields.IllegalArgumentException - if the field is null or not static, or value is not assignableIllegalAccessException - if the field is not made accessible or is finalpublic static void writeStaticField(Class<?> cls, String fieldName, Object value) throws IllegalAccessException
public static Field. Superclasses will be considered.cls - Class on which the field is to be foundfieldName - to writevalue - to setIllegalArgumentException - if cls is null, the field name is blank or empty, the field cannot be located or is
             not static, or value is not assignableIllegalAccessException - if the field is not public or is finalpublic static void writeStaticField(Class<?> cls, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException
static Field. Superclasses will be considered.cls - Class on which the field is to be foundfieldName - to writevalue - to setforceAccess - whether to break scope restrictions using the
            AccessibleObject.setAccessible(boolean) method. false will only
            match public fields.IllegalArgumentException - if cls is null, the field name is blank or empty, the field cannot be located or is
             not static, or value is not assignableIllegalAccessException - if the field is not made accessible or is finalpublic static void writeDeclaredStaticField(Class<?> cls, String fieldName, Object value) throws IllegalAccessException
public static Field. Only the specified class will be considered.cls - Class on which the field is to be foundfieldName - to writevalue - to setIllegalArgumentException - if cls is null, the field name is blank or empty, the field cannot be located or is
             not static, or value is not assignableIllegalAccessException - if the field is not public or is finalpublic static void writeDeclaredStaticField(Class<?> cls, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException
static Field. Only the specified class will be considered.cls - Class on which the field is to be foundfieldName - to writevalue - to setforceAccess - whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method.
            false will only match public fields.IllegalArgumentException - if cls is null, the field name is blank or empty, the field cannot be located or is
             not static, or value is not assignableIllegalAccessException - if the field is not made accessible or is finalpublic static void writeField(Field field, Object target, Object value) throws IllegalAccessException
Field.field - to writetarget - the object to call on, may be null for static fieldsvalue - to setIllegalAccessException - if the field or target is null, the field is not accessible or is final, or
             value is not assignablepublic static void writeField(Field field, Object target, Object value, boolean forceAccess) throws IllegalAccessException
Field.field - to writetarget - the object to call on, may be null for static fieldsvalue - to setforceAccess - whether to break scope restrictions using the
            AccessibleObject.setAccessible(boolean) method. false will only
            match public fields.IllegalArgumentException - if the field is null or value is not assignableIllegalAccessException - if the field is not made accessible or is finalpublic static void removeFinalModifier(Field field)
Field.field - to remove the final modifierIllegalArgumentException - if the field is nullpublic static void removeFinalModifier(Field field, boolean forceAccess)
Field.field - to remove the final modifierforceAccess - whether to break scope restrictions using the
            AccessibleObject.setAccessible(boolean) method. false will only
            match public fields.IllegalArgumentException - if the field is nullpublic static void writeField(Object target, String fieldName, Object value) throws IllegalAccessException
public Field. Superclasses will be considered.target - the object to reflect, must not be nullfieldName - the field name to obtainvalue - to setIllegalArgumentException - if target is null, fieldName is blank or empty or could not be found, or
             value is not assignableIllegalAccessException - if the field is not accessiblepublic static void writeField(Object target, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException
Field. Superclasses will be considered.target - the object to reflect, must not be nullfieldName - the field name to obtainvalue - to setforceAccess - whether to break scope restrictions using the
            AccessibleObject.setAccessible(boolean) method. false will only
            match public fields.IllegalArgumentException - if target is null, fieldName is blank or empty or could not be found, or
             value is not assignableIllegalAccessException - if the field is not made accessiblepublic static void writeDeclaredField(Object target, String fieldName, Object value) throws IllegalAccessException
public Field. Only the specified class will be considered.target - the object to reflect, must not be nullfieldName - the field name to obtainvalue - to setIllegalArgumentException - if target is null, fieldName is blank or empty or could not be found, or
             value is not assignableIllegalAccessException - if the field is not made accessiblepublic static void writeDeclaredField(Object target, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException
public Field. Only the specified class will be considered.target - the object to reflect, must not be nullfieldName - the field name to obtainvalue - to setforceAccess - whether to break scope restrictions using the
            AccessibleObject.setAccessible(boolean) method. false will only
            match public fields.IllegalArgumentException - if target is null, fieldName is blank or empty or could not be found, or
             value is not assignableIllegalAccessException - if the field is not made accessibleCopyright © 2001–2016 The Apache Software Foundation. All rights reserved.