Package org.vaadin.miki.util
Class ReflectTools
java.lang.Object
org.vaadin.miki.util.ReflectTools
A collection of static methods to access things through reflection.
- Since:
- 2020-05-03
- Author:
- miki
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionextractFieldsWithMethods(Class<?> type, boolean ignoreSuperclasses) Scans a class using reflection and associates fields with getters (first) and setters (second).extractGenericType(Field field, int genericIndex) Extracts the class present as the given parameter in the generic definition of the type in the given field.static <V> Optional<V>getValueOfField(Object instance, Class<? extends V> valueType, String fieldName) Attempts to get the value of aFieldof a given name that is declared in given class.static <T> TnewInstance(Class<T> type) Creates an instance of a given type through a publicly accessible no-arg constructor.
-
Field Details
-
GETTER_INDEX
public static final int GETTER_INDEX- See Also:
-
SETTER_INDEX
public static final int SETTER_INDEX- See Also:
-
-
Method Details
-
getValueOfField
public static <V> Optional<V> getValueOfField(Object instance, Class<? extends V> valueType, String fieldName) Attempts to get the value of aFieldof a given name that is declared in given class.AccessibleObject.trySetAccessible()will be used. This method will attempt to find a field in the class of the given object, and then it will go up the hierarchy until the field of given name and a compatible type is found. When a field is found, an attempt totrySetAccessiblewill be made, followed by casting it to the provided value type. When all the above is successful, an optional with the value of the field for the given object will be returned.- Type Parameters:
V- Type of the returned object.- Parameters:
instance- Instance of an object to get the field from.valueType- Type of value to expect from the field. Type of the field, if found, will be passed to this type'sClass.isAssignableFrom(Class)for type checking.fieldName- Name of the field.- Returns:
- Value, if successfully obtained. Otherwise,
Optional.empty().
-
extractFieldsWithMethods
public static Map<Field,Method[]> extractFieldsWithMethods(Class<?> type, boolean ignoreSuperclasses) Scans a class using reflection and associates fields with getters (first) and setters (second). First, it scans all the fields in the given class, then moves up the hierarchy if told to and scans fields on the way. Then, it attempts to find matchingpublic, non-staticgetters/setters intype, and returns those. Setters must start withset, getters withget(isandaresupported for booleans). Return type of the getter must be the same as the field's type or one of its superclasses or superinterfaces. The only parameter of the setter must either be the same type as the field's type or one of its subclasses. Fields that have no setter and no getter are stripped from the result. In case of duplicate names of fields only the first occurrence is kept. -
extractGenericType
Extracts the class present as the given parameter in the generic definition of the type in the given field. For example, if field is of typeList<String>, the generic type at indexzerowill beString.class.- Parameters:
field- Field to check.genericIndex- Index of the type to return.- Returns:
- The type, if present or found.
-
newInstance
Creates an instance of a given type through a publicly accessible no-arg constructor.- Type Parameters:
T- Type of object to create.- Parameters:
type- Type to create.- Returns:
- An instance of a given type.
- Throws:
IllegalArgumentException- when the object cannot be created for any reason
-