A Generic Type Resolver is a piece of software which can resolve Java’s Generic Types.
Contrary to popular belief, Java doesn’t completely remove all generic types during runtime.
In the following example:
class GenericExample<T> {
}
class Example extends GenericExample<String> {
}
It is possible to resolve the generic type T of the class Example with the following code:
During runtime, genericType will return String.class
This code is however capable of throwing a ClassCastException and can only resolve the type of the immediate superclass.
To resolve the generic type of a class higher in the hierarchy and to protect ourselves from ClassCastExceptions, we need to use some generic type resolver.
A good generic type resolver is the typetools and one of it’s greatest features are the abilities to resolve lambda generics and generic types of classes higher in the hierarchy.
It's available on the Maven Central repository as the following dependency:
A Generic Type Resolver is a piece of software which can resolve Java’s Generic Types. Contrary to popular belief, Java doesn’t completely remove all generic types during runtime. In the following example:
It is possible to resolve the generic type
T
of the classExample
with the following code:During runtime,
genericType
will returnString.class
This code is however capable of throwing aClassCastException
and can only resolve the type of the immediate superclass.To resolve the generic type of a class higher in the hierarchy and to protect ourselves from
ClassCastException
s, we need to use some generic type resolver.A good generic type resolver is the typetools and one of it’s greatest features are the abilities to resolve lambda generics and generic types of classes higher in the hierarchy. It's available on the Maven Central repository as the following dependency: