@Test public void genericType_actualTypeParametersAreCorrectlyCaptured() { TypeToken<Map<String, Foo>> mapTypeToken = TypeToken.of(new Safe<Map<String, Foo>>() {}); assertThat(mapTypeToken).isInstanceOf(TypeToken.ClassToken.class); TypeToken.ClassToken<Map<String, Foo>> mapClassToken = (TypeToken.ClassToken<Map<String, Foo>>) mapTypeToken; TypeTokenContainer typeTokenContainer = mapClassToken.getTypeArguments(); TypeToken<String> firstArgumentToken = typeTokenContainer.at(0); TypeToken<Foo> secondArgumentTypeToken = typeTokenContainer.at(1); assertThat(firstArgumentToken).isInstanceOf(TypeToken.ClassToken.class); assertThat(secondArgumentTypeToken).isInstanceOf(TypeToken.ClassToken.class); TypeToken.ClassToken<String> firstArgumentClassToken = (TypeToken.ClassToken<String>) firstArgumentToken; TypeToken.ClassToken<Foo> secondArgumentClassToken = (TypeToken.ClassToken<Foo>) secondArgumentTypeToken; assertThat(firstArgumentClassToken.getRawType()).isEqualTo(String.class); assertThat(secondArgumentClassToken.getRawType()).isEqualTo(Foo.class); }
@NonNull public static <T> TypeToken<T> of(@NonNull Safe<T> token) { Type superclass = token.getClass().getGenericSuperclass(); if (superclass instanceof Class) { throw new IllegalArgumentException("Missing type parameters"); } @SuppressWarnings("ConstantConditions") Type type = ((ParameterizedType) superclass).getActualTypeArguments()[0]; return of(type); }
TypeToken { @NonNull public static <T> TypeToken<T> of(@NonNull Safe<T> token) { Type superclass = token.getClass().getGenericSuperclass(); if (superclass instanceof Class) { throw new IllegalArgumentException("Missing type parameters"); } @SuppressWarnings("ConstantConditions") Type type = ((ParameterizedType) superclass).getActualTypeArguments()[0]; return of(type); } }
TypeToken { @NonNull public static <T> TypeToken<T> of(@NonNull Safe<T> token) { Type superclass = token.getClass().getGenericSuperclass(); if (superclass instanceof Class) { throw new IllegalArgumentException("Missing type parameters"); } @SuppressWarnings("ConstantConditions") Type type = ((ParameterizedType) superclass).getActualTypeArguments()[0]; return of(type); } private  TypeToken(); }
TypeToken { @NonNull public static <T> TypeToken<T> of(@NonNull Safe<T> token) { Type superclass = token.getClass().getGenericSuperclass(); if (superclass instanceof Class) { throw new IllegalArgumentException("Missing type parameters"); } @SuppressWarnings("ConstantConditions") Type type = ((ParameterizedType) superclass).getActualTypeArguments()[0]; return of(type); } private  TypeToken(); @NonNull static TypeToken<T> of(@NonNull Safe<T> token); @NonNull static TypeToken<T> of(@NonNull Class<T> typeToken); @NonNull @Override String toString(); }
TypeToken { @NonNull public static <T> TypeToken<T> of(@NonNull Safe<T> token) { Type superclass = token.getClass().getGenericSuperclass(); if (superclass instanceof Class) { throw new IllegalArgumentException("Missing type parameters"); } @SuppressWarnings("ConstantConditions") Type type = ((ParameterizedType) superclass).getActualTypeArguments()[0]; return of(type); } private  TypeToken(); @NonNull static TypeToken<T> of(@NonNull Safe<T> token); @NonNull static TypeToken<T> of(@NonNull Class<T> typeToken); @NonNull @Override String toString();  }