Class FieldUtils
- java.lang.Object
-
- org.assertj.core.util.introspection.FieldUtils
-
class FieldUtils extends Object
Shameless copy from Apache commons lang and then modified to keep only the interesting stuff for AssertJ. 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
Constructors Constructor Description FieldUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description (package private) static Field
getField(Class<?> cls, String fieldName, boolean forceAccess)
Gets an accessibleField
by name breaking scope if requested.(package private) static Object
readField(Object target, String fieldName, boolean forceAccess)
Reads the named field.private static Object
readField(Field field, Object target)
Reads an accessible Field.private static Object
readField(Field field, Object target, boolean forceAccess)
Reads a Field.
-
-
-
Method Detail
-
getField
static Field getField(Class<?> cls, String fieldName, boolean forceAccess) throws IllegalAccessException
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:
IllegalArgumentException
- if the class or field name is nullIllegalAccessException
- if field exists but is not public
-
readField
private static Object readField(Field field, Object target) throws IllegalAccessException
Reads 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
private static Object readField(Field field, Object target, boolean forceAccess) throws IllegalAccessException
Reads 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:
IllegalArgumentException
- if the field is nullIllegalAccessException
- if the field is not made accessible
-
readField
static Object readField(Object target, String fieldName, boolean forceAccess) throws IllegalAccessException
Reads the named field. Superclasses will be considered.Since 3.19.0 static and synthetic fields are ignored.
- 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:
IllegalArgumentException
- if the class or field name is null or the field can not be found.IllegalAccessException
- if the named field is not made accessible
-
-