Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: getting inner type in nested parameterized types (reflection)

Most of the documentation regarding type erasure handling in Java assumes that the use case is handling a type like SomeType<ParamType>. I am trying to process method parameter for the following method:

public void setOtherReferenceRanges(List<ReferenceRange<T>> referenceRanges)

When the container class is instantiated with a type DvQuantity, this signature should become public void setOtherReferenceRanges(List<ReferenceRange<DvQuanitity>> referenceRanges) in runtime.

Using reflection one can see that the List has an actualTypeArgument which is ReferenceRange<T>. Since reflection uses class information, I would not expect it to give me ReferenceRange<DvQuantity>. However, when I created the class containing this method, I passed the DvQuantity type as T. So the type filling in T should be available to Java runtime, but I could not find a way of getting it. I end up with a TypeVariableImpl object accessed via reflection, which does not seem to contain any useful data.

Can you think of any ways to discover this information in runtime?

like image 558
mahonya Avatar asked Nov 15 '22 12:11

mahonya


1 Answers

When you say

when I created the class containing this method

I guess you mean when you create an object of that type, for example:

foo = new ContainerClass<DvQuantity>();

In that case, because of erasure, there is no way to recover the type DvQuantity.

However, if you create a class passing a type parameter to the superclass, like this

class DvQuantityContainerClass extends ContainerClass<DvQuantity> {...}
...
foo = new DvQuantityContainerClass();

Or, shorter, an inline anonymous subclass (which looks almost like the first example but with a subtle but important difference):

foo = new ContainerClass<DvQuantity>(){};

Then you can recover the type parameter, because you recover the type parameter used to extend a superclass at runtime. Unfortunately, Java itself doesn't provide an easy way to now get the type of the DvQuantityContainerClass.setOtherReferenceRanges method with the T filled in. For that, I've written gentyref, to do advanced reflection on generic types:

Method m = DvQuantityContainerClass.class.getMethod("setOtherReferenceRanges", List.class);
// this will return List<ReferenceRange<DvQuanity>>, like you are lookingn for
return GenericTypeReflector.getExactParameterTypes(m, DvQuantityContainerClass.class)
like image 179
Wouter Coekaerts Avatar answered Dec 15 '22 07:12

Wouter Coekaerts