vendredi 21 avril 2017

Match (formal and actual) type tokens programmtically

In Guava, there is a way to programmatically replace formal type parameters by actual ones: http://ift.tt/2plHmYp . To kind of reverse this, I want to find out how to programmatically match several given types:

TypeToken<?>
        t1 = new TypeToken<List<String>>() {},
        t2 = new TypeToken<List<?>>() {},
        t3 = new TypeToken<List<? extends Number>>() {},
        t4 = new TypeToken<L>() {},
        t5 = new TypeToken<Map<?, ?>>(){},
        t6 = new TypeToken<Map<?, String>>() {},
        t7 = new TypeToken<Integer>(){};

match(t1, t3); // returns null
match(t2, t1); // returns LinkedHashMap(WildcardType(?) => Class<String>)
match(t4, t1); // returns LinkedHashMap(TypeVariable("L") => ParameterizedType(List<String>));
match(t5, t6); // returns LinkedHashMap(
               //   WildcardType(?) => WildcardType(?),
               //   WildcardType(?) => Class<String>
               // )
match(t4, t7); // returns LinkedHashMap(TypeVariable("L") => Class<Integer>);

It should at least return a map of formal type parameters to actual types, if types match, else null. An extension could match subclasses, too. Is there a free implementation that solves this problem?





Aucun commentaire:

Enregistrer un commentaire