Interface TypeReifier

    • Method Detail

      • getExtractor

        Optional<TypeExtractor> getExtractor​(Class<?> c)
        Gets the type extractor which handles the given class, or null if none.
      • reify

        default Type reify​(Object o)
        Extracts the generic Type of the given Object.

        The ideal goal of the extraction is to reconstitute a fully concrete generic type, with all type variables fully resolved—e.g.: ArrayList<Integer> rather than a raw ArrayList class or even ArrayList<N extends Number>. Failing that, though, type variables which are still unknown after analysis will be replaced with wildcards—e.g., HashMap might become HashMap<String, ?> if a concrete type for the map values cannot be determined.

        For objects whose concrete type has no parameters, this method simply returns o.getClass(). For example:

              StringList implements List<String>
         
        will return StringList.class.

        The interesting case is for objects whose concrete class does have type parameters. E.g.:

              NumberList<N extends Number> implements List<N>
              ListMap<K, V, T> implements Map<K, V>, List<T>
         
        For such types, we try to fill the type parameters recursively, using TypeExtractor plugins that know how to glean types at runtime from specific sorts of objects.

        For example, IterableTypeExtractor knows how to guess a T for any Iterable<T> by examining the type of the elements in its iteration. (Of course, this may be inaccurate if the elements in the iteration are heterogeneously typed, but for many use cases this guess is better than nothing.)

        In this way, the behavior of the generic type extraction is fully extensible, since additional TypeExtractor plugins can always be introduced which extract types more intelligently in cases where more a priori knowledge about that type is available at runtime.