Package org.apache.commons.lang.reflect
Class FieldUtils
- java.lang.Object
-
- org.apache.commons.lang.reflect.FieldUtils
-
@Deprecated(since="2021-04-30") public class FieldUtils extends java.lang.Object
Deprecated.Commons Lang 2 is in maintenance mode. Commons Lang 3 should be used instead.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
-
-
Constructor Summary
Constructors Constructor Description FieldUtils()
Deprecated.FieldUtils instances should NOT be constructed in standard programming.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.lang.reflect.Field
getDeclaredField(java.lang.Class cls, java.lang.String fieldName)
Deprecated.Gets an accessibleField
by name respecting scope.static java.lang.reflect.Field
getDeclaredField(java.lang.Class cls, java.lang.String fieldName, boolean forceAccess)
Deprecated.Gets an accessibleField
by name breaking scope if requested.static java.lang.reflect.Field
getField(java.lang.Class cls, java.lang.String fieldName)
Deprecated.Gets an accessibleField
by name respecting scope.static java.lang.reflect.Field
getField(java.lang.Class cls, java.lang.String fieldName, boolean forceAccess)
Deprecated.Gets an accessibleField
by name breaking scope if requested.static java.lang.Object
readDeclaredField(java.lang.Object target, java.lang.String fieldName)
Deprecated.Read the named public field.static java.lang.Object
readDeclaredField(java.lang.Object target, java.lang.String fieldName, boolean forceAccess)
Deprecated.Gets a Field value by name.
static java.lang.Object
readDeclaredStaticField(java.lang.Class cls, java.lang.String fieldName)
Deprecated.Gets a static Field value by name.static java.lang.Object
readDeclaredStaticField(java.lang.Class cls, java.lang.String fieldName, boolean forceAccess)
Deprecated.Gets a static Field value by name.static java.lang.Object
readField(java.lang.Object target, java.lang.String fieldName)
Deprecated.Read the named public field.static java.lang.Object
readField(java.lang.Object target, java.lang.String fieldName, boolean forceAccess)
Deprecated.Read the named field.static java.lang.Object
readField(java.lang.reflect.Field field, java.lang.Object target)
Deprecated.Read an accessible Field.static java.lang.Object
readField(java.lang.reflect.Field field, java.lang.Object target, boolean forceAccess)
Deprecated.Read a Field.static java.lang.Object
readStaticField(java.lang.Class cls, java.lang.String fieldName)
Deprecated.Read the named public static field.static java.lang.Object
readStaticField(java.lang.Class cls, java.lang.String fieldName, boolean forceAccess)
Deprecated.Read the named static field.static java.lang.Object
readStaticField(java.lang.reflect.Field field)
Deprecated.Read an accessible static Field.static java.lang.Object
readStaticField(java.lang.reflect.Field field, boolean forceAccess)
Deprecated.Read a static Field.static void
writeDeclaredField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value)
Deprecated.Write a public field.static void
writeDeclaredField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value, boolean forceAccess)
Deprecated.Write a public field.static void
writeDeclaredStaticField(java.lang.Class cls, java.lang.String fieldName, java.lang.Object value)
Deprecated.Write a named public static Field.static void
writeDeclaredStaticField(java.lang.Class cls, java.lang.String fieldName, java.lang.Object value, boolean forceAccess)
Deprecated.Write a named static Field.static void
writeField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value)
Deprecated.Write a public field.static void
writeField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value, boolean forceAccess)
Deprecated.Write a field.static void
writeField(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value)
Deprecated.Write an accessible field.static void
writeField(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value, boolean forceAccess)
Deprecated.Write a field.static void
writeStaticField(java.lang.Class cls, java.lang.String fieldName, java.lang.Object value)
Deprecated.Write a named public static Field.static void
writeStaticField(java.lang.Class cls, java.lang.String fieldName, java.lang.Object value, boolean forceAccess)
Deprecated.Write a named static Field.static void
writeStaticField(java.lang.reflect.Field field, java.lang.Object value)
Deprecated.Write a public static Field.static void
writeStaticField(java.lang.reflect.Field field, java.lang.Object value, boolean forceAccess)
Deprecated.Write a static Field.
-
-
-
Method Detail
-
getField
public static java.lang.reflect.Field getField(java.lang.Class cls, java.lang.String fieldName)
Deprecated.Gets an accessibleField
by name respecting scope. Superclasses/interfaces will be considered.- Parameters:
cls
- the class to reflect, must not be nullfieldName
- the field name to obtain- Returns:
- the Field object
- Throws:
java.lang.IllegalArgumentException
- if the class or field name is null
-
getField
public static java.lang.reflect.Field getField(java.lang.Class cls, java.lang.String fieldName, boolean forceAccess)
Deprecated.Gets an accessibleField
by name breaking scope if requested. Superclasses/interfaces will be considered.- Parameters:
cls
- the class to reflect, must not be nullfieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using thesetAccessible
method.False
will only match public fields.- Returns:
- the Field object
- Throws:
java.lang.IllegalArgumentException
- if the class or field name is null
-
getDeclaredField
public static java.lang.reflect.Field getDeclaredField(java.lang.Class cls, java.lang.String fieldName)
Deprecated.Gets an accessibleField
by name respecting scope. Only the specified class will be considered.- Parameters:
cls
- the class to reflect, must not be nullfieldName
- the field name to obtain- Returns:
- the Field object
- Throws:
java.lang.IllegalArgumentException
- if the class or field name is null
-
getDeclaredField
public static java.lang.reflect.Field getDeclaredField(java.lang.Class cls, java.lang.String fieldName, boolean forceAccess)
Deprecated.Gets an accessibleField
by name breaking scope if requested. Only the specified class will be considered.- Parameters:
cls
- the class to reflect, must not be nullfieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using thesetAccessible
method. False will only match public fields.- Returns:
- the Field object
- Throws:
java.lang.IllegalArgumentException
- if the class or field name is null
-
readStaticField
public static java.lang.Object readStaticField(java.lang.reflect.Field field) throws java.lang.IllegalAccessException
Deprecated.Read an accessible static Field.- Parameters:
field
- to read- Returns:
- the field value
- Throws:
java.lang.IllegalArgumentException
- if the field is null or not staticjava.lang.IllegalAccessException
- if the field is not accessible
-
readStaticField
public static java.lang.Object readStaticField(java.lang.reflect.Field field, boolean forceAccess) throws java.lang.IllegalAccessException
Deprecated.Read a static Field.- Parameters:
field
- to readforceAccess
- whether to break scope restrictions using thesetAccessible
method.- Returns:
- the field value
- Throws:
java.lang.IllegalArgumentException
- if the field is null or not staticjava.lang.IllegalAccessException
- if the field is not made accessible
-
readStaticField
public static java.lang.Object readStaticField(java.lang.Class cls, java.lang.String fieldName) throws java.lang.IllegalAccessException
Deprecated.Read the named public static field. Superclasses will be considered.- Parameters:
cls
- the class to reflect, must not be nullfieldName
- the field name to obtain- Returns:
- the value of the field
- Throws:
java.lang.IllegalArgumentException
- if the class or field name is nulljava.lang.IllegalAccessException
- if the field is not accessible
-
readStaticField
public static java.lang.Object readStaticField(java.lang.Class cls, java.lang.String fieldName, boolean forceAccess) throws java.lang.IllegalAccessException
Deprecated.Read the named static field. Superclasses will be considered.- Parameters:
cls
- the class to reflect, must not be nullfieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using thesetAccessible
method.False
will only match public fields.- Returns:
- the Field object
- Throws:
java.lang.IllegalArgumentException
- if the class or field name is nulljava.lang.IllegalAccessException
- if the field is not made accessible
-
readDeclaredStaticField
public static java.lang.Object readDeclaredStaticField(java.lang.Class cls, java.lang.String fieldName) throws java.lang.IllegalAccessException
Deprecated.Gets a static Field value by name. The field must be public. Only the specified class will be considered.- Parameters:
cls
- the class to reflect, must not be nullfieldName
- the field name to obtain- Returns:
- the value of the field
- Throws:
java.lang.IllegalArgumentException
- if the class or field name is nulljava.lang.IllegalAccessException
- if the field is not accessible
-
readDeclaredStaticField
public static java.lang.Object readDeclaredStaticField(java.lang.Class cls, java.lang.String fieldName, boolean forceAccess) throws java.lang.IllegalAccessException
Deprecated.Gets a static Field value by name. Only the specified class will be considered.- Parameters:
cls
- the class to reflect, must not be nullfieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using thesetAccessible
method.False
will only match public fields.- Returns:
- the Field object
- Throws:
java.lang.IllegalArgumentException
- if the class or field name is nulljava.lang.IllegalAccessException
- if the field is not made accessible
-
readField
public static java.lang.Object readField(java.lang.reflect.Field field, java.lang.Object target) throws java.lang.IllegalAccessException
Deprecated.Read an accessible Field.- Parameters:
field
- the field to usetarget
- the object to call on, may be null for static fields- Returns:
- the field value
- Throws:
java.lang.IllegalArgumentException
- if the field is nulljava.lang.IllegalAccessException
- if the field is not accessible
-
readField
public static java.lang.Object readField(java.lang.reflect.Field field, java.lang.Object target, boolean forceAccess) throws java.lang.IllegalAccessException
Deprecated.Read a Field.- Parameters:
field
- the field to usetarget
- the object to call on, may be null for static fieldsforceAccess
- whether to break scope restrictions using thesetAccessible
method.- Returns:
- the field value
- Throws:
java.lang.IllegalArgumentException
- if the field is nulljava.lang.IllegalAccessException
- if the field is not made accessible
-
readField
public static java.lang.Object readField(java.lang.Object target, java.lang.String fieldName) throws java.lang.IllegalAccessException
Deprecated.Read the named public field. Superclasses will be considered.- Parameters:
target
- the object to reflect, must not be nullfieldName
- the field name to obtain- Returns:
- the value of the field
- Throws:
java.lang.IllegalArgumentException
- if the class or field name is nulljava.lang.IllegalAccessException
- if the named field is not public
-
readField
public static java.lang.Object readField(java.lang.Object target, java.lang.String fieldName, boolean forceAccess) throws java.lang.IllegalAccessException
Deprecated.Read the named field. Superclasses will be considered.- Parameters:
target
- the object to reflect, must not be nullfieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using thesetAccessible
method.False
will only match public fields.- Returns:
- the field value
- Throws:
java.lang.IllegalArgumentException
- if the class or field name is nulljava.lang.IllegalAccessException
- if the named field is not made accessible
-
readDeclaredField
public static java.lang.Object readDeclaredField(java.lang.Object target, java.lang.String fieldName) throws java.lang.IllegalAccessException
Deprecated.Read the named public field. Only the class of the specified object will be considered.- Parameters:
target
- the object to reflect, must not be nullfieldName
- the field name to obtain- Returns:
- the value of the field
- Throws:
java.lang.IllegalArgumentException
- if the class or field name is nulljava.lang.IllegalAccessException
- if the named field is not public
-
readDeclaredField
public static java.lang.Object readDeclaredField(java.lang.Object target, java.lang.String fieldName, boolean forceAccess) throws java.lang.IllegalAccessException
Deprecated.Gets a Field value by name. Only the class of the specified object will be considered.
- Parameters:
target
- the object to reflect, must not be nullfieldName
- the field name to obtainforceAccess
- whether to break scope restrictions using thesetAccessible
method.False
will only match public fields.- Returns:
- the Field object
- Throws:
java.lang.IllegalArgumentException
- iftarget
orfieldName
is nulljava.lang.IllegalAccessException
- if the field is not made accessible
-
writeStaticField
public static void writeStaticField(java.lang.reflect.Field field, java.lang.Object value) throws java.lang.IllegalAccessException
Deprecated.Write a public static Field.- Parameters:
field
- to writevalue
- to set- Throws:
java.lang.IllegalArgumentException
- if the field is null or not staticjava.lang.IllegalAccessException
- if the field is not public or is final
-
writeStaticField
public static void writeStaticField(java.lang.reflect.Field field, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
Deprecated.Write a static Field.- Parameters:
field
- to writevalue
- to setforceAccess
- whether to break scope restrictions using thesetAccessible
method.False
will only match public fields.- Throws:
java.lang.IllegalArgumentException
- if the field is null or not staticjava.lang.IllegalAccessException
- if the field is not made accessible or is final
-
writeStaticField
public static void writeStaticField(java.lang.Class cls, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalAccessException
Deprecated.Write a named public static Field. Superclasses will be considered.- Parameters:
cls
- Class on which the Field is to be foundfieldName
- to writevalue
- to set- Throws:
java.lang.IllegalArgumentException
- if the field cannot be located or is not staticjava.lang.IllegalAccessException
- if the field is not public or is final
-
writeStaticField
public static void writeStaticField(java.lang.Class cls, java.lang.String fieldName, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
Deprecated.Write a named static Field. Superclasses will be considered.- Parameters:
cls
- Class on which the Field is to be foundfieldName
- to writevalue
- to setforceAccess
- whether to break scope restrictions using thesetAccessible
method.False
will only match public fields.- Throws:
java.lang.IllegalArgumentException
- if the field cannot be located or is not staticjava.lang.IllegalAccessException
- if the field is not made accessible or is final
-
writeDeclaredStaticField
public static void writeDeclaredStaticField(java.lang.Class cls, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalAccessException
Deprecated.Write a named public static Field. Only the specified class will be considered.- Parameters:
cls
- Class on which the Field is to be foundfieldName
- to writevalue
- to set- Throws:
java.lang.IllegalArgumentException
- if the field cannot be located or is not staticjava.lang.IllegalAccessException
- if the field is not public or is final
-
writeDeclaredStaticField
public static void writeDeclaredStaticField(java.lang.Class cls, java.lang.String fieldName, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
Deprecated.Write a named static Field. Only the specified class will be considered.- Parameters:
cls
- Class on which the Field is to be foundfieldName
- to writevalue
- to setforceAccess
- whether to break scope restrictions using thesetAccessible
method.False
will only match public fields.- Throws:
java.lang.IllegalArgumentException
- if the field cannot be located or is not staticjava.lang.IllegalAccessException
- if the field is not made accessible or is final
-
writeField
public static void writeField(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value) throws java.lang.IllegalAccessException
Deprecated.Write an accessible field.- Parameters:
field
- to writetarget
- the object to call on, may be null for static fieldsvalue
- to set- Throws:
java.lang.IllegalArgumentException
- if the field is nulljava.lang.IllegalAccessException
- if the field is not accessible or is final
-
writeField
public static void writeField(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
Deprecated.Write a field.- Parameters:
field
- to writetarget
- the object to call on, may be null for static fieldsvalue
- to setforceAccess
- whether to break scope restrictions using thesetAccessible
method.False
will only match public fields.- Throws:
java.lang.IllegalArgumentException
- if the field is nulljava.lang.IllegalAccessException
- if the field is not made accessible or is final
-
writeField
public static void writeField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalAccessException
Deprecated.Write a public field. Superclasses will be considered.- Parameters:
target
- the object to reflect, must not be nullfieldName
- the field name to obtainvalue
- to set- Throws:
java.lang.IllegalArgumentException
- iftarget
orfieldName
is nulljava.lang.IllegalAccessException
- if the field is not accessible
-
writeField
public static void writeField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
Deprecated.Write a field. Superclasses will be considered.- Parameters:
target
- the object to reflect, must not be nullfieldName
- the field name to obtainvalue
- to setforceAccess
- whether to break scope restrictions using thesetAccessible
method.False
will only match public fields.- Throws:
java.lang.IllegalArgumentException
- iftarget
orfieldName
is nulljava.lang.IllegalAccessException
- if the field is not made accessible
-
writeDeclaredField
public static void writeDeclaredField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalAccessException
Deprecated.Write a public field. Only the specified class will be considered.- Parameters:
target
- the object to reflect, must not be nullfieldName
- the field name to obtainvalue
- to set- Throws:
java.lang.IllegalArgumentException
- iftarget
orfieldName
is nulljava.lang.IllegalAccessException
- if the field is not made accessible
-
writeDeclaredField
public static void writeDeclaredField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessException
Deprecated.Write a public field. Only the specified class will be considered.- Parameters:
target
- the object to reflect, must not be nullfieldName
- the field name to obtainvalue
- to setforceAccess
- whether to break scope restrictions using thesetAccessible
method.False
will only match public fields.- Throws:
java.lang.IllegalArgumentException
- iftarget
orfieldName
is nulljava.lang.IllegalAccessException
- if the field is not made accessible
-
-