Utility for extracting type parameters
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Fri Sep 30 02:39:59 PDT 2011
On 30/09/11 09:22, Florian Weimer wrote:
> I've got a type T (in the most general sense; that is, it can be a
> generic type with or with type parameters applied) and a generic type X
> such that T is assignable to X. If no raw types are involved and I'm
> not mistaken, the type parameters of X which make this assignment valid
> are uniquely determined. Is there a helper function to compute them?
>
> At first glance, this does not seem to be too difficult to compute, but
> generics are tricky, so I wonder if there's existing code.
>
Is X a completely unknown type or do you know its class name?
i.e.
X = List<...>
In that case you can use the code in Types.asSuper [1]; see line 1397:
*) call Types.asSuper passing in the type of T (as first parameter) and
the symbol of X (as second parameter) - assuming that T is a subtype of
X, this method will return the supertype of T whose class name is the
same as X - call it X'.
*) now that you have X', you can easily determine its type-parameters,
which will be the ones that make the assignment legal (when applied to X).
Hope this helps.
[1] -
http://hg.openjdk.java.net/jdk8/tl/langtools/file/4e754e4b0a52/src/share/classes/com/sun/tools/javac/code/Types.java
Maurizio
More information about the compiler-dev
mailing list