Class FieldUtils
java.lang.Object
org.assertj.core.util.introspection.FieldUtils
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.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static Field
getDeclaredField
(String fieldName, Class<?> acls) Returns theField
corresponding to the given fieldName for the specified class.(package private) static Field
Gets an accessibleField
by name breaking scope if requested.(package private) static Object
Reads the named field.private static Object
Reads an accessible Field.private static Object
Reads a Field.
-
Field Details
-
fieldsPerClass
-
-
Constructor Details
-
FieldUtils
FieldUtils()
-
-
Method Details
-
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
-
getDeclaredField
Returns theField
corresponding to the given fieldName for the specified class.Caches the field after getting it for efficiency.
- Parameters:
fieldName
- the name of the field to getacls
- the class to introspect- Returns:
- the
Field
corresponding to the given fieldName for the specified class. - Throws:
NoSuchFieldException
- bubbled up from the call toClass.getDeclaredField(String)
-
readField
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
-