Package org.apache.commons.lang3.reflect
Class FieldUtils
java.lang.Object
org.apache.commons.lang3.reflect.FieldUtils
Utilities for working with
Field
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 -
Method Summary
Modifier 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 Field
getDeclaredField
(Class<?> cls, String fieldName) Gets an accessibleField
by name respecting scope.static Field
getDeclaredField
(Class<?> cls, String fieldName, boolean forceAccess) Gets an accessibleField
by name, breaking scope if requested.static Field
Gets an accessibleField
by name respecting scope.static Field
Gets an accessibleField
by 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 Object
readDeclaredField
(Object target, String fieldName) Reads the namedpublic
Field
.static Object
readDeclaredField
(Object target, String fieldName, boolean forceAccess) Gets aField
value by name.static Object
readDeclaredStaticField
(Class<?> cls, String fieldName) Gets the value of astatic
Field
by name.static Object
readDeclaredStaticField
(Class<?> cls, String fieldName, boolean forceAccess) Gets the value of astatic
Field
by name.static Object
Reads the namedpublic
Field
.static Object
Reads the namedField
.static Object
Reads an accessibleField
.static Object
Reads aField
.static Object
readStaticField
(Class<?> cls, String fieldName) Reads the namedpublic static
Field
.static Object
readStaticField
(Class<?> cls, String fieldName, boolean forceAccess) Reads the namedstatic
Field
.static Object
readStaticField
(Field field) Reads an accessiblestatic
Field
.static Object
readStaticField
(Field field, boolean forceAccess) Reads a staticField
.static void
removeFinalModifier
(Field field) Removes the final modifier from aField
.static void
removeFinalModifier
(Field field, boolean forceAccess) Deprecated.As of Java 12, we can no longer drop thefinal
modifier, thus rendering this method obsolete.static void
writeDeclaredField
(Object target, String fieldName, Object value) Writes apublic
Field
.static void
writeDeclaredField
(Object target, String fieldName, Object value, boolean forceAccess) Writes apublic
Field
.static void
writeDeclaredStaticField
(Class<?> cls, String fieldName, Object value) Writes a namedpublic static
Field
.static void
writeDeclaredStaticField
(Class<?> cls, String fieldName, Object value, boolean forceAccess) Writes a namedstatic
Field
.static void
writeField
(Object target, String fieldName, Object value) Writes apublic
Field
.static void
writeField
(Object target, String fieldName, Object value, boolean forceAccess) Writes aField
.static void
writeField
(Field field, Object target, Object value) Writes an accessibleField
.static void
writeField
(Field field, Object target, Object value, boolean forceAccess) Writes aField
.static void
writeStaticField
(Class<?> cls, String fieldName, Object value) Writes a namedpublic static
Field
.static void
writeStaticField
(Class<?> cls, String fieldName, Object value, boolean forceAccess) Writes a namedstatic
Field
.static void
writeStaticField
(Field field, Object value) Writes apublic static
Field
.static void
writeStaticField
(Field field, Object value, boolean forceAccess) Writes a staticField
.
-
Constructor Details
-
FieldUtils
Deprecated.TODO Make private in 4.0.FieldUtils
instances should NOT be constructed in standard programming.This constructor is
public
to permit tools that require a JavaBean instance to operate.
-
-
Method Details
-
getAllFields
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:
NullPointerException
- if the class isnull
.- Since:
- 3.2
-
getAllFieldsList
Gets all fields of the given class and its parents (if any).- Parameters:
cls
- theClass
to query- Returns:
- a list of Fields (possibly empty).
- Throws:
NullPointerException
- if the class isnull
.- Since:
- 3.2
-
getDeclaredField
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:
NullPointerException
- if the class isnull
.IllegalArgumentException
- if the field name isnull
, blank, or empty.SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
getDeclaredField
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 obtain.forceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Returns:
- the Field object
- Throws:
NullPointerException
- if the class isnull
.IllegalArgumentException
- if the field name isnull
, blank, or empty.SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
getField
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:
NullPointerException
- if the class isnull
.IllegalArgumentException
- if the field name isnull
, blank, or emptySecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
getField
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 obtain.forceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Returns:
- the Field object.
- Throws:
NullPointerException
- if the class isnull
.IllegalArgumentException
- if the field name is blank or empty or is matched at multiple places in the inheritance hierarchy.SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
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
- theClass
to query.annotationCls
- theAnnotation
that must be present on a field to be matched.- Returns:
- a list of Fields (possibly empty).
- Throws:
NullPointerException
- if the class or annotation arenull
.- Since:
- 3.4
-
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
- theClass
to query.annotationCls
- theAnnotation
that must be present on a field to be matched- Returns:
- an array of Fields (possibly empty).
- Throws:
NullPointerException
- if the class or annotation arenull
.- Since:
- 3.4
-
readDeclaredField
public static Object readDeclaredField(Object target, String fieldName) throws 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:
NullPointerException
- iftarget
isnull
.IllegalArgumentException
- iffieldName
isnull
, blank or empty, or could not be found.IllegalAccessException
- if the named field is notpublic
SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
readDeclaredField
public static Object readDeclaredField(Object target, String fieldName, boolean forceAccess) throws 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 obtain.forceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only match public fields.- Returns:
- the Field object.
- Throws:
NullPointerException
- iftarget
isnull
.IllegalArgumentException
- iffieldName
isnull
, blank or empty, or could not be found.IllegalAccessException
- if the field is not made accessible.SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
readDeclaredStaticField
public static Object readDeclaredStaticField(Class<?> cls, String fieldName) throws 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:
NullPointerException
- if the class isnull
, or the field could not be found.IllegalArgumentException
- if the field name isnull
, blank, empty, or is notstatic
.IllegalAccessException
- if the field is not accessibleSecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
readDeclaredStaticField
public static Object readDeclaredStaticField(Class<?> cls, String fieldName, boolean forceAccess) throws 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 obtain.forceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Returns:
- the Field object
- Throws:
NullPointerException
- if the class isnull
, or the field could not be found.IllegalArgumentException
- if the field name is blank or empty, is notstatic
.IllegalAccessException
- if the field is not made accessibleSecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
readField
Reads an accessibleField
.- Parameters:
field
- the field to use.target
- the object to call on, may benull
forstatic
fields.- Returns:
- the field value
- Throws:
NullPointerException
- if the field isnull
.IllegalAccessException
- if the field is not accessible.SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
readField
public 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 benull
forstatic
fields.forceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.- Returns:
- the field value
- Throws:
NullPointerException
- if the field isnull
.IllegalAccessException
- if the field is not made accessible.SecurityException
- if an underlying accessible object's method denies the request.SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
readField
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:
NullPointerException
- if the target isnull
.IllegalArgumentException
- if the field name isnull
, blank, empty, or could not be found.IllegalAccessException
- if the named field is notpublic
.SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
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 benull
.fieldName
- the field name to obtain.forceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Returns:
- the field value
- Throws:
NullPointerException
- iftarget
isnull
.IllegalArgumentException
- if the field name isnull
, blank, empty, or could not be found.IllegalAccessException
- if the named field is not made accessible.SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
readStaticField
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:
NullPointerException
- if the class isnull
, or the field could not be found.IllegalArgumentException
- if the field name isnull
, blank or empty, or is notstatic
.IllegalAccessException
- if the field is not accessible.SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
readStaticField
public static Object readStaticField(Class<?> cls, String fieldName, boolean forceAccess) throws IllegalAccessException Reads the namedstatic
Field
. Superclasses will be considered.- Parameters:
cls
- theClass
to reflect, must not benull
.fieldName
- the field name to obtain.forceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Returns:
- the Field object.
- Throws:
NullPointerException
- if the class isnull
, or the field could not be found.IllegalArgumentException
- if the field name isnull
, blank or empty, or is notstatic
.IllegalAccessException
- if the field is not made accessible.SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
readStaticField
Reads an accessiblestatic
Field
.- Parameters:
field
- to read.- Returns:
- the field value.
- Throws:
NullPointerException
- if the field isnull
.IllegalArgumentException
- if the field is notstatic
.IllegalAccessException
- if the field is not accessibleSecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
readStaticField
public static Object readStaticField(Field field, boolean forceAccess) throws IllegalAccessException Reads a staticField
.- Parameters:
field
- to read.forceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.- Returns:
- the field value.
- Throws:
NullPointerException
- if the field isnull
.IllegalArgumentException
- if the field is notstatic
.IllegalAccessException
- if the field is not made accessible.SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
removeFinalModifier
Removes the final modifier from aField
.- Parameters:
field
- to remove the final modifier.- Throws:
NullPointerException
- if the field isnull
.SecurityException
- if an underlying accessible object's method denies the request.- Since:
- 3.2
- See Also:
-
removeFinalModifier
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: 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 theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Throws:
NullPointerException
- if the field isnull
.SecurityException
- if an underlying accessible object's method denies the request.- Since:
- 3.3
- See Also:
-
writeDeclaredField
public static void writeDeclaredField(Object target, String fieldName, Object value) throws 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 obtain.value
- the new value.- Throws:
NullPointerException
- iftarget
isnull
.IllegalArgumentException
- iffieldName
isnull
, blank or empty, or could not be found, orvalue
is not assignable.IllegalAccessException
- if the field is not made accessibleSecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
writeDeclaredField
public static void writeDeclaredField(Object target, String fieldName, Object value, boolean forceAccess) throws 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 obtain.value
- the new value.forceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Throws:
IllegalArgumentException
- iffieldName
isnull
, blank or empty, or could not be found, orvalue
is not assignableIllegalAccessException
- if the field is not made accessibleSecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
writeDeclaredStaticField
public static void writeDeclaredStaticField(Class<?> cls, String fieldName, Object value) throws IllegalAccessException Writes a namedpublic static
Field
. Only the specified class will be considered.- Parameters:
cls
-Class
on which the field is to be found.fieldName
- to write.value
- the new value.- Throws:
NullPointerException
- ifcls
isnull
or the field cannot be located.IllegalArgumentException
- if the field name isnull
, blank, empty, notstatic
, orvalue
is not assignable.IllegalAccessException
- if the field is notpublic
or isfinal
SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
writeDeclaredStaticField
public static void writeDeclaredStaticField(Class<?> cls, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException Writes a namedstatic
Field
. Only the specified class will be considered.- Parameters:
cls
-Class
on which the field is to be found.fieldName
- to writevalue
- the new value.forceAccess
- whether to break scope restrictions using theAccessibleObject#setAccessible(boolean)
method.false
will only matchpublic
fields.- Throws:
NullPointerException
- ifcls
isnull
or the field cannot be located.IllegalArgumentException
- if the field name isnull
, blank, empty, notstatic
, orvalue
is not assignable.IllegalAccessException
- if the field is not made accessible or isfinal
SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
writeField
public 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 benull
forstatic
fields.value
- the new value.- Throws:
NullPointerException
- if the field isnull
.IllegalArgumentException
- ifvalue
is not assignable.IllegalAccessException
- if the field is not accessible or isfinal
.SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
writeField
public 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 benull
forstatic
fieldsvalue
- the new value.forceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Throws:
NullPointerException
- if the field isnull
.IllegalArgumentException
- ifvalue
is not assignable.IllegalAccessException
- if the field is not made accessible or isfinal
SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
writeField
public static void writeField(Object target, String fieldName, Object value) throws IllegalAccessException Writes apublic
Field
. Superclasses will be considered.- Parameters:
target
- the object to reflect, must not benull
.fieldName
- the field name to obtain.value
- the new value.- Throws:
NullPointerException
- iftarget
isnull
.IllegalArgumentException
- iffieldName
isnull
, blank, empty, or could not be found, orvalue
is not assignable.IllegalAccessException
- if the field is not accessible.SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
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 benull
.fieldName
- the field name to obtain.value
- the new value.forceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Throws:
NullPointerException
- iftarget
isnull
.IllegalArgumentException
- iffieldName
isnull
, blank, empty, or could not be found, orvalue
is not assignable.IllegalAccessException
- if the field is not made accessible.SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
writeStaticField
public static void writeStaticField(Class<?> cls, String fieldName, Object value) throws IllegalAccessException Writes a namedpublic static
Field
. Superclasses will be considered.- Parameters:
cls
-Class
on which the field is to be found.fieldName
- to write.value
- the new value.- Throws:
NullPointerException
- iftarget
isnull
.IllegalArgumentException
- iffieldName
isnull
, blank or empty, the field cannot be located or is notstatic
, orvalue
is not assignable.IllegalAccessException
- if the field is notpublic
or isfinal
SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
writeStaticField
public static void writeStaticField(Class<?> cls, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException Writes a namedstatic
Field
. Superclasses will be considered.- Parameters:
cls
-Class
on which the field is to be found.fieldName
- to write.value
- the new value.forceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Throws:
NullPointerException
- ifcls
isnull
or the field cannot be located.IllegalArgumentException
- iffieldName
isnull
, blank or empty, the field notstatic
, orvalue
is not assignable.IllegalAccessException
- if the field is not made accessible or isfinal
.SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
writeStaticField
Writes apublic static
Field
.- Parameters:
field
- to write.value
- the new value.- Throws:
NullPointerException
- if the field isnull
.IllegalArgumentException
- if the field is notstatic
, orvalue
is not assignable.IllegalAccessException
- if the field is notpublic
or isfinal
.SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-
writeStaticField
public static void writeStaticField(Field field, Object value, boolean forceAccess) throws IllegalAccessException Writes a staticField
.- Parameters:
field
- to write.value
- the new value.forceAccess
- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)
method.false
will only matchpublic
fields.- Throws:
NullPointerException
- if the field isnull
.IllegalArgumentException
- if the field is notstatic
, orvalue
is not assignable.IllegalAccessException
- if the field is not made accessible or isfinal
SecurityException
- if an underlying accessible object's method denies the request.- See Also:
-