Java exposes generic type information e.g. for field types at runtime through interface java.lang.reflect.Type and its subinterfaces (ParameterizedType, WildcardType).
Does anybody know where I can find a component that can determine if objects of one Type can be casted to another Type, e.g. if a Set<Object> can be casted to Set or a Comparator<User> can be casted to Comparator<? extends Object>?
The solution should work for type information represented by java.lang.reflect.Type, not only Classes, and it should take generics into consideration, i.e. a List<Integer> should not be castable to a List<Object>.
Regards,
Jochen
Guava’s
TypeTokenis designed for exactly this problem, among others. It’s meant to be a replacement forClassthat’s generics-aware. (Disclosure: I contribute to Guava.)