Package org.apache.commons.lang3.reflect
Class FieldUtils
- java.lang.Object
-
- org.apache.commons.lang3.reflect.FieldUtils
-
public class FieldUtils extends java.lang.Object
Utilities for working withField
s 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()
FieldUtils
instances should NOT be constructed in standard programming.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.lang.reflect.Field[]
getAllFields(java.lang.Class<?> cls)
Gets all fields of the given class and its parents (if any).static java.util.List<java.lang.reflect.Field>
getAllFieldsList(java.lang.Class<?> cls)
Gets all fields of the given class and its parents (if any).static java.lang.reflect.Field
getDeclaredField(java.lang.Class<?> cls, java.lang.String fieldName)
Gets an accessibleField
by name respecting scope.static java.lang.reflect.Field
getDeclaredField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess)
Gets an accessibleField
by name, breaking scope if requested.static java.lang.reflect.Field
getField(java.lang.Class<?> cls, java.lang.String fieldName)
Gets an accessibleField
by name respecting scope.static java.lang.reflect.Field
getField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess)
Gets an accessibleField
by name, breaking scope if requested.static java.util.List<java.lang.reflect.Field>
getFieldsListWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls)
Gets all fields of the given class and its parents (if any) that are annotated with the given annotation.static java.lang.reflect.Field[]
getFieldsWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls)
Gets all fields of the given class and its parents (if any) that are annotated with the given annotation.static java.lang.Object
readDeclaredField(java.lang.Object target, java.lang.String fieldName)
Reads the namedpublic
Field
.static java.lang.Object
readDeclaredField(java.lang.Object target, java.lang.String fieldName, boolean forceAccess)
Gets aField
value by name.static java.lang.Object
readDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName)
Gets the value of astatic
Field
by name.static java.lang.Object
readDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess)
Gets the value of astatic
Field
by name.static java.lang.Object
readField(java.lang.Object target, java.lang.String fieldName)
Reads the namedpublic
Field
.static java.lang.Object
readField(java.lang.Object target, java.lang.String fieldName, boolean forceAccess)
Reads the namedField
.static java.lang.Object
readField(java.lang.reflect.Field field, java.lang.Object target)
Reads an accessibleField
.static java.lang.Object
readField(java.lang.reflect.Field field, java.lang.Object target, boolean forceAccess)
Reads aField
.static java.lang.Object
readStaticField(java.lang.Class<?> cls, java.lang.String fieldName)
Reads the namedpublic static
Field
.static java.lang.Object
readStaticField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess)
Reads the namedstatic
Field
.static java.lang.Object
readStaticField(java.lang.reflect.Field field)
Reads an accessiblestatic
Field
.static java.lang.Object
readStaticField(java.lang.reflect.Field field, boolean forceAccess)
Reads a staticField
.static void
removeFinalModifier(java.lang.reflect.Field field)
Removes the final modifier from aField
.static void
removeFinalModifier(java.lang.reflect.Field field, boolean forceAccess)
Deprecated.As of Java 12, we can no longer drop thefinal
modifier, thus rendering this method obsolete.static void
writeDeclaredField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value)
Writes apublic
Field
.static void
writeDeclaredField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value, boolean forceAccess)
Writes apublic
Field
.static void
writeDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value)
Writes a namedpublic static
Field
.static void
writeDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value, boolean forceAccess)
Writes a namedstatic
Field
.static void
writeField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value)
Writes apublic
Field
.static void
writeField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value, boolean forceAccess)
Writes aField
.static void
writeField(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value)
Writes an accessibleField
.static void
writeField(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value, boolean forceAccess)
Writes aField
.static void
writeStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value)
Writes a namedpublic static
Field
.static void
writeStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value, boolean forceAccess)
Writes a namedstatic
Field
.static void
writeStaticField(java.lang.reflect.Field field, java.lang.Object value)
Writes apublic static
Field
.static void
writeStaticField(java.lang.reflect.Field field, java.lang.Object value, boolean forceAccess)
Writes a staticField
.
-
-
-
Constructor Detail
-
FieldUtils
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.
-
-
Method Detail
-
getField
public static java.lang.reflect.Field getField(java.lang.Class<?> cls, java.lang.String fieldName)
Gets an accessibleField
by name respecting scope. Superclasses/interfaces will be considered.- Parameters:
cls
- theClass
to reflect, must not benull
fieldName
- the field name to obtain- Returns:
- the Field object
- Throws:
java.lang.IllegalArgumentException
- if the class isnull
, or the field name is blank or empty
-
getField
public static java.lang.reflect.Field getField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess)
Gets an accessibleField
by name, breaking scope if requested. Superclasses/interfaces will be considered.- Parameters:
cls
- theClass
to reflect, must not benull
fieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Returns:
- the Field object
- Throws:
java.lang.NullPointerException
- if the class isnull
java.lang.IllegalArgumentException
- if the field name is blank or empty or is matched at multiple places in the inheritance hierarchy
-
getDeclaredField
public static java.lang.reflect.Field getDeclaredField(java.lang.Class<?> cls, java.lang.String fieldName)
Gets an accessibleField
by name respecting scope. Only the specified class will be considered.- Parameters:
cls
- theClass
to reflect, must not benull
fieldName
- the field name to obtain- Returns:
- the Field object
- Throws:
java.lang.IllegalArgumentException
- if the class isnull
, or the field name is blank or empty
-
getDeclaredField
public static java.lang.reflect.Field getDeclaredField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess)
Gets an accessibleField
by name, breaking scope if requested. Only the specified class will be considered.- Parameters:
cls
- theClass
to reflect, must not benull
fieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Returns:
- the Field object
- Throws:
java.lang.IllegalArgumentException
- if the class isnull
, or the field name is blank or empty
-
getAllFields
public static java.lang.reflect.Field[] getAllFields(java.lang.Class<?> cls)
Gets all fields of the given class and its parents (if any).- Parameters:
cls
- theClass
to query- Returns:
- an array of Fields (possibly empty).
- Throws:
java.lang.IllegalArgumentException
- if the class isnull
- Since:
- 3.2
-
getAllFieldsList
public static java.util.List<java.lang.reflect.Field> getAllFieldsList(java.lang.Class<?> cls)
Gets all fields of the given class and its parents (if any).- Parameters:
cls
- theClass
to query- Returns:
- an array of Fields (possibly empty).
- Throws:
java.lang.IllegalArgumentException
- if the class isnull
- Since:
- 3.2
-
getFieldsWithAnnotation
public static java.lang.reflect.Field[] getFieldsWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls)
Gets all fields of the given class and its parents (if any) that are annotated with the given annotation.- Parameters:
cls
- theClass
to queryannotationCls
- theAnnotation
that must be present on a field to be matched- Returns:
- an array of Fields (possibly empty).
- Throws:
java.lang.IllegalArgumentException
- if the class or annotation arenull
- Since:
- 3.4
-
getFieldsListWithAnnotation
public static java.util.List<java.lang.reflect.Field> getFieldsListWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls)
Gets all fields of the given class and its parents (if any) that are annotated with the given annotation.- Parameters:
cls
- theClass
to queryannotationCls
- theAnnotation
that must be present on a field to be matched- Returns:
- a list of Fields (possibly empty).
- Throws:
java.lang.IllegalArgumentException
- if the class or annotation arenull
- Since:
- 3.4
-
readStaticField
public static java.lang.Object readStaticField(java.lang.reflect.Field field) throws java.lang.IllegalAccessException
Reads an accessiblestatic
Field
.- Parameters:
field
- to read- Returns:
- the field value
- Throws:
java.lang.IllegalArgumentException
- if the field isnull
, or notstatic
java.lang.IllegalAccessException
- if the field is not accessible
-
readStaticField
public static java.lang.Object readStaticField(java.lang.reflect.Field field, boolean forceAccess) throws java.lang.IllegalAccessException
Reads a staticField
.- Parameters:
field
- to readforceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.- Returns:
- the field value
- Throws:
java.lang.IllegalArgumentException
- if the field isnull
or notstatic
java.lang.IllegalAccessException
- if the field is not made accessible
-
readStaticField
public static java.lang.Object readStaticField(java.lang.Class<?> cls, java.lang.String fieldName) throws java.lang.IllegalAccessException
Reads the namedpublic static
Field
. Superclasses will be considered.- Parameters:
cls
- theClass
to reflect, must not benull
fieldName
- the field name to obtain- Returns:
- the value of the field
- Throws:
java.lang.IllegalArgumentException
- if the class isnull
, or the field name is blank or empty, is notstatic
, or could not be foundjava.lang.IllegalAccessException
- if the field is not accessible
-
readStaticField
public static java.lang.Object readStaticField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess) throws java.lang.IllegalAccessException
Reads the namedstatic
Field
. Superclasses will be considered.- Parameters:
cls
- theClass
to reflect, must not benull
fieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Returns:
- the Field object
- Throws:
java.lang.IllegalArgumentException
- if the class isnull
, or the field name is blank or empty, is notstatic
, or could not be foundjava.lang.IllegalAccessException
- if the field is not made accessible
-
readDeclaredStaticField
public static java.lang.Object readDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName) throws java.lang.IllegalAccessException
Gets the value of astatic
Field
by name. The field must bepublic
. Only the specified class will be considered.- Parameters:
cls
- theClass
to reflect, must not benull
fieldName
- the field name to obtain- Returns:
- the value of the field
- Throws:
java.lang.IllegalArgumentException
- if the class isnull
, or the field name is blank or empty, is notstatic
, or could not be foundjava.lang.IllegalAccessException
- if the field is not accessible
-
readDeclaredStaticField
public static java.lang.Object readDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess) throws java.lang.IllegalAccessException
Gets the value of astatic
Field
by name. Only the specified class will be considered.- Parameters:
cls
- theClass
to reflect, must not benull
fieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Returns:
- the Field object
- Throws:
java.lang.IllegalArgumentException
- if the class isnull
, or the field name is blank or empty, is notstatic
, or could not be foundjava.lang.IllegalAccessException
- if the field is not made accessible
-
readField
public static java.lang.Object readField(java.lang.reflect.Field field, java.lang.Object target) throws java.lang.IllegalAccessException
Reads an accessibleField
.- Parameters:
field
- the field to usetarget
- the object to call on, may benull
forstatic
fields- Returns:
- the field value
- Throws:
java.lang.IllegalArgumentException
- if the field isnull
java.lang.IllegalAccessException
- if the field is not accessible
-
readField
public static java.lang.Object readField(java.lang.reflect.Field field, java.lang.Object target, boolean forceAccess) throws java.lang.IllegalAccessException
Reads aField
.- Parameters:
field
- the field to usetarget
- the object to call on, may benull
forstatic
fieldsforceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.- Returns:
- the field value
- Throws:
java.lang.IllegalArgumentException
- if the field isnull
java.lang.IllegalAccessException
- if the field is not made accessible
-
readField
public static java.lang.Object readField(java.lang.Object target, java.lang.String fieldName) throws java.lang.IllegalAccessException
Reads the namedpublic
Field
. Superclasses will be considered.- Parameters:
target
- the object to reflect, must not benull
fieldName
- the field name to obtain- Returns:
- the value of the field
- Throws:
java.lang.IllegalArgumentException
- if the class isnull
, or the field name is blank or empty or could not be foundjava.lang.IllegalAccessException
- if the named field is notpublic
-
readField
public static java.lang.Object readField(java.lang.Object target, java.lang.String fieldName, boolean forceAccess) throws java.lang.IllegalAccessException
Reads the namedField
. Superclasses will be considered.- Parameters:
target
- the object to reflect, must not benull
fieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Returns:
- the field value
- Throws:
java.lang.IllegalArgumentException
- iftarget
isnull
, or the field name is blank or empty or could not be foundjava.lang.IllegalAccessException
- if the named field is not made accessible
-
readDeclaredField
public static java.lang.Object readDeclaredField(java.lang.Object target, java.lang.String fieldName) throws java.lang.IllegalAccessException
Reads the namedpublic
Field
. Only the class of the specified object will be considered.- Parameters:
target
- the object to reflect, must not benull
fieldName
- the field name to obtain- Returns:
- the value of the field
- Throws:
java.lang.IllegalArgumentException
- iftarget
isnull
, or the field name is blank or empty or could not be foundjava.lang.IllegalAccessException
- if the named field is notpublic
-
readDeclaredField
public static java.lang.Object readDeclaredField(java.lang.Object target, java.lang.String fieldName, boolean forceAccess) throws java.lang.IllegalAccessException
Gets aField
value by name. Only the class of the specified object will be considered.- Parameters:
target
- the object to reflect, must not benull
fieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only match public fields.- Returns:
- the Field object
- Throws:
java.lang.IllegalArgumentException
- iftarget
isnull
, or the field name is blank or empty or could not be foundjava.lang.IllegalAccessException
- if the field is not made accessible
-
writeStaticField
public static void writeStaticField(java.lang.reflect.Field field, java.lang.Object value) throws java.lang.IllegalAccessException
Writes apublic static
Field
.- Parameters:
field
- to writevalue
- to set- Throws:
java.lang.IllegalArgumentException
- if the field isnull
or notstatic
, orvalue
is not assignablejava.lang.IllegalAccessException
- if the field is notpublic
or isfinal
-
writeStaticField
public static void writeStaticField(java.lang.reflect.Field field, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
Writes a staticField
.- Parameters:
field
- to writevalue
- to setforceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Throws:
java.lang.IllegalArgumentException
- if the field isnull
or notstatic
, orvalue
is not assignablejava.lang.IllegalAccessException
- if the field is not made accessible or isfinal
-
writeStaticField
public static void writeStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalAccessException
Writes a namedpublic static
Field
. Superclasses will be considered.- Parameters:
cls
-Class
on which the field is to be foundfieldName
- to writevalue
- to set- Throws:
java.lang.IllegalArgumentException
- ifcls
isnull
, the field name is blank or empty, the field cannot be located or is notstatic
, orvalue
is not assignablejava.lang.IllegalAccessException
- if the field is notpublic
or isfinal
-
writeStaticField
public static void writeStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
Writes a namedstatic
Field
. Superclasses will be considered.- Parameters:
cls
-Class
on which the field is to be foundfieldName
- to writevalue
- to setforceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Throws:
java.lang.IllegalArgumentException
- ifcls
isnull
, the field name is blank or empty, the field cannot be located or is notstatic
, orvalue
is not assignablejava.lang.IllegalAccessException
- if the field is not made accessible or isfinal
-
writeDeclaredStaticField
public static void writeDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalAccessException
Writes a namedpublic static
Field
. Only the specified class will be considered.- Parameters:
cls
-Class
on which the field is to be foundfieldName
- to writevalue
- to set- Throws:
java.lang.IllegalArgumentException
- ifcls
isnull
, the field name is blank or empty, the field cannot be located or is notstatic
, orvalue
is not assignablejava.lang.IllegalAccessException
- if the field is notpublic
or isfinal
-
writeDeclaredStaticField
public static void writeDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
Writes a namedstatic
Field
. Only the specified class will be considered.- Parameters:
cls
-Class
on which the field is to be foundfieldName
- to writevalue
- to setforceAccess
- whether to break scope restrictions using theAccessibleObject#setAccessible(boolean)
method.false
will only matchpublic
fields.- Throws:
java.lang.IllegalArgumentException
- ifcls
isnull
, the field name is blank or empty, the field cannot be located or is notstatic
, orvalue
is not assignablejava.lang.IllegalAccessException
- if the field is not made accessible or isfinal
-
writeField
public static void writeField(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value) throws java.lang.IllegalAccessException
Writes an accessibleField
.- Parameters:
field
- to writetarget
- the object to call on, may benull
forstatic
fieldsvalue
- to set- Throws:
java.lang.IllegalAccessException
- if the field or target isnull
, the field is not accessible or isfinal
, orvalue
is not assignable
-
writeField
public static void writeField(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
Writes aField
.- Parameters:
field
- to writetarget
- the object to call on, may benull
forstatic
fieldsvalue
- to setforceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Throws:
java.lang.IllegalArgumentException
- if the field isnull
orvalue
is not assignablejava.lang.IllegalAccessException
- if the field is not made accessible or isfinal
-
removeFinalModifier
public static void removeFinalModifier(java.lang.reflect.Field field)
Removes the final modifier from aField
.- Parameters:
field
- to remove the final modifier- Throws:
java.lang.IllegalArgumentException
- if the field isnull
- Since:
- 3.2
-
removeFinalModifier
@Deprecated public static void removeFinalModifier(java.lang.reflect.Field field, boolean forceAccess)
Deprecated.As of Java 12, we can no longer drop thefinal
modifier, 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.false
will only matchpublic
fields.- Throws:
java.lang.IllegalArgumentException
- if the field isnull
- Since:
- 3.3
-
writeField
public static void writeField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalAccessException
Writes apublic
Field
. Superclasses will be considered.- Parameters:
target
- the object to reflect, must not benull
fieldName
- the field name to obtainvalue
- to set- Throws:
java.lang.IllegalArgumentException
- iftarget
isnull
,fieldName
is blank or empty or could not be found, orvalue
is not assignablejava.lang.IllegalAccessException
- if the field is not accessible
-
writeField
public static void writeField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
Writes aField
. Superclasses will be considered.- Parameters:
target
- the object to reflect, must not benull
fieldName
- the field name to obtainvalue
- to setforceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Throws:
java.lang.IllegalArgumentException
- iftarget
isnull
,fieldName
is blank or empty or could not be found, orvalue
is not assignablejava.lang.IllegalAccessException
- if the field is not made accessible
-
writeDeclaredField
public static void writeDeclaredField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalAccessException
Writes apublic
Field
. Only the specified class will be considered.- Parameters:
target
- the object to reflect, must not benull
fieldName
- the field name to obtainvalue
- to set- Throws:
java.lang.IllegalArgumentException
- iftarget
isnull
,fieldName
is blank or empty or could not be found, orvalue
is not assignablejava.lang.IllegalAccessException
- if the field is not made accessible
-
writeDeclaredField
public static void writeDeclaredField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
Writes apublic
Field
. Only the specified class will be considered.- Parameters:
target
- the object to reflect, must not benull
fieldName
- the field name to obtainvalue
- to setforceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Throws:
java.lang.IllegalArgumentException
- iftarget
isnull
,fieldName
is blank or empty or could not be found, orvalue
is not assignablejava.lang.IllegalAccessException
- if the field is not made accessible
-
-