Package io.cui.tools.reflect
Class FieldWrapper
- java.lang.Object
-
- io.cui.tools.reflect.FieldWrapper
-
public class FieldWrapper extends Object
Wrapper around aFieldthat handles implicitly the accessible flag for access.- Author:
- Oliver Wolff
-
-
Constructor Summary
Constructors Constructor Description FieldWrapper(Field field)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Optional<FieldWrapper>from(Class<?> type, String fieldName)Factory Method for creating anFieldWrapperinstance@NonNull FieldgetField()Optional<Object>readValue(Object object)Reads from the field determined bygetField().static Optional<Object>readValue(String fieldName, Object object)Reads the value from the field in the given object.voidwriteValue(@NonNull Object object, Object value)Writes to the field determined bygetField().
-
-
-
Constructor Detail
-
FieldWrapper
public FieldWrapper(Field field)
- Parameters:
field- must not be null
-
-
Method Detail
-
readValue
public Optional<Object> readValue(Object object)
Reads from the field determined bygetField(). 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
public static final Optional<Object> readValue(String fieldName, Object object)
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
public void writeValue(@NonNull @NonNull Object object, Object value)
Writes to the field determined bygetField(). 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
public static final Optional<FieldWrapper> from(Class<?> type, String fieldName)
Factory Method for creating anFieldWrapperinstance- Parameters:
type- must not be nullfieldName- must not be null- Returns:
- a
FieldWrapperif theFieldcan be determined,Optional.empty()otherwise
-
-