I have the following problem:
Given a Guice type literal
TypeLiteral<T> template
and a classClass c
implementing or extendingT
, construct a typeType t
which is equivalent toc
with all type variables instantiated so as to be compatible withtemplate
.
If c
has no type variables, it's easy; c
is the type in question. However, if c
has type variables, then I need to do the following:
- Find the type in
c
's inheritance and implementation hierarchy corresponding to the raw type ofT
- Walk through the type parameter structure, finding any type variable uses and their corresponding types in
template
- Use the Guice
Types
helper functions to create a type fromc
instantiated with the types found in (2).
Of course, there are error cases and it might not be complete. If it can't find matching uses of all type variables, it will fail. There might be other cases as well. However, if I have this:
class CS<I> implements S<Map<I,Float>> {
// some stuff
}
and a type literal TypeLiteral<S<Map<I,Float>>>
, I want to get a type which represents CS
fully instantiated to match the type literal.
It looks like reflection provides enough information to accomplish this, but the logic looks complex and error-prone. Is there an existing library which exposes this logic?