Package org.apache.commons.lang3.reflect
Class FieldUtils
java.lang.Object
org.apache.commons.lang3.reflect.FieldUtils
Utilities for working with 
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.
- Since:
- 2.5
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic Field[]getAllFields(Class<?> cls) Gets all fields of the given class and its parents (if any).getAllFieldsList(Class<?> cls) Gets all fields of the given class and its parents (if any).static FieldgetDeclaredField(Class<?> cls, String fieldName) Gets an accessibleFieldby name respecting scope.static FieldgetDeclaredField(Class<?> cls, String fieldName, boolean forceAccess) Gets an accessibleFieldby name, breaking scope if requested.static FieldGets an accessibleFieldby name respecting scope.static FieldGets an accessibleFieldby name, breaking scope if requested.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 ObjectreadDeclaredField(Object target, String fieldName) Reads the namedpublicField.static ObjectreadDeclaredField(Object target, String fieldName, boolean forceAccess) Gets aFieldvalue by name.static ObjectreadDeclaredStaticField(Class<?> cls, String fieldName) Gets the value of astaticFieldby name.static ObjectreadDeclaredStaticField(Class<?> cls, String fieldName, boolean forceAccess) Gets the value of astaticFieldby name.static ObjectReads the namedpublicField.static ObjectReads the namedField.static ObjectReads an accessibleField.static ObjectReads aField.static ObjectreadStaticField(Class<?> cls, String fieldName) Reads the namedpublic staticField.static ObjectreadStaticField(Class<?> cls, String fieldName, boolean forceAccess) Reads the namedstaticField.static ObjectreadStaticField(Field field) Reads an accessiblestaticField.static ObjectreadStaticField(Field field, boolean forceAccess) Reads a staticField.static voidremoveFinalModifier(Field field) Removes the final modifier from aField.static voidremoveFinalModifier(Field field, boolean forceAccess) Deprecated.As of Java 12, we can no longer drop thefinalmodifier, thus rendering this method obsolete.static voidwriteDeclaredField(Object target, String fieldName, Object value) Writes apublicField.static voidwriteDeclaredField(Object target, String fieldName, Object value, boolean forceAccess) Writes apublicField.static voidwriteDeclaredStaticField(Class<?> cls, String fieldName, Object value) Writes a namedpublic staticField.static voidwriteDeclaredStaticField(Class<?> cls, String fieldName, Object value, boolean forceAccess) Writes a namedstaticField.static voidwriteField(Object target, String fieldName, Object value) Writes apublicField.static voidwriteField(Object target, String fieldName, Object value, boolean forceAccess) Writes aField.static voidwriteField(Field field, Object target, Object value) Writes an accessibleField.static voidwriteField(Field field, Object target, Object value, boolean forceAccess) Writes aField.static voidwriteStaticField(Class<?> cls, String fieldName, Object value) Writes a namedpublic staticField.static voidwriteStaticField(Class<?> cls, String fieldName, Object value, boolean forceAccess) Writes a namedstaticField.static voidwriteStaticField(Field field, Object value) Writes apublic staticField.static voidwriteStaticField(Field field, Object value, boolean forceAccess) Writes a staticField.
- 
Constructor Details- 
FieldUtilsDeprecated.TODO Make private in 4.0.FieldUtilsinstances should NOT be constructed in standard programming.This constructor is publicto permit tools that require a JavaBean instance to operate.
 
- 
- 
Method Details- 
getAllFieldsGets all fields of the given class and its parents (if any).- Parameters:
- cls- the- Classto query
- Returns:
- an array of Fields (possibly empty).
- Throws:
- NullPointerException- if the class is- null
- Since:
- 3.2
 
- 
getAllFieldsListGets all fields of the given class and its parents (if any).- Parameters:
- cls- the- Classto query
- Returns:
- a list of Fields (possibly empty).
- Throws:
- NullPointerException- if the class is- null
- Since:
- 3.2
 
- 
getDeclaredFieldGets an accessibleFieldby name respecting scope. Only the specified class will be considered.- Parameters:
- cls- the- Classto reflect, must not be- null
- fieldName- the field name to obtain
- Returns:
- the Field object
- Throws:
- NullPointerException- if the class is- null
- IllegalArgumentException- if the field name is- null, blank, or empty
 
- 
getDeclaredFieldGets an accessibleFieldby name, breaking scope if requested. Only the specified class will be considered.- Parameters:
- cls- the- Classto reflect, must not be- null
- fieldName- the field name to obtain
- forceAccess- whether to break scope restrictions using the- AccessibleObject.setAccessible(boolean)method.- falsewill only match- publicfields.
- Returns:
- the Field object
- Throws:
- NullPointerException- if the class is- null
- IllegalArgumentException- if the field name is- null, blank, or empty
 
