Top Description Fields Constructors Methods
org.assertj.core.util.introspection

pack-priv Class FieldUtils

extends Object
Class Inheritance
Static Imports
java.lang.reflect.Modifier.isStatic, org.assertj.core.util.Preconditions.checkArgument

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

Modifier and TypeField and Description
private static final Map<Class<?>, Map<String, Field>>

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
private static Field

Returns:

the Field corresponding to the given fieldName for the specified class.
getDeclaredField
(String
the name of the field to get
fieldName
,
Class<?>
the class to introspect
acls
)

Returns the Field corresponding to the given fieldName for the specified class.

pack-priv static Field

Returns:

the Field object
getField
(final Class<?>
the class to reflect, must not be null
cls
,
String
the field name to obtain
fieldName
,
boolean
whether to break scope restrictions using the setAccessible method. False will only match public fields.
forceAccess
)

Gets an accessible Field by name breaking scope if requested.

private static Object

Returns:

the field value
readField
(Field
the field to use
field
,
Object
the object to call on, may be null for static fields
target
)

Reads an accessible Field.

private static Object

Returns:

the field value
readField
(Field
the field to use
field
,
Object
the object to call on, may be null for static fields
target
,
boolean
whether to break scope restrictions using the setAccessible method.
forceAccess
)

Reads a Field.

pack-priv static Object

Returns:

the field value
readField
(Object
the object to reflect, must not be null
target
,
String
the field name to obtain
fieldName
,
boolean
whether to break scope restrictions using the setAccessible method. False will only match public fields.
forceAccess
)

Reads the named field.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

fieldsPerClassback to summary
private static final Map<Class<?>, Map<String, Field>> fieldsPerClass

Constructor Detail

FieldUtilsback to summary
pack-priv FieldUtils()

Method Detail

getDeclaredFieldback to summary
private static Field getDeclaredField(String fieldName, Class<?> acls) throws NoSuchFieldException

Returns the Field corresponding to the given fieldName for the specified class.

Caches the field after getting it for efficiency.

Parameters
fieldName:String

the name of the field to get

acls:Class<?>

the class to introspect

Returns:Field

the Field corresponding to the given fieldName for the specified class.

Exceptions
NoSuchFieldException:
bubbled up from the call to Class#getDeclaredField(String)
getFieldback to summary
pack-priv static Field getField(final Class<?> cls, String fieldName, boolean forceAccess) throws IllegalAccessException

Gets an accessible Field by name breaking scope if requested. Superclasses/interfaces will be considered.

Parameters
cls:Class<?>

the class to reflect, must not be null

fieldName:String

the field name to obtain

forceAccess:boolean

whether to break scope restrictions using the setAccessible method. False will only match public fields.

Returns:Field

the Field object

Exceptions
IllegalAccessException:
if field exists but is not public
IllegalArgumentException:
if the class or field name is null
readFieldback to summary
private static Object readField(Field field, Object target) throws IllegalAccessException

Reads an accessible Field.

Parameters
field:Field

the field to use

target:Object

the object to call on, may be null for static fields

Returns:Object

the field value

Exceptions
IllegalAccessException:
if the field is not accessible
IllegalArgumentException:
if the field is null
readFieldback to summary
private static Object readField(Field field, Object target, boolean forceAccess) throws IllegalAccessException

Reads a Field.

Parameters
field:Field

the field to use

target:Object

the object to call on, may be null for static fields

forceAccess:boolean

whether to break scope restrictions using the setAccessible method.

Returns:Object

the field value

Exceptions
IllegalAccessException:
if the field is not made accessible
IllegalArgumentException:
if the field is null
readFieldback to summary
pack-priv 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:Object

the object to reflect, must not be null

fieldName:String

the field name to obtain

forceAccess:boolean

whether to break scope restrictions using the setAccessible method. False will only match public fields.

Returns:Object

the field value

Exceptions
IllegalAccessException:
if the named field is not made accessible
IllegalArgumentException:
if the class or field name is null or the field can not be found.