-
- All Known Implementing Classes:
DefaultTypeReifier
public interface TypeReifier
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description Optional<TypeExtractor>getExtractor(Class<?> c)Gets the type extractor which handles the given class, or null if none.default Typereify(Object o)
-
-
-
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 genericTypeof the givenObject.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 rawArrayListclass or evenArrayList<N extends Number>. Failing that, though, type variables which are still unknown after analysis will be replaced with wildcards—e.g.,HashMapmight becomeHashMap<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 returnStringList.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, usingTypeExtractorplugins that know how to glean types at runtime from specific sorts of objects.For example,
IterableTypeExtractorknows how to guess aTfor anyIterable<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
TypeExtractorplugins can always be introduced which extract types more intelligently in cases where more a priori knowledge about that type is available at runtime.
-
-