Class FieldUtils
- java.lang.Object
-
- org.assertj.core.util.introspection.FieldUtils
-
public class FieldUtils extends java.lang.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 java.lang.reflect.Field
getField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess)
Gets an accessibleField
by name breaking scope if requested.(package private) static java.lang.Object
readField(java.lang.Object target, java.lang.String fieldName, boolean forceAccess)
Reads the named field.private static java.lang.Object
readField(java.lang.reflect.Field field, java.lang.Object target)
Reads an accessible Field.private static java.lang.Object
readField(java.lang.reflect.Field field, java.lang.Object target, boolean forceAccess)
Reads a Field.
-
-
-
Method Detail
-
getField
static java.lang.reflect.Field getField(java.lang.Class<?> cls, java.lang.String fieldName, boolean forceAccess) throws java.lang.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:
java.lang.IllegalArgumentException
- if the class or field name is nulljava.lang.IllegalAccessException
- if field exists but is not public
-
readField
private static java.lang.Object readField(java.lang.reflect.Field field, java.lang.Object target) throws java.lang.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:
java.lang.IllegalArgumentException
- if the field is nulljava.lang.IllegalAccessException
- if the field is not accessible
-
readField
private static java.lang.Object readField(java.lang.reflect.Field field, java.lang.Object target, boolean forceAccess) throws java.lang.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:
java.lang.IllegalArgumentException
- if the field is nulljava.lang.IllegalAccessException
- if the field is not made accessible
-
readField
static java.lang.Object readField(java.lang.Object target, java.lang.String fieldName, boolean forceAccess) throws java.lang.IllegalAccessException
Reads 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 thesetAccessible
method.False
will only match public fields.- Returns:
- the field value
- Throws:
java.lang.IllegalArgumentException
- if the class or field name is nulljava.lang.IllegalAccessException
- if the named field is not made accessible
-
-