jeudi 14 mars 2019

Find out the actual value for a type-variable for a generic parent class

I wonder whether anyone could point out to a library that would resolve this for me...

Given a type declaration of a class A I would like to determine the actual types assigned to the type variables of a class or interface B that A either extends or implements.

For example ...


interface Collection<T> { /*...*/ }
interface List<T> extends Collection<T> { /* ... */ }
class ArrayList<T> implements List<T> { /* ... */ }

class Blah  {
  // ...
  public ArrayList<String> list;
  // ...
}

// ...

  Class[] result =  resolveSuperTypeTypeVariable(Blah.class.getField("list"),
                            Collection.class);

  // result == { String.class } since list would be Collection<String>.


This is perhaps the simplest scenario ... if the ArrayList type-variable is a wildcard the I would be interested in the most restricting upper bound... If it is a type-variable of Blah (thus Blah<T> and ArrayList<T>) then it would be the upper bound of T and so forth.

I've started looking into using the java.lang.reflect.Type hierarchy but its seem that it would be lots of work and perhaps a solution is already out.





Aucun commentaire:

Enregistrer un commentaire