java.lang.Object
de.cuioss.tools.reflect.FieldWrapper
Wrapper around a
Field that handles implicitly the accessible flag
for access.- Author:
- Oliver Wolff
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final Optional<FieldWrapper>Factory Method for creating anFieldWrapperinstanceReads from the field determined by#getField().Reads the value from the field in the given object.voidwriteValue(@NonNull Object object, Object value) Writes to the field determined by#getField().
-
Constructor Details
-
FieldWrapper
- Parameters:
field- must not be null
-
-
Method Details
-
readValue
Reads from the field determined by#getField(). It implicitly sets and resets theAccessibleObject.isAccessible()flag.- Parameters:
object- to be read from- Returns:
- an
Optionalon the given field value if applicable. May returnOptional.empty()for cases where:- Field value is
null - Given Object is
null - Given Object is improper type
- an
IllegalAccessExceptionoccurred while accessing
- Field value is
-
readValue
Reads the value from the field in the given object. It implicitly sets and resets theAccessibleObject.isAccessible()flag.- Parameters:
fieldName- to be readobject- to be read from- Returns:
- the field value.
Optional.empty()if the field cannot be read.
-
writeValue
Writes to the field determined by#getField(). It implicitly sets and resets theAccessibleObject.isAccessible()flag.- Parameters:
object- to be written to, must not be nullvalue- to be written, may be null- Throws:
NullPointerException- in case object isnullIllegalArgumentException- in case the value is not applicable to the fieldIllegalStateException- wrapping anIllegalAccessException
-
from
Factory Method for creating anFieldWrapperinstance- Parameters:
type- must not be nullfieldName- must not be null- Returns:
- a
FieldWrapperif theFieldcan be determined,Optional.empty()otherwise
-