Package org.apache.commons.lang3.reflect
Class FieldUtils
- java.lang.Object
-
- org.apache.commons.lang3.reflect.FieldUtils
-
public class FieldUtils extends java.lang.ObjectUtilities for working withFields 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.TODO Make private in 4.0.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method 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.FieldgetDeclaredField(java.lang.Class<?> cls, java.lang.String fieldName)Gets an accessibleFieldby name respecting scope.static java.lang.reflect.FieldgetDeclaredField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess)Gets an accessibleFieldby name, breaking scope if requested.static java.lang.reflect.FieldgetField(java.lang.Class<?> cls, java.lang.String fieldName)Gets an accessibleFieldby name respecting scope.static java.lang.reflect.FieldgetField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess)Gets an accessibleFieldby 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.ObjectreadDeclaredField(java.lang.Object target, java.lang.String fieldName)Reads the namedpublicField.static java.lang.ObjectreadDeclaredField(java.lang.Object target, java.lang.String fieldName, boolean forceAccess)Gets aFieldvalue by name.static java.lang.ObjectreadDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName)Gets the value of astaticFieldby name.static java.lang.ObjectreadDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess)Gets the value of astaticFieldby name.static java.lang.ObjectreadField(java.lang.Object target, java.lang.String fieldName)Reads the namedpublicField.static java.lang.ObjectreadField(java.lang.Object target, java.lang.String fieldName, boolean forceAccess)Reads the namedField.static java.lang.ObjectreadField(java.lang.reflect.Field field, java.lang.Object target)Reads an accessibleField.static java.lang.ObjectreadField(java.lang.reflect.Field field, java.lang.Object target, boolean forceAccess)Reads aField.static java.lang.ObjectreadStaticField(java.lang.Class<?> cls, java.lang.String fieldName)Reads the namedpublic staticField.static java.lang.ObjectreadStaticField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess)Reads the namedstaticField.static java.lang.ObjectreadStaticField(java.lang.reflect.Field field)Reads an accessiblestaticField.static java.lang.ObjectreadStaticField(java.lang.reflect.Field field, boolean forceAccess)Reads a staticField.static voidremoveFinalModifier(java.lang.reflect.Field field)Removes the final modifier from aField.static voidremoveFinalModifier(java.lang.reflect.Field field, boolean forceAccess)Deprecated.As of Java 12, we can no longer drop thefinalmodifier, thus rendering this method obsolete.static voidwriteDeclaredField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value)Writes apublicField.static voidwriteDeclaredField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value, boolean forceAccess)Writes apublicField.static voidwriteDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value)Writes a namedpublic staticField.static voidwriteDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value, boolean forceAccess)Writes a namedstaticField.static voidwriteField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value)Writes apublicField.static voidwriteField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value, boolean forceAccess)Writes aField.static voidwriteField(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value)Writes an accessibleField.static voidwriteField(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value, boolean forceAccess)Writes aField.static voidwriteStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value)Writes a namedpublic staticField.static voidwriteStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value, boolean forceAccess)Writes a namedstaticField.static voidwriteStaticField(java.lang.reflect.Field field, java.lang.Object value)Writes apublic staticField.static voidwriteStaticField(java.lang.reflect.Field field, java.lang.Object value, boolean forceAccess)Writes a staticField.
-
-
-
Constructor Detail
-
FieldUtils
@Deprecated public FieldUtils()
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.
-
-
Method Detail
-
getAllFields
public static java.lang.reflect.Field[] getAllFields(java.lang.Class<?> cls)
Gets all fields of the given class and its parents (if any).- Parameters:
cls- theClassto query- Returns:
- an array of Fields (possibly empty).
- Throws:
java.lang.NullPointerException- if the class isnull.- Since:
- 3.2
-
getAllFieldsList
public 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).- Parameters:
cls- theClassto query- Returns:
- a list of Fields (possibly empty).
- Throws:
java.lang.NullPointerException- if the class isnull.- Since:
- 3.2
-
getDeclaredField
public static java.lang.reflect.Field getDeclaredField(java.lang.Class<?> cls, java.lang.String fieldName)Gets an accessibleFieldby name respecting scope. Only the specified class will be considered.- Parameters:
cls- theClassto reflect, must not benullfieldName- the field name to obtain.- Returns:
- the Field object.
- Throws:
java.lang.NullPointerException- if the class isnull.java.lang.IllegalArgumentException- if the field name isnull, blank, or empty.java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
getDeclaredField
public static java.lang.reflect.Field getDeclaredField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess)Gets an accessibleFieldby name, breaking scope if requested. Only the specified class will be considered.- Parameters:
cls- theClassto reflect, must not benull.fieldName- the field name to obtain.forceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Returns:
- the Field object
- Throws:
java.lang.NullPointerException- if the class isnull.java.lang.IllegalArgumentException- if the field name isnull, blank, or empty.java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
getField
public static java.lang.reflect.Field getField(java.lang.Class<?> cls, java.lang.String fieldName)Gets an accessibleFieldby name respecting scope. Superclasses/interfaces will be considered.- Parameters:
cls- theClassto reflect, must not benull.fieldName- the field name to obtain.- Returns:
- the Field object.
- Throws:
java.lang.NullPointerException- if the class isnull.java.lang.IllegalArgumentException- if the field name isnull, blank, or emptyjava.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
getField
public static java.lang.reflect.Field getField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess)Gets an accessibleFieldby name, breaking scope if requested. Superclasses/interfaces will be considered.- Parameters:
cls- theClassto reflect, must not benull.fieldName- the field name to obtain.forceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Returns:
- the Field object.
- Throws:
java.lang.NullPointerException- if the class isnull.java.lang.IllegalArgumentException- if the field name is blank or empty or is matched at multiple places in the inheritance hierarchy.java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
getFieldsListWithAnnotation
public 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.- Parameters:
cls- theClassto query.annotationCls- theAnnotationthat must be present on a field to be matched.- Returns:
- a list of Fields (possibly empty).
- Throws:
java.lang.NullPointerException- if the class or annotation arenull.- Since:
- 3.4
-
getFieldsWithAnnotation
public 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.- Parameters:
cls- theClassto query.annotationCls- theAnnotationthat must be present on a field to be matched- Returns:
- an array of Fields (possibly empty).
- Throws:
java.lang.NullPointerException- if the class or annotation arenull.- Since:
- 3.4
-
readDeclaredField
public static java.lang.Object readDeclaredField(java.lang.Object target, java.lang.String fieldName) throws java.lang.IllegalAccessExceptionReads the namedpublicField. Only the class of the specified object will be considered.- Parameters:
target- the object to reflect, must not benull.fieldName- the field name to obtain.- Returns:
- the value of the field.
- Throws:
java.lang.NullPointerException- iftargetisnull.java.lang.IllegalArgumentException- iffieldNameisnull, blank or empty, or could not be found.java.lang.IllegalAccessException- if the named field is notpublicjava.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
readDeclaredField
public static java.lang.Object readDeclaredField(java.lang.Object target, java.lang.String fieldName, boolean forceAccess) throws java.lang.IllegalAccessExceptionGets aFieldvalue by name. Only the class of the specified object will be considered.- Parameters:
target- the object to reflect, must not benull.fieldName- the field name to obtain.forceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only match public fields.- Returns:
- the Field object.
- Throws:
java.lang.NullPointerException- iftargetisnull.java.lang.IllegalArgumentException- iffieldNameisnull, blank or empty, or could not be found.java.lang.IllegalAccessException- if the field is not made accessible.java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
readDeclaredStaticField
public static java.lang.Object readDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName) throws java.lang.IllegalAccessExceptionGets the value of astaticFieldby name. The field must bepublic. Only the specified class will be considered.- Parameters:
cls- theClassto reflect, must not benull.fieldName- the field name to obtain.- Returns:
- the value of the field.
- Throws:
java.lang.NullPointerException- if the class isnull, or the field could not be found.java.lang.IllegalArgumentException- if the field name isnull, blank, empty, or is notstatic.java.lang.IllegalAccessException- if the field is not accessiblejava.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
readDeclaredStaticField
public static java.lang.Object readDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess) throws java.lang.IllegalAccessExceptionGets the value of astaticFieldby name. Only the specified class will be considered.- Parameters:
cls- theClassto reflect, must not benull.fieldName- the field name to obtain.forceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Returns:
- the Field object
- Throws:
java.lang.NullPointerException- if the class isnull, or the field could not be found.java.lang.IllegalArgumentException- if the field name is blank or empty, is notstatic.java.lang.IllegalAccessException- if the field is not made accessiblejava.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
readField
public static java.lang.Object readField(java.lang.reflect.Field field, java.lang.Object target) throws java.lang.IllegalAccessExceptionReads an accessibleField.- Parameters:
field- the field to use.target- the object to call on, may benullforstaticfields.- Returns:
- the field value
- Throws:
java.lang.NullPointerException- if the field isnull.java.lang.IllegalAccessException- if the field is not accessible.java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
readField
public static java.lang.Object readField(java.lang.reflect.Field field, java.lang.Object target, boolean forceAccess) throws java.lang.IllegalAccessExceptionReads aField.- Parameters:
field- the field to use.target- the object to call on, may benullforstaticfields.forceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.- Returns:
- the field value
- Throws:
java.lang.NullPointerException- if the field isnull.java.lang.IllegalAccessException- if the field is not made accessible.java.lang.SecurityException- if an underlying accessible object's method denies the request.java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission),SecurityManager.checkPermission(java.security.Permission)
-
readField
public static java.lang.Object readField(java.lang.Object target, java.lang.String fieldName) throws java.lang.IllegalAccessExceptionReads the namedpublicField. Superclasses will be considered.- Parameters:
target- the object to reflect, must not benull.fieldName- the field name to obtain.- Returns:
- the value of the field.
- Throws:
java.lang.NullPointerException- if the target isnull.java.lang.IllegalArgumentException- if the field name isnull, blank, empty, or could not be found.java.lang.IllegalAccessException- if the named field is notpublic.java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
readField
public static java.lang.Object readField(java.lang.Object target, java.lang.String fieldName, boolean forceAccess) throws java.lang.IllegalAccessExceptionReads the namedField. Superclasses will be considered.- Parameters:
target- the object to reflect, must not benull.fieldName- the field name to obtain.forceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Returns:
- the field value
- Throws:
java.lang.NullPointerException- iftargetisnull.java.lang.IllegalArgumentException- if the field name isnull, blank, empty, or could not be found.java.lang.IllegalAccessException- if the named field is not made accessible.java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
readStaticField
public static java.lang.Object readStaticField(java.lang.Class<?> cls, java.lang.String fieldName) throws java.lang.IllegalAccessExceptionReads the namedpublic staticField. Superclasses will be considered.- Parameters:
cls- theClassto reflect, must not benull.fieldName- the field name to obtain.- Returns:
- the value of the field.
- Throws:
java.lang.NullPointerException- if the class isnull, or the field could not be found.java.lang.IllegalArgumentException- if the field name isnull, blank or empty, or is notstatic.java.lang.IllegalAccessException- if the field is not accessible.java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
readStaticField
public static java.lang.Object readStaticField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess) throws java.lang.IllegalAccessExceptionReads the namedstaticField. Superclasses will be considered.- Parameters:
cls- theClassto reflect, must not benull.fieldName- the field name to obtain.forceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Returns:
- the Field object.
- Throws:
java.lang.NullPointerException- if the class isnull, or the field could not be found.java.lang.IllegalArgumentException- if the field name isnull, blank or empty, or is notstatic.java.lang.IllegalAccessException- if the field is not made accessible.java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
readStaticField
public static java.lang.Object readStaticField(java.lang.reflect.Field field) throws java.lang.IllegalAccessExceptionReads an accessiblestaticField.- Parameters:
field- to read.- Returns:
- the field value.
- Throws:
java.lang.NullPointerException- if the field isnull.java.lang.IllegalArgumentException- if the field is notstatic.java.lang.IllegalAccessException- if the field is not accessiblejava.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
readStaticField
public static java.lang.Object readStaticField(java.lang.reflect.Field field, boolean forceAccess) throws java.lang.IllegalAccessExceptionReads a staticField.- Parameters:
field- to read.forceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.- Returns:
- the field value.
- Throws:
java.lang.NullPointerException- if the field isnull.java.lang.IllegalArgumentException- if the field is notstatic.java.lang.IllegalAccessException- if the field is not made accessible.java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
removeFinalModifier
public static void removeFinalModifier(java.lang.reflect.Field field)
Removes the final modifier from aField.- Parameters:
field- to remove the final modifier.- Throws:
java.lang.NullPointerException- if the field isnull.java.lang.SecurityException- if an underlying accessible object's method denies the request.- Since:
- 3.2
- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
removeFinalModifier
@Deprecated public static void removeFinalModifier(java.lang.reflect.Field field, boolean forceAccess)Deprecated.As of Java 12, we can no longer drop thefinalmodifier, 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 aField.- Parameters:
field- to remove the final modifier.forceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Throws:
java.lang.NullPointerException- if the field isnull.java.lang.SecurityException- if an underlying accessible object's method denies the request.- Since:
- 3.3
- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
writeDeclaredField
public static void writeDeclaredField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalAccessExceptionWrites apublicField. Only the specified class will be considered.- Parameters:
target- the object to reflect, must not benull.fieldName- the field name to obtain.value- the new value.- Throws:
java.lang.NullPointerException- iftargetisnull.java.lang.IllegalArgumentException- iffieldNameisnull, blank or empty, or could not be found, orvalueis not assignable.java.lang.IllegalAccessException- if the field is not made accessiblejava.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
writeDeclaredField
public static void writeDeclaredField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessExceptionWrites apublicField. Only the specified class will be considered.- Parameters:
target- the object to reflect, must not benull.fieldName- the field name to obtain.value- the new value.forceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Throws:
java.lang.IllegalArgumentException- iffieldNameisnull, blank or empty, or could not be found, orvalueis not assignablejava.lang.IllegalAccessException- if the field is not made accessiblejava.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
writeDeclaredStaticField
public static void writeDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalAccessExceptionWrites a namedpublic 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- ifclsisnullor the field cannot be located.java.lang.IllegalArgumentException- if the field name isnull, blank, empty, notstatic, orvalueis not assignable.java.lang.IllegalAccessException- if the field is notpublicor isfinaljava.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
writeDeclaredStaticField
public static void writeDeclaredStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessExceptionWrites a namedstaticField. Only the specified class will be considered.- Parameters:
cls-Classon which the field is to be found.fieldName- to writevalue- the new value.forceAccess- whether to break scope restrictions using theAccessibleObject#setAccessible(boolean)method.falsewill only matchpublicfields.- Throws:
java.lang.NullPointerException- ifclsisnullor the field cannot be located.java.lang.IllegalArgumentException- if the field name isnull, blank, empty, notstatic, orvalueis not assignable.java.lang.IllegalAccessException- if the field is not made accessible or isfinaljava.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
writeField
public static void writeField(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value) throws java.lang.IllegalAccessExceptionWrites an accessibleField.- Parameters:
field- to write.target- the object to call on, may benullforstaticfields.value- the new value.- Throws:
java.lang.NullPointerException- if the field isnull.java.lang.IllegalArgumentException- ifvalueis not assignable.java.lang.IllegalAccessException- if the field is not accessible or isfinal.java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
writeField
public static void writeField(java.lang.reflect.Field field, java.lang.Object target, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessExceptionWrites aField.- Parameters:
field- to write.target- the object to call on, may benullforstaticfieldsvalue- the new value.forceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Throws:
java.lang.NullPointerException- if the field isnull.java.lang.IllegalArgumentException- ifvalueis not assignable.java.lang.IllegalAccessException- if the field is not made accessible or isfinaljava.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
writeField
public static void writeField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalAccessExceptionWrites apublicField. Superclasses will be considered.- Parameters:
target- the object to reflect, must not benull.fieldName- the field name to obtain.value- the new value.- Throws:
java.lang.NullPointerException- iftargetisnull.java.lang.IllegalArgumentException- iffieldNameisnull, blank, empty, or could not be found, orvalueis not assignable.java.lang.IllegalAccessException- if the field is not accessible.java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
writeField
public static void writeField(java.lang.Object target, java.lang.String fieldName, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessExceptionWrites aField. Superclasses will be considered.- Parameters:
target- the object to reflect, must not benull.fieldName- the field name to obtain.value- the new value.forceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Throws:
java.lang.NullPointerException- iftargetisnull.java.lang.IllegalArgumentException- iffieldNameisnull, blank, empty, or could not be found, orvalueis not assignable.java.lang.IllegalAccessException- if the field is not made accessible.java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
writeStaticField
public static void writeStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value) throws java.lang.IllegalAccessExceptionWrites a namedpublic 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- iftargetisnull.java.lang.IllegalArgumentException- iffieldNameisnull, blank or empty, the field cannot be located or is notstatic, orvalueis not assignable.java.lang.IllegalAccessException- if the field is notpublicor isfinaljava.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
writeStaticField
public static void writeStaticField(java.lang.Class<?> cls, java.lang.String fieldName, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessExceptionWrites a namedstaticField. 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 theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Throws:
java.lang.NullPointerException- ifclsisnullor the field cannot be located.java.lang.IllegalArgumentException- iffieldNameisnull, blank or empty, the field notstatic, orvalueis not assignable.java.lang.IllegalAccessException- if the field is not made accessible or isfinal.java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
writeStaticField
public static void writeStaticField(java.lang.reflect.Field field, java.lang.Object value) throws java.lang.IllegalAccessExceptionWrites apublic staticField.- Parameters:
field- to write.value- the new value.- Throws:
java.lang.NullPointerException- if the field isnull.java.lang.IllegalArgumentException- if the field is notstatic, orvalueis not assignable.java.lang.IllegalAccessException- if the field is notpublicor isfinal.java.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
writeStaticField
public static void writeStaticField(java.lang.reflect.Field field, java.lang.Object value, boolean forceAccess) throws java.lang.IllegalAccessExceptionWrites a staticField.- Parameters:
field- to write.value- the new value.forceAccess- whether to break scope restrictions using theAccessibleObject.setAccessible(boolean)method.falsewill only matchpublicfields.- Throws:
java.lang.NullPointerException- if the field isnull.java.lang.IllegalArgumentException- if the field is notstatic, orvalueis not assignable.java.lang.IllegalAccessException- if the field is not made accessible or isfinaljava.lang.SecurityException- if an underlying accessible object's method denies the request.- See Also:
SecurityManager.checkPermission(java.security.Permission)
-
-