Package org.apache.commons.lang3.reflect
Class FieldUtils
- java.lang.Object
-
- org.apache.commons.lang3.reflect.FieldUtils
-
public class FieldUtils extends Object
Utilities for working withFields 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 Summary
Constructors Constructor Description FieldUtils()FieldUtilsinstances should NOT be constructed in standard programming.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method 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 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 FieldgetField(Class<?> cls, String fieldName)Gets an accessibleFieldby name respecting scope.static FieldgetField(Class<?> cls, String fieldName, boolean forceAccess)Gets an accessibleFieldby 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 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 ObjectreadField(Object target, String fieldName)Reads the namedpublicField.static ObjectreadField(Object target, String fieldName, boolean forceAccess)Reads the namedField.static ObjectreadField(Field field, Object target)Reads an accessibleField.static ObjectreadField(Field field, Object target, boolean forceAccess)Reads 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 Detail
-
FieldUtils
public FieldUtils()
FieldUtilsinstances should NOT be constructed in standard programming.This constructor is
publicto permit tools that require a JavaBean instance to operate.
-
-
Method Detail
-
getField
public static Field getField(Class<?> cls, String fieldName)
Gets an accessibleFieldby name respecting scope. Superclasses/interfaces will be considered.- Parameters:
cls- theClassto reflect, must not benullfieldName- the field name to obtain- Returns:
- the Field object
- Throws:
IllegalArgumentException- if the class isnull, or the field name is blank or empty
-
getField
public static Field getField(Class<?> cls, String fieldName, boolean forceAccess)
Gets an accessibleFieldby name, breaking scope if requested. Superclasses/interfaces will be considered.- Parameters:
cls- theClassto reflect, must not benullfieldName- the field name to obtainforceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Returns:
- the Field object
- Throws:
NullPointerException- if the class isnullIllegalArgumentException- if the field name is blank or empty or is matched at multiple places in the inheritance hierarchy
-
getDeclaredField
public static Field getDeclaredField(Class<?> cls, String fieldName)
Gets an accessibleFieldby name respecting scope. Only the specified class will be considered.- Parameters:
cls- theClassto reflect, must not benullfieldName- the field name to obtain- Returns:
- the Field object
- Throws:
IllegalArgumentException- if the class isnull, or the field name is blank or empty
-
getDeclaredField
public static Field getDeclaredField(Class<?> cls, String fieldName, boolean forceAccess)
Gets an accessibleFieldby name, breaking scope if requested. Only the specified class will be considered.- Parameters:
cls- theClassto reflect, must not benullfieldName- the field name to obtainforceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Returns:
- the Field object
- Throws:
IllegalArgumentException- if the class isnull, or the field name is blank or empty
-
getAllFields
public static Field[] getAllFields(Class<?> cls)
Gets all fields of the given class and its parents (if any).- Parameters:
cls- theClassto query- Returns:
- an array of Fields (possibly empty).
- Throws:
IllegalArgumentException- if the class isnull- Since:
- 3.2
-
getAllFieldsList
public static List<Field> getAllFieldsList(Class<?> cls)
Gets all fields of the given class and its parents (if any).- Parameters:
cls- theClassto query- Returns:
- an array of Fields (possibly empty).
- Throws:
IllegalArgumentException- if the class isnull- Since:
- 3.2
-
getFieldsWithAnnotation
public 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- theClassto queryannotationCls- theAnnotationthat must be present on a field to be matched- Returns:
- an array of Fields (possibly empty).
- Throws:
IllegalArgumentException- if the class or annotation arenull- Since:
- 3.4
-
getFieldsListWithAnnotation
public 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- theClassto queryannotationCls- theAnnotationthat must be present on a field to be matched- Returns:
- a list of Fields (possibly empty).
- Throws:
IllegalArgumentException- if the class or annotation arenull- Since:
- 3.4
-
readStaticField
public static Object readStaticField(Field field) throws IllegalAccessException
Reads an accessiblestaticField.- Parameters:
field- to read- Returns:
- the field value
- Throws:
IllegalArgumentException- if the field isnull, or notstaticIllegalAccessException- if the field is not accessible
-
readStaticField
public static Object readStaticField(Field field, boolean forceAccess) throws IllegalAccessException
Reads a staticField.- Parameters:
field- to readforceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.- Returns:
- the field value
- Throws:
IllegalArgumentException- if the field isnullor notstaticIllegalAccessException- if the field is not made accessible
-
readStaticField
public static Object readStaticField(Class<?> cls, String fieldName) throws IllegalAccessException
Reads the namedpublic staticField. Superclasses will be considered.- Parameters:
cls- theClassto reflect, must not benullfieldName- the field name to obtain- Returns:
- the value of the field
- Throws:
IllegalArgumentException- if the class isnull, or the field name is blank or empty, is notstatic, or could not be foundIllegalAccessException- if the field is not accessible
-
readStaticField
public static Object readStaticField(Class<?> cls, String fieldName, boolean forceAccess) throws IllegalAccessException
Reads the namedstaticField. Superclasses will be considered.- Parameters:
cls- theClassto reflect, must not benullfieldName- the field name to obtainforceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Returns:
- the Field object
- Throws:
IllegalArgumentException- if the class isnull, or the field name is blank or empty, is notstatic, or could not be foundIllegalAccessException- if the field is not made accessible
-
readDeclaredStaticField
public 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- theClassto reflect, must not benullfieldName- the field name to obtain- Returns:
- the value of the field
- Throws:
IllegalArgumentException- if the class isnull, or the field name is blank or empty, is notstatic, or could not be foundIllegalAccessException- if the field is not accessible
-
readDeclaredStaticField
public 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- theClassto reflect, must not benullfieldName- the field name to obtainforceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Returns:
- the Field object
- Throws:
IllegalArgumentException- if the class isnull, or the field name is blank or empty, is notstatic, or could not be foundIllegalAccessException- if the field is not made accessible
-
readField
public static Object readField(Field field, Object target) throws IllegalAccessException
Reads an accessibleField.- Parameters:
field- the field to usetarget- the object to call on, may benullforstaticfields- Returns:
- the field value
- Throws:
IllegalArgumentException- if the field isnullIllegalAccessException- if the field is not accessible
-
readField
public static Object readField(Field field, Object target, boolean forceAccess) throws IllegalAccessException
Reads aField.- Parameters:
field- the field to usetarget- the object to call on, may benullforstaticfieldsforceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.- Returns:
- the field value
- Throws:
IllegalArgumentException- if the field isnullIllegalAccessException- if the field is not made accessible
-
readField
public static Object readField(Object target, String fieldName) throws IllegalAccessException
Reads the namedpublicField. Superclasses will be considered.- Parameters:
target- the object to reflect, must not benullfieldName- the field name to obtain- Returns:
- the value of the field
- Throws:
IllegalArgumentException- if the class isnull, or the field name is blank or empty or could not be foundIllegalAccessException- if the named field is notpublic
-
readField
public 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 benullfieldName- the field name to obtainforceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Returns:
- the field value
- Throws:
IllegalArgumentException- iftargetisnull, or the field name is blank or empty or could not be foundIllegalAccessException- if the named field is not made accessible
-
readDeclaredField
public 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 benullfieldName- the field name to obtain- Returns:
- the value of the field
- Throws:
IllegalArgumentException- iftargetisnull, or the field name is blank or empty or could not be foundIllegalAccessException- if the named field is notpublic
-
readDeclaredField
public 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 benullfieldName- the field name to obtainforceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only match public fields.- Returns:
- the Field object
- Throws:
IllegalArgumentException- iftargetisnull, or the field name is blank or empty or could not be foundIllegalAccessException- if the field is not made accessible
-
writeStaticField
public static void writeStaticField(Field field, Object value) throws IllegalAccessException
Writes apublic staticField.- Parameters:
field- to writevalue- to set- Throws:
IllegalArgumentException- if the field isnullor notstatic, orvalueis not assignableIllegalAccessException- if the field is notpublicor isfinal
-
writeStaticField
public static void writeStaticField(Field field, Object value, boolean forceAccess) throws IllegalAccessException
Writes a staticField.- Parameters:
field- to writevalue- to setforceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Throws:
IllegalArgumentException- if the field isnullor notstatic, orvalueis not assignableIllegalAccessException- if the field is not made accessible or isfinal
-
writeStaticField
public 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 foundfieldName- to writevalue- to set- Throws:
IllegalArgumentException- ifclsisnull, the field name is blank or empty, the field cannot be located or is notstatic, orvalueis not assignableIllegalAccessException- if the field is notpublicor isfinal
-
writeStaticField
public 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 foundfieldName- to writevalue- to setforceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Throws:
IllegalArgumentException- ifclsisnull, the field name is blank or empty, the field cannot be located or is notstatic, orvalueis not assignableIllegalAccessException- if the field is not made accessible or isfinal
-
writeDeclaredStaticField
public 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 foundfieldName- to writevalue- to set- Throws:
IllegalArgumentException- ifclsisnull, the field name is blank or empty, the field cannot be located or is notstatic, orvalueis not assignableIllegalAccessException- if the field is notpublicor isfinal
-
writeDeclaredStaticField
public 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 foundfieldName- to writevalue- to setforceAccess- whether to break scope restrictions using theAccessibleObject#setAccessible(boolean)method.falsewill only matchpublicfields.- Throws:
IllegalArgumentException- ifclsisnull, the field name is blank or empty, the field cannot be located or is notstatic, orvalueis not assignableIllegalAccessException- if the field is not made accessible or isfinal
-
writeField
public static void writeField(Field field, Object target, Object value) throws IllegalAccessException
Writes an accessibleField.- Parameters:
field- to writetarget- the object to call on, may benullforstaticfieldsvalue- to set- Throws:
IllegalAccessException- if the field or target isnull, the field is not accessible or isfinal, orvalueis not assignable
-
writeField
public static void writeField(Field field, Object target, Object value, boolean forceAccess) throws IllegalAccessException
Writes aField.- Parameters:
field- to writetarget- the object to call on, may benullforstaticfieldsvalue- to setforceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Throws:
IllegalArgumentException- if the field isnullorvalueis not assignableIllegalAccessException- if the field is not made accessible or isfinal
-
removeFinalModifier
public static void removeFinalModifier(Field field)
Removes the final modifier from aField.- Parameters:
field- to remove the final modifier- Throws:
IllegalArgumentException- if the field isnull- Since:
- 3.2
-
removeFinalModifier
@Deprecated public static void removeFinalModifier(Field field, boolean forceAccess)
Deprecated.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: http://mail.openjdk.java.net/pipermail/core-libs-dev/2018-November/056486.htmlRemoves the final modifier from aField.- Parameters:
field- to remove the final modifierforceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Throws:
IllegalArgumentException- if the field isnull- Since:
- 3.3
-
writeField
public 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 benullfieldName- the field name to obtainvalue- to set- Throws:
IllegalArgumentException- iftargetisnull,fieldNameis blank or empty or could not be found, orvalueis not assignableIllegalAccessException- if the field is not accessible
-
writeField
public 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 benullfieldName- the field name to obtainvalue- to setforceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Throws:
IllegalArgumentException- iftargetisnull,fieldNameis blank or empty or could not be found, orvalueis not assignableIllegalAccessException- if the field is not made accessible
-
writeDeclaredField
public 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 benullfieldName- the field name to obtainvalue- to set- Throws:
IllegalArgumentException- iftargetisnull,fieldNameis blank or empty or could not be found, orvalueis not assignableIllegalAccessException- if the field is not made accessible
-
writeDeclaredField
public 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 benullfieldName- the field name to obtainvalue- to setforceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Throws:
IllegalArgumentException- iftargetisnull,fieldNameis blank or empty or could not be found, orvalueis not assignableIllegalAccessException- if the field is not made accessible
-
-