- 
getFieldGets an accessibleFieldby name respecting scope. Superclasses/interfaces will be considered.- Parameters:
- cls- the- Classto reflect, must not be- null
- fieldName- the field name to obtain
- Returns:
- the Field object
- Throws:
- NullPointerException- if the class is- null
- IllegalArgumentException- if the field name is- null, blank, or empty
 
- 
getFieldGets an accessibleFieldby name, breaking scope if requested. Superclasses/interfaces will be considered.- Parameters:
- cls- the- Classto reflect, must not be- null
- fieldName- the field name to obtain
- forceAccess- whether to break scope restrictions using the- AccessibleObject.setAccessible(boolean)method.- falsewill only match- publicfields.
- Returns:
- the Field object
- Throws:
- NullPointerException- if the class is- null
- IllegalArgumentException- if the field name is blank or empty or is matched at multiple places in the inheritance hierarchy
 
- 
getFieldsListWithAnnotationpublic 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.- Parameters:
- cls- the- Classto query
- annotationCls- the- Annotationthat must be present on a field to be matched
- Returns:
- a list of Fields (possibly empty).
- Throws:
- NullPointerException- if the class or annotation are- null
- Since:
- 3.4
 
- 
getFieldsWithAnnotationpublic 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.- Parameters:
- cls- the- Classto query
- annotationCls- the- Annotationthat must be present on a field to be matched
- Returns:
- an array of Fields (possibly empty).
- Throws:
- NullPointerException- if the class or annotation are- null
- Since:
- 3.4
 
- 
readDeclaredFieldpublic static Object readDeclaredField(Object target, String fieldName) throws IllegalAccessException Reads the namedpublicField. Only the class of the specified object will be considered.- Parameters:
- target- the object to reflect, must not be- null
- fieldName- the field name to obtain
- Returns:
- the value of the field
- Throws:
- NullPointerException- if- targetis- null
- IllegalArgumentException- if- fieldNameis- null, blank or empty, or could not be found
- IllegalAccessException- if the named field is not- public
 
- 
readDeclaredFieldpublic static Object readDeclaredField(Object target, String fieldName, boolean forceAccess) throws IllegalAccessException Gets aFieldvalue by name. Only the class of the specified object will be considered.- Parameters:
- target- the object to reflect, must not be- null
- fieldName- the field name to obtain
- forceAccess- whether to break scope restrictions using the- AccessibleObject.setAccessible(boolean)method.- falsewill only match public fields.
- Returns:
- the Field object
- Throws:
- NullPointerException- if- targetis- null
- IllegalArgumentException- if- fieldNameis- null, blank or empty, or could not be found
- IllegalAccessException- if the field is not made accessible
 
- 
readDeclaredStaticFieldpublic static Object readDeclaredStaticField(Class<?> cls, String fieldName) throws IllegalAccessException Gets the value of astaticFieldby name. The field must bepublic. Only the specified class will be considered.- Parameters:
- cls- the- Classto reflect, must not be- null
- fieldName- the field name to obtain
- Returns:
- the value of the field
- Throws:
- NullPointerException- if the class is- null, or the field could not be found
- IllegalArgumentException- if the field name is- null, blank, empty, or is not- static
- IllegalAccessException- if the field is not accessible
 
- 
readDeclaredStaticFieldpublic static Object readDeclaredStaticField(Class<?> cls, String fieldName, boolean forceAccess) throws IllegalAccessException Gets the value of astaticFieldby name. Only the specified class will be considered.- Parameters:
- cls- the- Classto reflect, must not be- null
- fieldName- the field name to obtain
- forceAccess- whether to break scope restrictions using the- AccessibleObject.setAccessible(boolean)method.- falsewill only match- publicfields.
- Returns:
- the Field object
- Throws:
- NullPointerException- if the class is- null, or the field could not be found
- IllegalArgumentException- if the field name is blank or empty, is not- static
- IllegalAccessException- if the field is not made accessible
 
- 
readFieldReads an accessibleField.- Parameters:
- field- the field to use
- target- the object to call on, may be- nullfor- staticfields
- Returns:
- the field value
- Throws:
- NullPointerException- if the field is- null
- IllegalAccessException- if the field is not accessible
 
- 
readFieldpublic static Object readField(Field field, Object target, boolean forceAccess) throws IllegalAccessException Reads aField.- Parameters:
- field- the field to use
- target- the object to call on, may be- nullfor- staticfields
- forceAccess- whether to break scope restrictions using the- AccessibleObject.setAccessible(boolean)method.
- Returns:
- the field value
- Throws:
- NullPointerException- if the field is- null
- IllegalAccessException- if the field is not made accessible
 
