Package com.comphenix.protocol.reflect
Class FieldUtils
java.lang.Object
com.comphenix.protocol.reflect.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.
- Since:
- 2.5
- Version:
- $Id: FieldUtils.java 1057009 2011-01-09 19:48:06Z niallp $
- Author:
- Apache Software Foundation, Matt Benson
-
Constructor Summary
ConstructorsConstructorDescriptionFieldUtils instances should NOT be constructed in standard programming. -
Method Summary
Modifier and TypeMethodDescriptionstatic FieldGets an accessibleFieldby name respecting scope.static FieldGets an accessibleFieldby name breaking scope if requested.static ObjectRead the named public field.static ObjectRead the named field.static ObjectRead an accessible Field.static ObjectRead a Field.static ObjectreadStaticField(Class cls, String fieldName)Read the named public static field.static ObjectreadStaticField(Class cls, String fieldName, boolean forceAccess)Read the named static field.static ObjectreadStaticField(Field field)Read an accessible static Field.static ObjectreadStaticField(Field field, boolean forceAccess)Read a static Field.static voidwriteField(Object target, String fieldName, Object value)Write a public field.static voidwriteField(Object target, String fieldName, Object value, boolean forceAccess)Write a field.static voidwriteField(Field field, Object target, Object value)Write an accessible field.static voidwriteField(Field field, Object target, Object value, boolean forceAccess)Write a field.static voidwriteStaticField(Class cls, String fieldName, Object value)Write a named public static Field.static voidwriteStaticField(Class cls, String fieldName, Object value, boolean forceAccess)Write a named static Field.static voidwriteStaticField(Field field, Object value)Write a public static Field.static voidwriteStaticField(Field field, Object value, boolean forceAccess)Write a static Field.static voidwriteStaticFinalField(Class<?> clazz, String fieldName, Object value, boolean forceAccess)
-
Constructor Details
-
FieldUtils
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.
-
-
Method Details
-
getField
Gets an accessibleFieldby 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:
IllegalArgumentException- if the class or field name is null
-
getField
Gets an accessibleFieldby 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 thesetAccessiblemethod.Falsewill only match public fields.- Returns:
- the Field object
- Throws:
IllegalArgumentException- if the class or field name is null
-
readStaticField
Read an accessible static Field.- Parameters:
field- to read- Returns:
- the field value
- Throws:
IllegalArgumentException- if the field is null or not staticIllegalAccessException- if the field is not accessible
-
readStaticField
public static Object readStaticField(Field field, boolean forceAccess) throws IllegalAccessExceptionRead a static Field.- Parameters:
field- to readforceAccess- whether to break scope restrictions using thesetAccessiblemethod.- Returns:
- the field value
- Throws:
IllegalArgumentException- if the field is null or not staticIllegalAccessException- if the field is not made accessible
-
readStaticField
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:
IllegalArgumentException- if the class or field name is nullIllegalAccessException- if the field is not accessible
-
readStaticField
public static Object readStaticField(Class cls, String fieldName, boolean forceAccess) throws IllegalAccessExceptionRead 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 thesetAccessiblemethod.Falsewill only match public fields.- Returns:
- the Field object
- Throws:
IllegalArgumentException- if the class or field name is nullIllegalAccessException- if the field is not made accessible
-
readField
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:
IllegalArgumentException- if the field is nullIllegalAccessException- if the field is not accessible
-
readField
public static Object readField(Field field, Object target, boolean forceAccess) throws IllegalAccessExceptionRead 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 thesetAccessiblemethod.- Returns:
- the field value
- Throws:
IllegalArgumentException- if the field is nullIllegalAccessException- if the field is not made accessible
-
readField
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:
IllegalArgumentException- if the class or field name is nullIllegalAccessException- if the named field is not public
-
readField
public static Object readField(Object target, String fieldName, boolean forceAccess) throws IllegalAccessExceptionRead 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 thesetAccessiblemethod.Falsewill only match public fields.- Returns:
- the field value
- Throws:
IllegalArgumentException- if the class or field name is nullIllegalAccessException- if the named field is not made accessible
-
writeStaticField
Write a public static Field.- Parameters:
field- to writevalue- to set- Throws:
IllegalArgumentException- if the field is null or not staticIllegalAccessException- if the field is not public or is final
-
writeStaticField
public static void writeStaticField(Field field, Object value, boolean forceAccess) throws IllegalAccessExceptionWrite a static Field.- Parameters:
field- to writevalue- to setforceAccess- whether to break scope restrictions using thesetAccessiblemethod.Falsewill only match public fields.- Throws:
IllegalArgumentException- if the field is null or not staticIllegalAccessException- if the field is not made accessible or is final
-
writeStaticField
public static void writeStaticField(Class cls, String fieldName, Object value) throws IllegalAccessExceptionWrite 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:
IllegalArgumentException- if the field cannot be located or is not staticIllegalAccessException- if the field is not public or is final
-
writeStaticField
public static void writeStaticField(Class cls, String fieldName, Object value, boolean forceAccess) throws IllegalAccessExceptionWrite 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 thesetAccessiblemethod.Falsewill only match public fields.- Throws:
IllegalArgumentException- if the field cannot be located or is not staticIllegalAccessException- if the field is not made accessible or is final
-
writeStaticFinalField
public static void writeStaticFinalField(Class<?> clazz, String fieldName, Object value, boolean forceAccess) throws Exception- Throws:
Exception
-
writeField
public static void writeField(Field field, Object target, Object value) throws IllegalAccessExceptionWrite an accessible field.- Parameters:
field- to writetarget- the object to call on, may be null for static fieldsvalue- to set- Throws:
IllegalArgumentException- if the field is nullIllegalAccessException- if the field is not accessible or is final
-
writeField
public static void writeField(Field field, Object target, Object value, boolean forceAccess) throws IllegalAccessExceptionWrite 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 thesetAccessiblemethod.Falsewill only match public fields.- Throws:
IllegalArgumentException- if the field is nullIllegalAccessException- if the field is not made accessible or is final
-
writeField
public static void writeField(Object target, String fieldName, Object value) throws IllegalAccessExceptionWrite 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:
IllegalArgumentException- iftargetorfieldNameis nullIllegalAccessException- if the field is not accessible
-
writeField
public static void writeField(Object target, String fieldName, Object value, boolean forceAccess) throws IllegalAccessExceptionWrite 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 thesetAccessiblemethod.Falsewill only match public fields.- Throws:
IllegalArgumentException- iftargetorfieldNameis nullIllegalAccessException- if the field is not made accessible
-