FieldUtils
- public class org.apache.commons.lang3.reflect.FieldUtils[source]
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
Constructors
Deprecated. |
- @Deprecated public FieldUtils()[source]
Deprecated: 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.
Methods
Gets all fields of the given class and its parents (if any). |
|
Gets all fields of the given class and its parents (if any). |
|
Gets an accessible |
|
Gets an accessible |
|
Gets an accessible |
|
Gets an accessible |
|
Gets all fields of the given class and its parents (if any) that are annotated with the given annotation. |
|
Gets all fields of the given class and its parents (if any) that are annotated with the given annotation. |
|
Reads the named |
|
Gets a |
|
Gets the value of a |
|
Gets the value of a |
|
Reads the named |
|
Reads the named |
|
Reads an accessible |
|
Reads a |
|
Reads the named |
|
Reads the named |
|
Reads an accessible |
|
Reads a static |
|
Removes the final modifier from a |
|
Deprecated. Removes the final modifier from a |
|
Writes a |
|
Writes a |
|
Writes a named |
|
Writes a named |
|
Writes a |
|
Writes a |
|
Writes an accessible |
|
Writes a |
|
Writes a named |
|
Writes a named |
|
Writes a |
|
Writes a static |
- public static Field[] getAllFields(Class<?> cls)[source]
Gets all fields of the given class and its parents (if any).
- Parameters:
cls – the
Classto query- Returns:
An array of Fields (possibly empty).
- Throws:
java.lang.NullPointerException – Thrown if the class is
null.- Since:
3.2
- public static List<Field> getAllFieldsList(Class<?> cls)[source]
Gets all fields of the given class and its parents (if any).
- Parameters:
cls – the
Classto query- Returns:
A list of Fields (possibly empty).
- Throws:
java.lang.NullPointerException – Thrown if the class is
null.- Since:
3.2
- public static Field getDeclaredField(Class<?> cls, String fieldName)[source]
Gets an accessible
Fieldby name respecting scope. Only the specified class will be considered.- Parameters:
cls – the
Classto reflect, must not benullfieldName – the field name to obtain.
- Returns:
The Field object.
- Throws:
java.lang.NullPointerException – Thrown if the class is
null.java.lang.IllegalArgumentException – Thrown if the field name is
null, blank, or empty.java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static Field getDeclaredField(Class<?> cls, String fieldName, boolean forceAccess)[source]
Gets an accessible
Fieldby name, breaking scope if requested. Only the specified class will be considered.- Parameters:
cls – the
Classto reflect, must not benull.fieldName – the field name to obtain.
forceAccess – whether to break scope restrictions using the
AccessibleObject#setAccessible(boolean)method.falsewill only matchpublicfields.
- Returns:
The Field object
- Throws:
java.lang.NullPointerException – Thrown if the class is
null.java.lang.IllegalArgumentException – Thrown if the field name is
null, blank, or empty.java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static Field getField(Class<?> cls, String fieldName)[source]
Gets an accessible
Fieldby name respecting scope. Superclasses/interfaces will be considered.- Parameters:
cls – the
Classto reflect, must not benull.fieldName – the field name to obtain.
- Returns:
The Field object.
- Throws:
java.lang.NullPointerException – Thrown if the class is
null.java.lang.IllegalArgumentException – Thrown if the field name is
null, blank, or empty.java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static Field getField(Class<?> cls, String fieldName, boolean forceAccess)[source]
Gets an accessible
Fieldby name, breaking scope if requested. Superclasses/interfaces will be considered.- Parameters:
cls – the
Classto reflect, must not benull.fieldName – the field name to obtain.
forceAccess – whether to break scope restrictions using the
AccessibleObject#setAccessible(boolean)method.falsewill only matchpublicfields.
- Returns:
The Field object.
- Throws:
java.lang.NullPointerException – Thrown if the class is
null.java.lang.IllegalArgumentException – Thrown if the field name is blank or empty or is matched at multiple places in the inheritance hierarchy.
java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static List<Field> getFieldsListWithAnnotation(Class<?> cls, Class<? extends Annotation> annotationCls)[source]
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:
java.lang.NullPointerException – Thrown if the class or annotation are
null.- Since:
3.4
- public static Field[] getFieldsWithAnnotation(Class<?> cls, Class<? extends Annotation> annotationCls)[source]
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:
java.lang.NullPointerException – Thrown if the class or annotation are
null.- Since:
3.4
- public static Object readDeclaredField(Object target, String fieldName) throws IllegalAccessException[source]
Reads the named
publicField. 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:
java.lang.NullPointerException – Thrown if
targetisnull.java.lang.IllegalArgumentException – Thrown if
fieldNameisnull, blank or empty, or could not be found.java.lang.IllegalAccessException – Thrown if the named field is not
public.java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static Object readDeclaredField(Object target, String fieldName, boolean forceAccess) throws IllegalAccessException[source]
Gets a
Fieldvalue 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:
java.lang.NullPointerException – Thrown if
targetisnull.java.lang.IllegalArgumentException – Thrown if
fieldNameisnull, blank or empty, or could not be found.java.lang.IllegalAccessException – Thrown if the field is not made accessible.
java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static Object readDeclaredStaticField(Class<?> cls, String fieldName) throws IllegalAccessException[source]
Gets the value of a
staticFieldby name. The field must bepublic. Only the specified class will be considered.- Parameters:
cls – the
Classto reflect, must not benull.fieldName – the field name to obtain.
- Returns:
The value of the field.
- Throws:
java.lang.NullPointerException – Thrown if the class is
null, or the field could not be found.java.lang.IllegalArgumentException – Thrown if the field name is
null, blank, empty, or is notstatic.java.lang.IllegalAccessException – Thrown if the field is not accessible.
java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static Object readDeclaredStaticField(Class<?> cls, String fieldName, boolean forceAccess) throws IllegalAccessException[source]
Gets the value of a
staticFieldby name. Only the specified class will be considered.- Parameters:
cls – the
Classto reflect, must not benull.fieldName – the field name to obtain.
forceAccess – whether to break scope restrictions using the
AccessibleObject#setAccessible(boolean)method.falsewill only matchpublicfields.
- Returns:
The Field object
- Throws:
java.lang.NullPointerException – Thrown if the class is
null, or the field could not be found.java.lang.IllegalArgumentException – Thrown if the field name is blank or empty, is not
static.java.lang.IllegalAccessException – Thrown if the field is not made accessible.
java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static Object readField(Object target, String fieldName) throws IllegalAccessException[source]
Reads the named
publicField. 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:
java.lang.NullPointerException – Thrown if the target is
null.java.lang.IllegalArgumentException – Thrown if the field name is
null, blank, empty, or could not be found.java.lang.IllegalAccessException – Thrown if the named field is not
public.java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static Object readField(Object target, String fieldName, boolean forceAccess) throws IllegalAccessException[source]
Reads the named
Field. 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 matchpublicfields.
- Returns:
The field value
- Throws:
java.lang.NullPointerException – Thrown if
targetisnull.java.lang.IllegalArgumentException – Thrown if the field name is
null, blank, empty, or could not be found.java.lang.IllegalAccessException – Thrown if the named field is not made accessible.
java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static Object readField(Field field, Object target) throws IllegalAccessException[source]
Reads an accessible
Field.- Parameters:
field – the field to use.
target – the object to call on, may be
nullforstaticfields.
- Returns:
The field value
- Throws:
java.lang.NullPointerException – Thrown if the field is
null.java.lang.IllegalAccessException – Thrown if the field is not accessible.
java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static Object readField(Field field, Object target, boolean forceAccess) throws IllegalAccessException[source]
Reads a
Field.- Parameters:
field – the field to use.
target – the object to call on, may be
nullforstaticfields.forceAccess – whether to break scope restrictions using the
AccessibleObject#setAccessible(boolean)method.
- Returns:
The field value
- Throws:
java.lang.NullPointerException – Thrown if the field is
null.java.lang.IllegalAccessException – Thrown if the field is not made accessible.
java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static Object readStaticField(Class<?> cls, String fieldName) throws IllegalAccessException[source]
Reads the named
public staticField. Superclasses will be considered.- Parameters:
cls – the
Classto reflect, must not benull.fieldName – the field name to obtain.
- Returns:
The value of the field.
- Throws:
java.lang.NullPointerException – Thrown if the class is
null, or the field could not be found.java.lang.IllegalArgumentException – Thrown if the field name is
null, blank or empty, or is notstatic.java.lang.IllegalAccessException – Thrown if the field is not accessible.
java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static Object readStaticField(Class<?> cls, String fieldName, boolean forceAccess) throws IllegalAccessException[source]
Reads the named
staticField. Superclasses will be considered.- Parameters:
cls – the
Classto reflect, must not benull.fieldName – the field name to obtain.
forceAccess – whether to break scope restrictions using the
AccessibleObject#setAccessible(boolean)method.falsewill only matchpublicfields.
- Returns:
The Field object.
- Throws:
java.lang.NullPointerException – Thrown if the class is
null, or the field could not be found.java.lang.IllegalArgumentException – Thrown if the field name is
null, blank or empty, or is notstatic.java.lang.IllegalAccessException – Thrown if the field is not made accessible.
java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static Object readStaticField(Field field) throws IllegalAccessException[source]
Reads an accessible
staticField.- Parameters:
field – to read.
- Returns:
The field value.
- Throws:
java.lang.NullPointerException – Thrown if the field is
null.java.lang.IllegalArgumentException – Thrown if the field is not
static.java.lang.IllegalAccessException – Thrown if the field is not accessible.
java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static Object readStaticField(Field field, boolean forceAccess) throws IllegalAccessException[source]
Reads a static
Field.- Parameters:
field – to read.
forceAccess – whether to break scope restrictions using the
AccessibleObject#setAccessible(boolean)method.
- Returns:
The field value.
- Throws:
java.lang.NullPointerException – Thrown if the field is
null.java.lang.IllegalArgumentException – Thrown if the field is not
static.java.lang.IllegalAccessException – Thrown if the field is not made accessible.
java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static void removeFinalModifier(Field field)[source]
Removes the final modifier from a
Field.- Parameters:
field – to remove the final modifier.
- Throws:
java.lang.NullPointerException – Thrown if the field is
null.java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- Since:
3.2
- @Deprecated public static void removeFinalModifier(Field field, boolean forceAccess)[source]
Deprecated: As of Java 12, we can no longer drop the
finalmodifier, 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 a
Field.- Parameters:
field – to remove the final modifier.
forceAccess – whether to break scope restrictions using the
AccessibleObject#setAccessible(boolean)method.falsewill only matchpublicfields.
- Throws:
java.lang.NullPointerException – Thrown if the field is
null.java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- Since:
3.3
- public static void writeDeclaredField(Object target, String fieldName, Object value) throws IllegalAccessException[source]
Writes a
publicField. Only the specified class will be considered.- Parameters:
target – the object to reflect, must not be
null.fieldName – the field name to obtain.
value – the new value.
- Throws:
java.lang.NullPointerException – Thrown if
targetisnull.java.lang.IllegalArgumentException – Thrown if
fieldNameisnull, blank or empty, or could not be found, orvalueis not assignable.java.lang.IllegalAccessException – Thrown if the field is not made accessible.
java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static void writeDeclaredField(Object target, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException[source]
Writes a
publicField. Only the specified class will be considered.- Parameters:
target – the object to reflect, must not be
null.fieldName – the field name to obtain.
value – the new value.
forceAccess – whether to break scope restrictions using the
AccessibleObject#setAccessible(boolean)method.falsewill only matchpublicfields.
- Throws:
java.lang.IllegalArgumentException – Thrown if
fieldNameisnull, blank or empty, or could not be found, orvalueis not assignable.java.lang.IllegalAccessException – Thrown if the field is not made accessible.
java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static void writeDeclaredStaticField(Class<?> cls, String fieldName, Object value) throws IllegalAccessException[source]
Writes a named
public staticField. Only the specified class will be considered.- Parameters:
cls –
Classon which the field is to be found.fieldName – to write.
value – the new value.
- Throws:
java.lang.NullPointerException – Thrown if
clsisnullor the field cannot be located.java.lang.IllegalArgumentException – Thrown if the field name is
null, blank, empty, notstatic, orvalueis not assignable.java.lang.IllegalAccessException – Thrown if the field is not
publicor isfinal.java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static void writeDeclaredStaticField(Class<?> cls, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException[source]
Writes a named
staticField. Only the specified class will be considered.- Parameters:
cls –
Classon which the field is to be found.fieldName – to write
value – the new value.
forceAccess – whether to break scope restrictions using the
AccessibleObject#setAccessible(boolean)method.falsewill only matchpublicfields.
- Throws:
java.lang.NullPointerException – Thrown if
clsisnullor the field cannot be located.java.lang.IllegalArgumentException – Thrown if the field name is
null, blank, empty, notstatic, orvalueis not assignable.java.lang.IllegalAccessException – Thrown if the field is not made accessible or is
final.java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static void writeField(Object target, String fieldName, Object value) throws IllegalAccessException[source]
Writes a
publicField. Superclasses will be considered.- Parameters:
target – the object to reflect, must not be
null.fieldName – the field name to obtain.
value – the new value.
- Throws:
java.lang.NullPointerException – Thrown if
targetisnull.java.lang.IllegalArgumentException – Thrown if
fieldNameisnull, blank, empty, or could not be found, orvalueis not assignable.java.lang.IllegalAccessException – Thrown if the field is not accessible.
java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static void writeField(Object target, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException[source]
Writes a
Field. Superclasses will be considered.- Parameters:
target – the object to reflect, must not be
null.fieldName – the field name to obtain.
value – the new value.
forceAccess – whether to break scope restrictions using the
AccessibleObject#setAccessible(boolean)method.falsewill only matchpublicfields.
- Throws:
java.lang.NullPointerException – Thrown if
targetisnull.java.lang.IllegalArgumentException – Thrown if
fieldNameisnull, blank, empty, or could not be found, orvalueis not assignable.java.lang.IllegalAccessException – Thrown if the field is not made accessible.
java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static void writeField(Field field, Object target, Object value) throws IllegalAccessException[source]
Writes an accessible
Field.- Parameters:
field – to write.
target – the object to call on, may be
nullforstaticfields.value – the new value.
- Throws:
java.lang.NullPointerException – Thrown if the field is
null.java.lang.IllegalArgumentException – Thrown if
valueis not assignable.java.lang.IllegalAccessException – Thrown if the field is not accessible or is
final.java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static void writeField(Field field, Object target, Object value, boolean forceAccess) throws IllegalAccessException[source]
Writes a
Field.- Parameters:
field – to write.
target – the object to call on, may be
nullforstaticfieldsvalue – the new value.
forceAccess – whether to break scope restrictions using the
AccessibleObject#setAccessible(boolean)method.falsewill only matchpublicfields.
- Throws:
java.lang.NullPointerException – Thrown if the field is
null.java.lang.IllegalArgumentException – Thrown if
valueis not assignable.java.lang.IllegalAccessException – Thrown if the field is not made accessible or is
final.java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static void writeStaticField(Class<?> cls, String fieldName, Object value) throws IllegalAccessException[source]
Writes a named
public staticField. Superclasses will be considered.- Parameters:
cls –
Classon which the field is to be found.fieldName – to write.
value – the new value.
- Throws:
java.lang.NullPointerException – Thrown if
targetisnull.java.lang.IllegalArgumentException – Thrown if
fieldNameisnull, blank or empty, the field cannot be located or is notstatic, orvalueis not assignable.java.lang.IllegalAccessException – Thrown if the field is not
publicor isfinal.java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static void writeStaticField(Class<?> cls, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException[source]
Writes a named
staticField. Superclasses will be considered.- Parameters:
cls –
Classon which the field is to be found.fieldName – to write.
value – the new value.
forceAccess – whether to break scope restrictions using the
AccessibleObject#setAccessible(boolean)method.falsewill only matchpublicfields.
- Throws:
java.lang.NullPointerException – Thrown if
clsisnullor the field cannot be located.java.lang.IllegalArgumentException – Thrown if
fieldNameisnull, blank or empty, the field notstatic, orvalueis not assignable.java.lang.IllegalAccessException – Thrown if the field is not made accessible or is
final.java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static void writeStaticField(Field field, Object value) throws IllegalAccessException[source]
Writes a
public staticField.- Parameters:
field – to write.
value – the new value.
- Throws:
java.lang.NullPointerException – Thrown if the field is
null.java.lang.IllegalArgumentException – Thrown if the field is not
static, orvalueis not assignable.java.lang.IllegalAccessException – Thrown if the field is not
publicor isfinal.java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also:
- public static void writeStaticField(Field field, Object value, boolean forceAccess) throws IllegalAccessException[source]
Writes a static
Field.- Parameters:
field – to write.
value – the new value.
forceAccess – whether to break scope restrictions using the
AccessibleObject#setAccessible(boolean)method.falsewill only matchpublicfields.
- Throws:
java.lang.NullPointerException – Thrown if the field is
null.java.lang.IllegalArgumentException – Thrown if the field is not
static, orvalueis not assignable.java.lang.IllegalAccessException – Thrown if the field is not made accessible or is
final.java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.
- See also: