public class FieldUtils
extends java.lang.Object
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.
Constructor and Description |
---|
FieldUtils()
FieldUtils instances should NOT be constructed in standard programming. |
Modifier and Type | Method and 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 accessible
Field by name respecting scope. |
static java.lang.reflect.Field |
getDeclaredField(java.lang.Class<?> cls,
java.lang.String fieldName,
boolean forceAccess)
Gets an accessible
Field by name, breaking scope if requested. |
static java.lang.reflect.Field |
getField(java.lang.Class<?> cls,
java.lang.String fieldName)
Gets an accessible
Field by name respecting scope. |
static java.lang.reflect.Field |
getField(java.lang.Class<?> cls,
java.lang.String fieldName,
boolean forceAccess)
Gets an accessible
Field 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 named
public Field . |
static java.lang.Object |
readDeclaredField(java.lang.Object target,
java.lang.String fieldName,
boolean forceAccess)
Gets a
Field value by name. |
static java.lang.Object |
readDeclaredStaticField(java.lang.Class<?> cls,
java.lang.String fieldName)
Gets the value of a
static Field by name. |
static java.lang.Object |
readDeclaredStaticField(java.lang.Class<?> cls,
java.lang.String fieldName,
boolean forceAccess)
Gets the value of a
static Field by name. |
static java.lang.Object |
readField(java.lang.reflect.Field field,
java.lang.Object target)
Reads an accessible
Field . |
static java.lang.Object |
readField(java.lang.reflect.Field field,
java.lang.Object target,
boolean forceAccess)
Reads a
Field . |
static java.lang.Object |
readField(java.lang.Object target,
java.lang.String fieldName)
Reads the named
public Field . |
static java.lang.Object |
readField(java.lang.Object target,
java.lang.String fieldName,
boolean forceAccess)
Reads the named
Field . |
static java.lang.Object |
readStaticField(java.lang.Class<?> cls,
java.lang.String fieldName)
Reads the named
public static Field . |
static java.lang.Object |
readStaticField(java.lang.Class<?> cls,
java.lang.String fieldName,
boolean forceAccess)
Reads the named
static Field . |
static java.lang.Object |
readStaticField(java.lang.reflect.Field field)
Reads an accessible
static Field . |
static java.lang.Object |
readStaticField(java.lang.reflect.Field field,
boolean forceAccess)
Reads a static
Field . |
static void |
removeFinalModifier(java.lang.reflect.Field field)
Removes the final modifier from a
Field . |
static void |
removeFinalModifier(java.lang.reflect.Field field,
boolean forceAccess)
Deprecated.
As of java 12.0, we can no longer drop the
final 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.html |
static void |
writeDeclaredField(java.lang.Object target,
java.lang.String fieldName,
java.lang.Object value)
Writes a
public Field . |
static void |
writeDeclaredField(java.lang.Object target,
java.lang.String fieldName,
java.lang.Object value,
boolean forceAccess)
Writes a
public Field . |
static void |
writeDeclaredStaticField(java.lang.Class<?> cls,
java.lang.String fieldName,
java.lang.Object value)
Writes a named
public static Field . |
static void |
writeDeclaredStaticField(java.lang.Class<?> cls,
java.lang.String fieldName,
java.lang.Object value,
boolean forceAccess)
Writes a named
static Field . |
static void |
writeField(java.lang.reflect.Field field,
java.lang.Object target,
java.lang.Object value)
Writes an accessible
Field . |
static void |
writeField(java.lang.reflect.Field field,
java.lang.Object target,
java.lang.Object value,
boolean forceAccess)
Writes a
Field . |
static void |
writeField(java.lang.Object target,
java.lang.String fieldName,
java.lang.Object value)
Writes a
public Field . |
static void |
writeField(java.lang.Object target,
java.lang.String fieldName,
java.lang.Object value,
boolean forceAccess)
Writes a
Field . |
static void |
writeStaticField(java.lang.Class<?> cls,
java.lang.String fieldName,
java.lang.Object value)
Writes a named
public static Field . |
static void |
writeStaticField(java.lang.Class<?> cls,
java.lang.String fieldName,
java.lang.Object value,
boolean forceAccess)
Writes a named
static Field . |
static void |
writeStaticField(java.lang.reflect.Field field,
java.lang.Object value)
Writes a
public static Field . |
static void |
writeStaticField(java.lang.reflect.Field field,
java.lang.Object value,
boolean forceAccess)
Writes a static
Field . |
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 java.lang.reflect.Field getField(java.lang.Class<?> cls, java.lang.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 obtainjava.lang.IllegalArgumentException
- if the class is null
, or the field name is blank or emptypublic static java.lang.reflect.Field getField(java.lang.Class<?> cls, java.lang.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 obtainforceAccess
- whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean)
method. false
will only
match public
fields.java.lang.IllegalArgumentException
- if the class is null
, or the field name is blank or empty or is matched at multiple places
in the inheritance hierarchypublic static java.lang.reflect.Field getDeclaredField(java.lang.Class<?> cls, java.lang.String fieldName)
Field
by name respecting scope. Only the specified class will be considered.cls
- the Class
to reflect, must not be null
fieldName
- the field name to obtainjava.lang.IllegalArgumentException
- if the class is null
, or the field name is blank or emptypublic static java.lang.reflect.Field getDeclaredField(java.lang.Class<?> cls, java.lang.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 obtainforceAccess
- whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean)
method. false
will only
match public
fields.java.lang.IllegalArgumentException
- if the class is null
, or the field name is blank or emptypublic static java.lang.reflect.Field[] getAllFields(java.lang.Class<?> cls)
cls
- the Class
to queryjava.lang.IllegalArgumentException
- if the class is null
public static java.util.List<java.lang.reflect.Field> getAllFieldsList(java.lang.Class<?> cls)
cls
- the Class
to queryjava.lang.IllegalArgumentException
- if the class is null
public static java.lang.reflect.Field[] getFieldsWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls)
cls
- the Class
to queryannotationCls
- the Annotation
that must be present on a field to be matchedjava.lang.IllegalArgumentException
- if the class or annotation are null
public static java.util.List<java.lang.reflect.Field> getFieldsListWithAnnotation(java.lang.Class<?> cls, java.lang.Class<? extends java.lang.annotation.Annotation> annotationCls)
cls
- the Class
to queryannotationCls
- the Annotation
that must be present on a field to be matchedjava.lang.IllegalArgumentException
- if the class or annotation are null
public static java.lang.Object readStaticField(java.lang.reflect.Field field) throws java.lang.IllegalAccessException
static
Field
.field
- to readjava.lang.IllegalArgumentException
- if the field is null
, or not static
java.lang.IllegalAccessException
- if the field is not accessiblepublic static java.lang.Object readStaticField(java.lang.reflect.Field field, boolean forceAccess) throws java.lang.IllegalAccessException
Field
.field
- to readforceAccess
- whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean)
method.java.lang.IllegalArgumentException
- if the field is null
or not static
java.lang.IllegalAccessException
- if the field is not made accessiblepublic static java.lang.Object readStaticField(java.lang.Class<?> cls, java.lang.String fieldName) throws java.lang.IllegalAccessException
public static
Field
. Superclasses will be considered.cls
- the Class
to reflect, must not be null
fieldName
- the field name to obtainjava.lang.IllegalArgumentException
- if the class is null
, or the field name is blank or empty, is not static
, or could
not be foundjava.lang.IllegalAccessException
- if the field is not accessiblepublic static java.lang.Object readStaticField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess) throws java.lang.IllegalAccessException
static
Field
. Superclasses will be considered.cls
- the Class
to reflect, must not be null
fieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean)
method. false
will only
match public
fields.java.lang.IllegalArgumentException
- if the class is null
, or the field name is blank or empty, is not static
, or could
not be foundjava.lang.IllegalAccessException
- if the field is not made accessiblepublic static java.lang.Object readDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName) throws java.lang.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 obtainjava.lang.IllegalArgumentException
- if the class is null
, or the field name is blank or empty, is not static
, or could
not be foundjava.lang.IllegalAccessException
- if the field is not accessiblepublic static java.lang.Object readDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess) throws java.lang.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 obtainforceAccess
- whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean)
method. false
will only
match public
fields.java.lang.IllegalArgumentException
- if the class is null
, or the field name is blank or empty, is not static
, or could
not be foundjava.lang.IllegalAccessException
- if the field is not made accessiblepublic static java.lang.Object readField(java.lang.reflect.Field field, java.lang.Object target) throws java.lang.IllegalAccessException
Field
.field
- the field to usetarget
- the object to call on, may be null
for static
fieldsjava.lang.IllegalArgumentException
- if the field is null
java.lang.IllegalAccessException
- if the field is not accessiblepublic static java.lang.Object readField(java.lang.reflect.Field field, java.lang.Object target, boolean forceAccess) throws java.lang.IllegalAccessException
Field
.field
- the field to usetarget
- the object to call on, may be null
for static
fieldsforceAccess
- whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean)
method.java.lang.IllegalArgumentException
- if the field is null
java.lang.IllegalAccessException
- if the field is not made accessiblepublic static java.lang.Object readField(java.lang.Object target, java.lang.String fieldName) throws java.lang.IllegalAccessException
public
Field
. Superclasses will be considered.target
- the object to reflect, must not be null
fieldName
- the field name to obtainjava.lang.IllegalArgumentException
- if the class is null
, or the field name is blank or empty or could not be foundjava.lang.IllegalAccessException
- if the named field is not public
public static java.lang.Object readField(java.lang.Object target, java.lang.String fieldName, boolean forceAccess) throws java.lang.IllegalAccessException
Field
. Superclasses will be considered.target
- the object to reflect, must not be null
fieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean)
method. false
will only
match public
fields.java.lang.IllegalArgumentException
- if target
is null
, or the field name is blank or empty or could not be foundjava.lang.IllegalAccessException
- if the named field is not made accessiblepublic static java.lang.Object readDeclaredField(java.lang.Object target, java.lang.String fieldName) throws java.lang.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 obtainjava.lang.IllegalArgumentException
- if target
is null
, or the field name is blank or empty or could not be foundjava.lang.IllegalAccessException
- if the named field is not public
public static java.lang.Object readDeclaredField(java.lang.Object target, java.lang.String fieldName, boolean forceAccess) throws java.lang.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 obtainforceAccess
- whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean)
method. false
will only
match public fields.java.lang.IllegalArgumentException
- if target
is null
, or the field name is blank or empty or could not be foundjava.lang.IllegalAccessException
- if the field is not made accessiblepublic static void writeStaticField(java.lang.reflect.Field field, java.lang.Object value) throws java.lang.IllegalAccessException
public static
Field
.field
- to writevalue
- to setjava.lang.IllegalArgumentException
- if the field is null
or not static
, or value
is not assignablejava.lang.IllegalAccessException
- if the field is not public
or is final
public static void writeStaticField(java.lang.reflect.Field field, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
Field
.field
- to writevalue
- to setforceAccess
- whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean)
method. false
will only
match public
fields.java.lang.IllegalArgumentException
- if the field is null
or not static
, or value
is not assignablejava.lang.IllegalAccessException
- if the field is not made accessible or is final
public static void writeStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalAccessException
public static
Field
. Superclasses will be considered.cls
- Class
on which the field is to be foundfieldName
- to writevalue
- to setjava.lang.IllegalArgumentException
- if cls
is null
, the field name is blank or empty, the field cannot be located or is
not static
, or value
is not assignablejava.lang.IllegalAccessException
- if the field is not public
or is final
public static void writeStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
static
Field
. Superclasses will be considered.cls
- Class
on which the field is to be foundfieldName
- to writevalue
- to setforceAccess
- whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean)
method. false
will only
match public
fields.java.lang.IllegalArgumentException
- if cls
is null
, the field name is blank or empty, the field cannot be located or is
not static
, or value
is not assignablejava.lang.IllegalAccessException
- if the field is not made accessible or is final
public static void writeDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalAccessException
public static
Field
. Only the specified class will be considered.cls
- Class
on which the field is to be foundfieldName
- to writevalue
- to setjava.lang.IllegalArgumentException
- if cls
is null
, the field name is blank or empty, the field cannot be located or is
not static
, or value
is not assignablejava.lang.IllegalAccessException
- if the field is not public
or is final
public static void writeDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
static
Field
. Only the specified class will be considered.cls
- Class
on which the field is to be foundfieldName
- to writevalue
- to setforceAccess
- whether to break scope restrictions using the AccessibleObject#setAccessible(boolean)
method.
false
will only match public
fields.java.lang.IllegalArgumentException
- if cls
is null
, the field name is blank or empty, the field cannot be located or is
not static
, or value
is not assignablejava.lang.IllegalAccessException
- if the field is not made accessible or is final
public static void writeField(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value) throws java.lang.IllegalAccessException
Field
.field
- to writetarget
- the object to call on, may be null
for static
fieldsvalue
- to setjava.lang.IllegalAccessException
- if the field or target is null
, the field is not accessible or is final
, or
value
is not assignablepublic static void writeField(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
Field
.field
- to writetarget
- the object to call on, may be null
for static
fieldsvalue
- to setforceAccess
- whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean)
method. false
will only
match public
fields.java.lang.IllegalArgumentException
- if the field is null
or value
is not assignablejava.lang.IllegalAccessException
- if the field is not made accessible or is final
public static void removeFinalModifier(java.lang.reflect.Field field)
Field
.field
- to remove the final modifierjava.lang.IllegalArgumentException
- if the field is null
@Deprecated public static void removeFinalModifier(java.lang.reflect.Field field, boolean forceAccess)
final
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.htmlField
.field
- to remove the final modifierforceAccess
- whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean)
method. false
will only
match public
fields.java.lang.IllegalArgumentException
- if the field is null
public static void writeField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalAccessException
public
Field
. Superclasses will be considered.target
- the object to reflect, must not be null
fieldName
- the field name to obtainvalue
- to setjava.lang.IllegalArgumentException
- if target
is null
, fieldName
is blank or empty or could not be found, or
value
is not assignablejava.lang.IllegalAccessException
- if the field is not accessiblepublic static void writeField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
Field
. Superclasses will be considered.target
- the object to reflect, must not be null
fieldName
- the field name to obtainvalue
- to setforceAccess
- whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean)
method. false
will only
match public
fields.java.lang.IllegalArgumentException
- if target
is null
, fieldName
is blank or empty or could not be found, or
value
is not assignablejava.lang.IllegalAccessException
- if the field is not made accessiblepublic static void writeDeclaredField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalAccessException
public
Field
. Only the specified class will be considered.target
- the object to reflect, must not be null
fieldName
- the field name to obtainvalue
- to setjava.lang.IllegalArgumentException
- if target
is null
, fieldName
is blank or empty or could not be found, or
value
is not assignablejava.lang.IllegalAccessException
- if the field is not made accessiblepublic static void writeDeclaredField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
public
Field
. Only the specified class will be considered.target
- the object to reflect, must not be null
fieldName
- the field name to obtainvalue
- to setforceAccess
- whether to break scope restrictions using the
AccessibleObject.setAccessible(boolean)
method. false
will only
match public
fields.java.lang.IllegalArgumentException
- if target
is null
, fieldName
is blank or empty or could not be found, or
value
is not assignablejava.lang.IllegalAccessException
- if the field is not made accessibleCopyright © 2010 - 2020 Adobe. All Rights Reserved