- 
readFieldReads the namedpublicField. Superclasses will be considered.- Parameters:
- target- the object to reflect, must not be- null
- fieldName- the field name to obtain
- Returns:
- the value of the field
- Throws:
- NullPointerException- if the target is- null
- IllegalArgumentException- if the field name is- null, blank, empty, or could not be found
- IllegalAccessException- if the named field is not- public
 
- 
readFieldpublic static Object readField(Object target, String fieldName, boolean forceAccess) throws IllegalAccessException Reads the namedField. Superclasses will be considered.- Parameters:
- target- the object to reflect, must not be- null
- fieldName- the field name to obtain
- forceAccess- whether to break scope restrictions using the- AccessibleObject.setAccessible(boolean)method.- falsewill only match- publicfields.
- Returns:
- the field value
- Throws:
- NullPointerException- if- targetis- null
- IllegalArgumentException- if the field name is- null, blank, empty, or could not be found
- IllegalAccessException- if the named field is not made accessible
 
- 
readStaticFieldReads the namedpublic staticField. Superclasses will be considered.- Parameters:
- cls- the- Classto reflect, must not be- null
- fieldName- the field name to obtain
- Returns:
- the value of the field
- Throws:
- NullPointerException- if the class is- null, or the field could not be found
- IllegalArgumentException- if the field name is- null, blank or empty, or is not- static
- IllegalAccessException- if the field is not accessible
 
- 
readStaticFieldpublic static Object readStaticField(Class<?> cls, String fieldName, boolean forceAccess) throws IllegalAccessException Reads the namedstaticField. Superclasses will be considered.- Parameters:
- cls- the- Classto reflect, must not be- null
- fieldName- the field name to obtain
- forceAccess- whether to break scope restrictions using the- AccessibleObject.setAccessible(boolean)method.- falsewill only match- publicfields.
- Returns:
- the Field object
- Throws:
- NullPointerException- if the class is- null, or the field could not be found
- IllegalArgumentException- if the field name is- null, blank or empty, or is not- static
- IllegalAccessException- if the field is not made accessible
 
- 
readStaticFieldReads an accessiblestaticField.- Parameters:
- field- to read
- Returns:
- the field value
- Throws:
- NullPointerException- if the field is- null
- IllegalArgumentException- if the field is not- static
- IllegalAccessException- if the field is not accessible
 
- 
readStaticFieldpublic static Object readStaticField(Field field, boolean forceAccess) throws IllegalAccessException Reads a staticField.- Parameters:
- field- to read
- forceAccess- whether to break scope restrictions using the- AccessibleObject.setAccessible(boolean)method.
- Returns:
- the field value
- Throws:
- NullPointerException- if the field is- null
- IllegalArgumentException- if the field is not- static
- IllegalAccessException- if the field is not made accessible
 
- 
removeFinalModifierRemoves the final modifier from aField.- Parameters:
- field- to remove the final modifier
- Throws:
- NullPointerException- if the field is- null
- Since:
- 3.2
 
- 
removeFinalModifierDeprecated.As of Java 12, we can no longer drop thefinalmodifier, thus rendering this method obsolete. The JDK discussion about this change can be found here: https://mail.openjdk.java.net/pipermail/core-libs-dev/2018-November/056486.htmlRemoves the final modifier from aField.- Parameters:
- field- to remove the final modifier
- forceAccess- whether to break scope restrictions using the- AccessibleObject.setAccessible(boolean)method.- falsewill only match- publicfields.
- Throws:
- NullPointerException- if the field is- null
- Since:
- 3.3
 
- 
writeDeclaredFieldpublic static void writeDeclaredField(Object target, String fieldName, Object value) throws IllegalAccessException Writes apublicField. Only the specified class will be considered.- Parameters:
- target- the object to reflect, must not be- null
- fieldName- the field name to obtain
- value- to set
- Throws:
- NullPointerException- if- targetis- null
- IllegalArgumentException- if- fieldNameis- null, blank or empty, or could not be found, or- valueis not assignable
- IllegalAccessException- if the field is not made accessible
 
