TypeTokenUtils { public static List<TypeToken<?>> extractGenericTypeTokens(TypeToken<?> typeToken) { List<TypeToken<?>> typeTokens = new ArrayList<>(); try { Type[] types = ((ParameterizedType) typeToken.getType()).getActualTypeArguments(); for (Type type : types) { typeTokens.add(TypeToken.of(type)); } } catch (ClassCastException e) { } return typeTokens; }  static TypeToken<?> extractElementTypeToken(TypeToken<?> typeToken); static List<TypeToken<?>> extractGenericTypeTokens(TypeToken<?> typeToken);  }
@Test public void testGetGenericTypeTokens() { TypeToken source = new TypeToken<List<String>>() {}; List<TypeToken<?>> typeTokens = TypeTokenUtils.extractGenericTypeTokens(source); assertEquals(typeTokens.size(), 1); assertEquals(typeTokens.get(0), TypeToken.of(String.class)); }
@Test public void testGetMultipleGenericTypeTokens() { TypeToken source = new TypeToken<Map<String, Integer>>() {}; List<TypeToken<?>> typeTokens = TypeTokenUtils.extractGenericTypeTokens(source); assertEquals(typeTokens.size(), 2); assertEquals(typeTokens.get(0), TypeToken.of(String.class)); assertEquals(typeTokens.get(1), TypeToken.of(Integer.class)); }