@Test public void nestedGenericType_actualTypeParametersAreCorrectlyCaptured() { TypeToken<List<List<String>>> typeToken = TypeToken.of(new Safe<List<List<String>>>() {}); assertThat(typeToken).isInstanceOf(TypeToken.ClassToken.class); TypeToken.ClassToken<List<List<String>>> typeClassToken = (TypeToken.ClassToken<List<List<String>>>) typeToken; TypeToken<List<String>> componentToken = typeClassToken.getTypeArguments().at(0); assertThat(componentToken).isInstanceOf(TypeToken.ClassToken.class); TypeToken.ClassToken<List<String>> componentClassToken = (TypeToken.ClassToken<List<String>>) componentToken; assertThat(componentClassToken.getRawType()).isEqualTo(List.class); TypeToken<String> innerComponentToken = componentClassToken.getTypeArguments().at(0); assertThat(innerComponentToken).isInstanceOf(TypeToken.ClassToken.class); TypeToken.ClassToken<String> innerComponentClassToken = (TypeToken.ClassToken<String>) innerComponentToken; assertThat(innerComponentClassToken.getRawType()).isEqualTo(String.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();  }