- 
writeDeclaredFieldpublic static void writeDeclaredField(Object target, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException Writes apublicField. Only the specified class will be considered.- Parameters:
- target- the object to reflect, must not be- null
- fieldName- the field name to obtain
- value- to set
- forceAccess- whether to break scope restrictions using the- AccessibleObject.setAccessible(boolean)method.- falsewill only match- publicfields.
- Throws:
- IllegalArgumentException- if- fieldNameis- null, blank or empty, or could not be found, or- valueis not assignable
- IllegalAccessException- if the field is not made accessible
 
- 
writeDeclaredStaticFieldpublic static void writeDeclaredStaticField(Class<?> cls, String fieldName, Object value) throws IllegalAccessException Writes a namedpublic staticField. Only the specified class will be considered.- Parameters:
- cls-- Classon which the field is to be found
- fieldName- to write
- value- to set
- Throws:
- NullPointerException- if- clsis- nullor the field cannot be located
- IllegalArgumentException- if the field name is- null, blank, empty, not- static, or- valueis not assignable
- IllegalAccessException- if the field is not- publicor is- final
 
- 
writeDeclaredStaticFieldpublic static void writeDeclaredStaticField(Class<?> cls, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException Writes a namedstaticField. Only the specified class will be considered.- Parameters:
- cls-- Classon which the field is to be found
- fieldName- to write
- value- to set
- forceAccess- whether to break scope restrictions using the- AccessibleObject#setAccessible(boolean)method.- falsewill only match- publicfields.
- Throws:
- NullPointerException- if- clsis- nullor the field cannot be located
- IllegalArgumentException- if the field name is- null, blank, empty, not- static, or- valueis not assignable
- IllegalAccessException- if the field is not made accessible or is- final
 
- 
writeFieldpublic static void writeField(Field field, Object target, Object value) throws IllegalAccessException Writes an accessibleField.- Parameters:
- field- to write
- target- the object to call on, may be- nullfor- staticfields
- value- to set
- Throws:
- NullPointerException- if the field is- null
- IllegalArgumentException- if- valueis not assignable
- IllegalAccessException- if the field is not accessible or is- final
 
- 
writeFieldpublic static void writeField(Field field, Object target, Object value, boolean forceAccess) throws IllegalAccessException Writes aField.- Parameters:
- field- to write
- target- the object to call on, may be- nullfor- staticfields
- value- to set
- forceAccess- whether to break scope restrictions using the- AccessibleObject.setAccessible(boolean)method.- falsewill only match- publicfields.
- Throws:
- NullPointerException- if the field is- null
- IllegalArgumentException- if- valueis not assignable
- IllegalAccessException- if the field is not made accessible or is- final
 
- 
writeFieldpublic static void writeField(Object target, String fieldName, Object value) throws IllegalAccessException Writes apublicField. Superclasses will be considered.- Parameters:
- target- the object to reflect, must not be- null
- fieldName- the field name to obtain
- value- to set
- Throws:
- NullPointerException- if- targetis- null
- IllegalArgumentException- if- fieldNameis- null, blank, empty, or could not be found, or- valueis not assignable
- IllegalAccessException- if the field is not accessible
 
- 
writeFieldpublic static void writeField(Object target, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException Writes aField. Superclasses will be considered.- Parameters:
- target- the object to reflect, must not be- null
- fieldName- the field name to obtain
- value- to set
- forceAccess- whether to break scope restrictions using the- AccessibleObject.setAccessible(boolean)method.- falsewill only match- publicfields.
- Throws:
- NullPointerException- if- targetis- null
- IllegalArgumentException- if- fieldNameis- null, blank, empty, or could not be found, or- valueis not assignable
- IllegalAccessException- if the field is not made accessible
 
- 
writeStaticFieldpublic static void writeStaticField(Class<?> cls, String fieldName, Object value) throws IllegalAccessException Writes a namedpublic staticField. Superclasses will be considered.- Parameters:
- cls-- Classon which the field is to be found
- fieldName- to write
- value- to set
- Throws:
- NullPointerException- if- targetis- null
- IllegalArgumentException- if- fieldNameis- null, blank or empty, the field cannot be located or is not- static, or- valueis not assignable
- IllegalAccessException- if the field is not- publicor is- final
 
- 
writeStaticFieldpublic static void writeStaticField(Class<?> cls, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException Writes a namedstaticField. Superclasses will be considered.- Parameters:
- cls-- Classon which the field is to be found
- fieldName- to write
- value- to set
- forceAccess- whether to break scope restrictions using the- AccessibleObject.setAccessible(boolean)method.- falsewill only match- publicfields.
- Throws:
- NullPointerException- if- clsis- nullor the field cannot be located
- IllegalArgumentException- if- fieldNameis- null, blank or empty, the field not- static, or- valueis not assignable
- IllegalAccessException- if the field is not made accessible or is- final
 
- 
writeStaticFieldWrites apublic staticField.- Parameters:
- field- to write
- value- to set
- Throws:
- NullPointerException- if the field is- null
- IllegalArgumentException- if the field is not- static, or- valueis not assignable
- IllegalAccessException- if the field is not- publicor is- final
 
- 
writeStaticFieldpublic static void writeStaticField(Field field, Object value, boolean forceAccess) throws IllegalAccessException Writes a staticField.- Parameters:
- field- to write
- value- to set
- forceAccess- whether to break scope restrictions using the- AccessibleObject.setAccessible(boolean)method.- falsewill only match- publicfields.
- Throws:
- NullPointerException- if the field is- null
- IllegalArgumentException- if the field is not- static, or- valueis not assignable
- IllegalAccessException- if the field is not made accessible or is- final
 
 
-