public class SafeField
extends java.lang.Object
Constructor and Description |
---|
SafeField(java.lang.reflect.Field field)
Constructs a safe reflection field for the given field.
|
Modifier and Type | Method and Description |
---|---|
<T> java.util.Optional<T> |
get(java.lang.Object instance,
java.lang.Class<T> type)
Gets the value assigned to the instance field and casts it to the provided type.
|
java.lang.reflect.Field |
getField() |
<T> java.util.Optional<T> |
getStatic(java.lang.Class<T> type)
Gets the value assigned to a class field and casts it to the provided type.
|
public SafeField(java.lang.reflect.Field field)
field
- the fieldpublic <T> java.util.Optional<T> get(java.lang.Object instance, java.lang.Class<T> type)
T
- the type to cast toinstance
- the object instancetype
- the value typejava.lang.IllegalAccessException
- if the Field object is enforcing Java language access control and the
underlying field is inaccessiblejava.lang.IllegalArgumentException
- if the specified object is not an instance of the class or interface declaring
the underlying field (or a subclass or implementor thereof)public <T> java.util.Optional<T> getStatic(java.lang.Class<T> type)
T
- the type to cast totype
- the value typepublic java.lang.reflect.Field getField()