Class ReflectTools

java.lang.Object
org.vaadin.miki.util.ReflectTools

public final class ReflectTools extends Object
A collection of static methods to access things through reflection.
Since:
2020-05-03
Author:
miki
  • Field Details

  • Method Details

    • getValueOfField

      public static <V> Optional<V> getValueOfField(Object instance, Class<? extends V> valueType, String fieldName)
      Attempts to get the value of a Field of 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 to trySetAccessible will 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's Class.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 matching public, non-static getters/setters in type, and returns those. Setters must start with set, getters with get (is and are supported 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.
      Parameters:
      type - Type to scan.
      ignoreSuperclasses - Whether to ignore superclasses (all the way until Object.
      Returns:
      A non-null Map that associates a Field with a corresponding getter method and a setter method (either can be null).
    • extractGenericType

      public static Optional<Class<?>> extractGenericType(Field field, int genericIndex)
      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 type List<String>, the generic type at index zero will be String.class.
      Parameters:
      field - Field to check.
      genericIndex - Index of the type to return.
      Returns:
      The type, if present or found.
    • newInstance

      public static <T> T newInstance(Class<T> type)
      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