public class FieldUtils extends Object
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.
| Constructor and Description |
|---|
FieldUtils()
Deprecated.
TODO Make private in 4.0.
|
| Modifier and Type | Method and 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 Field |
getDeclaredField(Class<?> cls,
String fieldName)
Gets an accessible
Field by name respecting scope. |
static Field |
getDeclaredField(Class<?> cls,
String fieldName,
boolean forceAccess)
Gets an accessible
Field by name, breaking scope if requested. |
static Field |
getField(Class<?> cls,
String fieldName)
Gets an accessible
Field by name respecting scope. |
static Field |
getField(Class<?> cls,
String fieldName,
boolean forceAccess)
Gets an accessible
Field by 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 Object |
readDeclaredField(Object target,
String fieldName)
Reads the named
public Field. |
static Object |
readDeclaredField(Object target,
String fieldName,
boolean forceAccess)
Gets a
Field value by name. |
static Object |
readDeclaredStaticField(Class<?> cls,
String fieldName)
Gets the value of a
static Field by name. |
static Object |
readDeclaredStaticField(Class<?> cls,
String fieldName,
boolean forceAccess)
Gets the value of a
static Field by name. |
static Object |
readField(Field field,
Object target)
Reads an accessible
Field. |
static Object |
readField(Field field,
Object target,
boolean forceAccess)
Reads a
Field. |
static Object |
readField(Object target,
String fieldName)
Reads the named
public Field. |
static Object |
readField(Object target,
String fieldName,
boolean forceAccess)
Reads the named
Field. |
static Object |
readStaticField(Class<?> cls,
String fieldName)
Reads the named
public static Field. |
static Object |
readStaticField(Class<?> cls,
String fieldName,
boolean forceAccess)
Reads the named
static Field. |
static Object |
readStaticField(Field field)
Reads an accessible
static Field. |
static Object |
readStaticField(Field field,
boolean forceAccess)
Reads a static
Field. |
static void |
removeFinalModifier(Field field)
Removes the final modifier from a
Field. |
static void |
removeFinalModifier(Field field,
boolean forceAccess)
Deprecated.
As of Java 12, we can no longer drop the
final 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.html |
static void |
writeDeclaredField(Object target,
String fieldName,
Object value)
Writes a
public Field. |
static void |
writeDeclaredField(Object target,
String fieldName,
Object value,
boolean forceAccess)
Writes a
public Field. |
static void |
writeDeclaredStaticField(Class<?> cls,
String fieldName,
Object value)
Writes a named
public static Field. |
static void |
writeDeclaredStaticField(Class<?> cls,
String fieldName,
Object value,
boolean forceAccess)
Writes a named
static Field. |
static void |
writeField(Field field,
Object target,
Object value)
Writes an accessible
Field. |
static void |
writeField(Field field,
Object target,
Object value,
boolean forceAccess)
Writes a
Field. |
static void |
writeField(Object target,
String fieldName,
Object value)
Writes a
public Field. |
static void |
writeField(Object target,
String fieldName,
Object value,
boolean forceAccess)
Writes a
Field. |
static void |
writeStaticField(Class<?> cls,
String fieldName,
Object value)
Writes a named
public static Field. |
static void |
writeStaticField(Class<?> cls,
String fieldName,
Object value,
boolean forceAccess)
Writes a named
static Field. |
static void |
writeStaticField(Field field,
Object value)
Writes a
public static Field. |
static void |
writeStaticField(Field field,
Object value,
boolean forceAccess)
Writes a static
Field. |
@Deprecated 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.
public static Field[] getAllFields(Class<?> cls)
cls - the Class to queryNullPointerException - if the class is null.public static List<Field> getAllFieldsList(Class<?> cls)
cls - the Class to queryNullPointerException - if the class is null.public static Field getDeclaredField(Class<?> cls, String fieldName)
Field by name respecting scope. Only the specified class will be considered.cls - the Class to reflect, must not be nullfieldName - the field name to obtain.NullPointerException - if the class is null.IllegalArgumentException - if the field name is null, blank, or empty.SecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static Field getDeclaredField(Class<?> cls, String fieldName, boolean forceAccess)
Field by name, breaking scope if requested. Only the specified class will be
considered.cls - the Class to reflect, must not be null.fieldName - the field name to obtain.forceAccess - whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean) method. false will only
match public fields.NullPointerException - if the class is null.IllegalArgumentException - if the field name is null, blank, or empty.SecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static Field getField(Class<?> cls, String fieldName)
Field by name respecting scope. Superclasses/interfaces will be considered.cls - the Class to reflect, must not be null.fieldName - the field name to obtain.NullPointerException - if the class is null.IllegalArgumentException - if the field name is null, blank, or emptySecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static Field getField(Class<?> cls, String fieldName, boolean forceAccess)
Field by name, breaking scope if requested. Superclasses/interfaces will be
considered.cls - the Class to reflect, must not be null.fieldName - the field name to obtain.forceAccess - whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean) method. false will only
match public fields.NullPointerException - if the class is null.IllegalArgumentException - if the field name is blank or empty or is matched at multiple places
in the inheritance hierarchy.SecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static List<Field> getFieldsListWithAnnotation(Class<?> cls, Class<? extends Annotation> annotationCls)
cls - the Class to query.annotationCls - the Annotation that must be present on a field to be matched.NullPointerException - if the class or annotation are null.public static Field[] getFieldsWithAnnotation(Class<?> cls, Class<? extends Annotation> annotationCls)
cls - the Class to query.annotationCls - the Annotation that must be present on a field to be matchedNullPointerException - if the class or annotation are null.public static Object readDeclaredField(Object target, String fieldName) throws IllegalAccessException
public Field. Only the class of the specified object will be considered.target - the object to reflect, must not be null.fieldName - the field name to obtain.NullPointerException - if target is null.IllegalArgumentException - if fieldName is null, blank or empty, or could not be found.IllegalAccessException - if the named field is not publicSecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static Object readDeclaredField(Object target, String fieldName, boolean forceAccess) throws IllegalAccessException
Field value by name. Only the class of the specified object will be considered.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. false will only
match public fields.NullPointerException - if target is null.IllegalArgumentException - if fieldName is null, 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.SecurityManager.checkPermission(java.security.Permission)public static Object readDeclaredStaticField(Class<?> cls, String fieldName) throws IllegalAccessException
static Field by name. The field must be public. Only the specified
class will be considered.cls - the Class to reflect, must not be null.fieldName - the field name to obtain.NullPointerException - if the class is null, or the field could not be found.IllegalArgumentException - if the field name is null, blank, empty, or is not static.IllegalAccessException - if the field is not accessibleSecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static Object readDeclaredStaticField(Class<?> cls, String fieldName, boolean forceAccess) throws IllegalAccessException
static Field by name. Only the specified class will be considered.cls - the Class to reflect, must not be null.fieldName - the field name to obtain.forceAccess - whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean) method. false will only
match public fields.NullPointerException - if the class is null, or the field could not be found.IllegalArgumentException - if the field name is blank or empty, is not static.IllegalAccessException - if the field is not made accessibleSecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static Object readField(Field field, Object target) throws IllegalAccessException
Field.field - the field to use.target - the object to call on, may be null for static fields.NullPointerException - if the field is null.IllegalAccessException - if the field is not accessible.SecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static Object readField(Field field, Object target, boolean forceAccess) throws IllegalAccessException
Field.field - the field to use.target - the object to call on, may be null for static fields.forceAccess - whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean) method.NullPointerException - if the field is null.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.SecurityManager.checkPermission(java.security.Permission),
SecurityManager.checkPermission(java.security.Permission)public static Object readField(Object target, String fieldName) throws IllegalAccessException
public Field. Superclasses will be considered.target - the object to reflect, must not be null.fieldName - the field name to obtain.NullPointerException - if the target is null.IllegalArgumentException - if the field name is null, blank, empty, or could not be found.IllegalAccessException - if the named field is not public.SecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static Object readField(Object target, String fieldName, boolean forceAccess) throws IllegalAccessException
Field. Superclasses will be considered.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. false will only
match public fields.NullPointerException - if target is null.IllegalArgumentException - if the field name is null, 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.SecurityManager.checkPermission(java.security.Permission)public static Object readStaticField(Class<?> cls, String fieldName) throws IllegalAccessException
public static Field. Superclasses will be considered.cls - the Class to reflect, must not be null.fieldName - the field name to obtain.NullPointerException - if the class is null, or the field could not be found.IllegalArgumentException - if the field name is null, blank or empty, or is not static.IllegalAccessException - if the field is not accessible.SecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static Object readStaticField(Class<?> cls, String fieldName, boolean forceAccess) throws IllegalAccessException
static Field. Superclasses will be considered.cls - the Class to reflect, must not be null.fieldName - the field name to obtain.forceAccess - whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean) method. false will only
match public fields.NullPointerException - if the class is null, or the field could not be found.IllegalArgumentException - if the field name is null, blank or empty, or is not static.IllegalAccessException - if the field is not made accessible.SecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static Object readStaticField(Field field) throws IllegalAccessException
static Field.field - to read.NullPointerException - if the field is null.IllegalArgumentException - if the field is not static.IllegalAccessException - if the field is not accessibleSecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static Object readStaticField(Field field, boolean forceAccess) throws IllegalAccessException
Field.field - to read.forceAccess - whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean) method.NullPointerException - if the field is null.IllegalArgumentException - if the field is not static.IllegalAccessException - if the field is not made accessible.SecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static void removeFinalModifier(Field field)
Field.field - to remove the final modifier.NullPointerException - if the field is null.SecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)@Deprecated public static void removeFinalModifier(Field field, boolean forceAccess)
final 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.htmlField.field - to remove the final modifier.forceAccess - whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean) method. false will only
match public fields.NullPointerException - if the field is null.SecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static void writeDeclaredField(Object target, String fieldName, Object value) throws IllegalAccessException
public Field. Only the specified class will be considered.target - the object to reflect, must not be null.fieldName - the field name to obtain.value - the new value.NullPointerException - if target is null.IllegalArgumentException - if fieldName is null, blank or empty, or could not be found,
or value is not assignable.IllegalAccessException - if the field is not made accessibleSecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static void writeDeclaredField(Object target, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException
public Field. Only the specified class will be considered.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. false will only
match public fields.IllegalArgumentException - if fieldName is null, blank or empty, or could not be found,
or value is not assignableIllegalAccessException - if the field is not made accessibleSecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static void writeDeclaredStaticField(Class<?> cls, String fieldName, Object value) throws IllegalAccessException
public static Field. Only the specified class will be considered.cls - Class on which the field is to be found.fieldName - to write.value - the new value.NullPointerException - if cls is null or the field cannot be located.IllegalArgumentException - if the field name is null, blank, empty, not static, or value is not assignable.IllegalAccessException - if the field is not public or is finalSecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static void writeDeclaredStaticField(Class<?> cls, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException
static Field. Only the specified class will be considered.cls - Class on which the field is to be found.fieldName - to writevalue - the new value.forceAccess - whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method.
false will only match public fields.NullPointerException - if cls is null or the field cannot be located.IllegalArgumentException - if the field name is null, blank, empty, not static, or value is not assignable.IllegalAccessException - if the field is not made accessible or is finalSecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static void writeField(Field field, Object target, Object value) throws IllegalAccessException
Field.field - to write.target - the object to call on, may be null for static fields.value - the new value.NullPointerException - if the field is null.IllegalArgumentException - if value is not assignable.IllegalAccessException - if the field is not accessible or is final.SecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static void writeField(Field field, Object target, Object value, boolean forceAccess) throws IllegalAccessException
Field.field - to write.target - the object to call on, may be null for static fieldsvalue - the new value.forceAccess - whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean) method. false will only
match public fields.NullPointerException - if the field is null.IllegalArgumentException - if value is not assignable.IllegalAccessException - if the field is not made accessible or is finalSecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static void writeField(Object target, String fieldName, Object value) throws IllegalAccessException
public Field. Superclasses will be considered.target - the object to reflect, must not be null.fieldName - the field name to obtain.value - the new value.NullPointerException - if target is null.IllegalArgumentException - if fieldName is null, blank, empty, or could not be found,
or value is not assignable.IllegalAccessException - if the field is not accessible.SecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static void writeField(Object target, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException
Field. Superclasses will be considered.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. false will only
match public fields.NullPointerException - if target is null.IllegalArgumentException - if fieldName is null, blank, empty, or could not be found,
or value is not assignable.IllegalAccessException - if the field is not made accessible.SecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static void writeStaticField(Class<?> cls, String fieldName, Object value) throws IllegalAccessException
public static Field. Superclasses will be considered.cls - Class on which the field is to be found.fieldName - to write.value - the new value.NullPointerException - if target is null.IllegalArgumentException - if fieldName is null, blank or empty, the field cannot be located or is
not static, or value is not assignable.IllegalAccessException - if the field is not public or is finalSecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static void writeStaticField(Class<?> cls, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException
static Field. Superclasses will be considered.cls - Class on 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. false will only
match public fields.NullPointerException - if cls is null or the field cannot be located.IllegalArgumentException - if fieldName is null, blank or empty, the field not static, or value is not assignable.IllegalAccessException - if the field is not made accessible or is final.SecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static void writeStaticField(Field field, Object value) throws IllegalAccessException
public static Field.field - to write.value - the new value.NullPointerException - if the field is null.IllegalArgumentException - if the field is not static, or value is not assignable.IllegalAccessException - if the field is not public or is final.SecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)public static void writeStaticField(Field field, Object value, boolean forceAccess) throws IllegalAccessException
Field.field - to write.value - the new value.forceAccess - whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean) method. false will only
match public fields.NullPointerException - if the field is null.IllegalArgumentException - if the field is not static, or value is not assignable.IllegalAccessException - if the field is not made accessible or is finalSecurityException - if an underlying accessible object's method denies the request.SecurityManager.checkPermission(java.security.Permission)Copyright © 2001–2025 The Apache Software Foundation. All rights reserved.