|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.lang.reflect.FieldUtils
public class FieldUtils
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.
Constructor Summary | |
---|---|
FieldUtils()
FieldUtils instances should NOT be constructed in standard programming. |
Method Summary | |
---|---|
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 repecting scope. |
static Field |
getField(Class cls,
String fieldName,
boolean forceAccess)
Gets an accessible Field by name breaking scope
if requested. |
static Object |
readDeclaredField(Object target,
String fieldName)
Read 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 a static Field value by name. |
static Object |
readDeclaredStaticField(Class cls,
String fieldName,
boolean forceAccess)
Gets a static Field value by name. |
static Object |
readField(Field field,
Object target)
Read an accessible Field. |
static Object |
readField(Field field,
Object target,
boolean forceAccess)
Read a Field. |
static Object |
readField(Object target,
String fieldName)
Read the named public field. |
static Object |
readField(Object target,
String fieldName,
boolean forceAccess)
Read the named field. |
static Object |
readStaticField(Class cls,
String fieldName)
Read the named public static field. |
static Object |
readStaticField(Class cls,
String fieldName,
boolean forceAccess)
Read the named static field. |
static Object |
readStaticField(Field field)
Read an accessible static Field. |
static Object |
readStaticField(Field field,
boolean forceAccess)
Read a static Field. |
static void |
writeDeclaredField(Object target,
String fieldName,
Object value)
Write a public field. |
static void |
writeDeclaredField(Object target,
String fieldName,
Object value,
boolean forceAccess)
Write a public field. |
static void |
writeDeclaredStaticField(Class cls,
String fieldName,
Object value)
Write a named public static Field. |
static void |
writeDeclaredStaticField(Class cls,
String fieldName,
Object value,
boolean forceAccess)
Write a named static Field. |
static void |
writeField(Field field,
Object target,
Object value)
Write an accessible field. |
static void |
writeField(Field field,
Object target,
Object value,
boolean forceAccess)
Write a field. |
static void |
writeField(Object target,
String fieldName,
Object value)
Write a public field. |
static void |
writeField(Object target,
String fieldName,
Object value,
boolean forceAccess)
Write a field. |
static void |
writeStaticField(Class cls,
String fieldName,
Object value)
Write a named public static Field. |
static void |
writeStaticField(Class cls,
String fieldName,
Object value,
boolean forceAccess)
Write a named static Field. |
static void |
writeStaticField(Field field,
Object value)
Write a public static Field. |
static void |
writeStaticField(Field field,
Object value,
boolean forceAccess)
Write a static Field. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public FieldUtils()
This constructor is public to permit tools that require a JavaBean instance to operate.
Method Detail |
---|
public static Field getField(Class cls, String fieldName)
Field
by name repecting scope.
Superclasses/interfaces will be considered.
cls
- the class to reflect, must not be nullfieldName
- the field name to obtain
IllegalArgumentException
- if the class or field name is nullpublic 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 nullfieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using the
setAccessible
method. False
will only
match public fields.
IllegalArgumentException
- if the class or field name is nullpublic 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
IllegalArgumentException
- if the class or field name is nullpublic 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 nullfieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using the
setAccessible
method. False will only match public fields.
IllegalArgumentException
- if the class or field name is nullpublic static Object readStaticField(Field field) throws IllegalAccessException
field
- to read
IllegalArgumentException
- if the field is null or not static
IllegalAccessException
- if the field is not accessiblepublic static Object readStaticField(Field field, boolean forceAccess) throws IllegalAccessException
field
- to readforceAccess
- whether to break scope restrictions using the
setAccessible
method.
IllegalArgumentException
- if the field is null or not static
IllegalAccessException
- if the field is not made accessiblepublic static Object readStaticField(Class cls, String fieldName) throws IllegalAccessException
cls
- the class to reflect, must not be nullfieldName
- the field name to obtain
IllegalArgumentException
- if the class or field name is null
IllegalAccessException
- if the field is not accessiblepublic static Object readStaticField(Class cls, String fieldName, boolean forceAccess) throws IllegalAccessException
cls
- the class to reflect, must not be nullfieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using the
setAccessible
method. False
will only
match public fields.
IllegalArgumentException
- if the class or field name is null
IllegalAccessException
- if the field is not made accessiblepublic static Object readDeclaredStaticField(Class cls, String fieldName) throws IllegalAccessException
cls
- the class to reflect, must not be nullfieldName
- the field name to obtain
IllegalArgumentException
- if the class or field name is null
IllegalAccessException
- if the field is not accessiblepublic static Object readDeclaredStaticField(Class cls, String fieldName, boolean forceAccess) throws IllegalAccessException
cls
- the class to reflect, must not be nullfieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using the
setAccessible
method. False
will only
match public fields.
IllegalArgumentException
- if the class or field name is null
IllegalAccessException
- if the field is not made accessiblepublic static Object readField(Field field, Object target) throws IllegalAccessException
field
- the field to usetarget
- the object to call on, may be null for static fields
IllegalArgumentException
- if the field is null
IllegalAccessException
- if the field is not accessiblepublic static Object readField(Field field, Object target, boolean forceAccess) throws IllegalAccessException
field
- the field to usetarget
- the object to call on, may be null for static fieldsforceAccess
- whether to break scope restrictions using the
setAccessible
method.
IllegalArgumentException
- if the field is null
IllegalAccessException
- if the field is not made accessiblepublic static Object readField(Object target, String fieldName) throws IllegalAccessException
target
- the object to reflect, must not be nullfieldName
- the field name to obtain
IllegalArgumentException
- if the class or field name is null
IllegalAccessException
- if the named field is not publicpublic static Object readField(Object target, String fieldName, boolean forceAccess) throws IllegalAccessException
target
- the object to reflect, must not be nullfieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using the
setAccessible
method. False
will only
match public fields.
IllegalArgumentException
- if the class or field name is null
IllegalAccessException
- if the named field is not made accessiblepublic static Object readDeclaredField(Object target, String fieldName) throws IllegalAccessException
target
- the object to reflect, must not be nullfieldName
- the field name to obtain
IllegalArgumentException
- if the class or field name is null
IllegalAccessException
- if the named field is not publicpublic static Object readDeclaredField(Object target, String fieldName, boolean forceAccess) throws IllegalAccessException
Gets a Field value by name. Only the class of the specified object will be considered.
target
- the object to reflect, must not be nullfieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using the
setAccessible
method. False
will only
match public fields.
IllegalArgumentException
- if target
or fieldName
is null
IllegalAccessException
- if the field is not made accessiblepublic static void writeStaticField(Field field, Object value) throws IllegalAccessException
field
- to writevalue
- to set
IllegalArgumentException
- if the field is null or not static
IllegalAccessException
- if the field is not public or is finalpublic static void writeStaticField(Field field, Object value, boolean forceAccess) throws IllegalAccessException
field
- to writevalue
- to setforceAccess
- whether to break scope restrictions using the
setAccessible
method. False
will only
match public fields.
IllegalArgumentException
- if the field is null or not static
IllegalAccessException
- if the field is not made accessible or is finalpublic static void writeStaticField(Class cls, String fieldName, Object value) throws IllegalAccessException
cls
- Class on which the Field is to be foundfieldName
- to writevalue
- to set
IllegalArgumentException
- if the field cannot be located or is not static
IllegalAccessException
- if the field is not public or is finalpublic static void writeStaticField(Class cls, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException
cls
- Class on which the Field is to be foundfieldName
- to writevalue
- to setforceAccess
- whether to break scope restrictions using the
setAccessible
method. False
will only
match public fields.
IllegalArgumentException
- if the field cannot be located or is not static
IllegalAccessException
- if the field is not made accessible or is finalpublic static void writeDeclaredStaticField(Class cls, String fieldName, Object value) throws IllegalAccessException
cls
- Class on which the Field is to be foundfieldName
- to writevalue
- to set
IllegalArgumentException
- if the field cannot be located or is not static
IllegalAccessException
- if the field is not public or is finalpublic static void writeDeclaredStaticField(Class cls, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException
cls
- Class on which the Field is to be foundfieldName
- to writevalue
- to setforceAccess
- whether to break scope restrictions using the
setAccessible
method. False
will only
match public fields.
IllegalArgumentException
- if the field cannot be located or is not static
IllegalAccessException
- if the field is not made accessible or is finalpublic static void writeField(Field field, Object target, Object value) throws IllegalAccessException
field
- to writetarget
- the object to call on, may be null for static fieldsvalue
- to set
IllegalArgumentException
- if the field is null
IllegalAccessException
- if the field is not accessible or is finalpublic static void writeField(Field field, Object target, Object value, boolean forceAccess) throws IllegalAccessException
field
- to writetarget
- the object to call on, may be null for static fieldsvalue
- to setforceAccess
- whether to break scope restrictions using the
setAccessible
method. False
will only
match public fields.
IllegalArgumentException
- if the field is null
IllegalAccessException
- if the field is not made accessible or is finalpublic static void writeField(Object target, String fieldName, Object value) throws IllegalAccessException
target
- the object to reflect, must not be nullfieldName
- the field name to obtainvalue
- to set
IllegalArgumentException
- if target
or fieldName
is null
IllegalAccessException
- if the field is not accessiblepublic static void writeField(Object target, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException
target
- the object to reflect, must not be nullfieldName
- the field name to obtainvalue
- to setforceAccess
- whether to break scope restrictions using the
setAccessible
method. False
will only
match public fields.
IllegalArgumentException
- if target
or fieldName
is null
IllegalAccessException
- if the field is not made accessiblepublic static void writeDeclaredField(Object target, String fieldName, Object value) throws IllegalAccessException
target
- the object to reflect, must not be nullfieldName
- the field name to obtainvalue
- to set
IllegalArgumentException
- if target
or fieldName
is null
IllegalAccessException
- if the field is not made accessiblepublic static void writeDeclaredField(Object target, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException
target
- the object to reflect, must not be nullfieldName
- the field name to obtainvalue
- to setforceAccess
- whether to break scope restrictions using the
setAccessible
method. False
will only
match public fields.
IllegalArgumentException
- if target
or fieldName
is null
IllegalAccessException
- if the field is not made accessible
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |