vendredi 22 février 2019

How to get the REAL type parameter of a subclass of List?

When I read the JavaFX source code, in com.sun.javafx.fxml.BeanAdapter, the static method getGenericListItemType could be used to determine the type of a list item.

However, with simple testcases, I found that this implementation may be flawed:

abstract class A<T> implements List<String> {}

abstract class B extends A<Integer> {}

abstract class C<S, T> implements List<T> {}

abstract class D extends C<String, Integer> {}

// in a function scope
BeanAdapter.getGenericListItemType(B.class) // expects String, gets Integer
BeanAdapter.getGenericListItemType(A.class) // works correctly

BeanAdapter.getGenericListItemType(D.class) // expects Integer, gets String

It turns out that this flawed implementation can only handle ideal conditions with the pattern of List<T> ← A<T> ← B<T> ← ... (parameterized) ... ← Y ← Z

However, when I tried to implement the function, I find it rather complicated. I wonder if it's possible to get the real type parameter. And if possible, could you provide some hints or a snippet?





Aucun commentaire:

Enregistrer un commentaire