GenericsUtil { public static <V> Class<V> fromGenericType(Class<?> subclass, Class<?> superclass) { return fromGenericType(subclass, null, superclass, 0); } private  GenericsUtil(); static Class<V> fromGenericType(Class<?> subclass, Class<?> superclass); static Class<V> fromGenericType(Type subtype, Class<?> superclass, int parameterIndex); static Class<V> fromGenericType(Class<?> subclass, ParameterizedType subtype, Class<?> superclass, int parameterIndex); static ParameterizedType getParameterizedType(Type type); static Class<T> getRawType(Type type);  }
@Test public void interfaceWithParameters() { assertThat(GenericsUtil.fromGenericType(MultiparameterSwitchingSubclass.class, ParameterizedInterface.class, 0)) .isEqualTo(Integer.class); }
@Test public void genericSubclass() { assertThat(GenericsUtil.fromGenericType(new TypeLiteral<GenericNumberSubclass<Integer>>() {}.getType(), GenericSuperclass.class, 0)).isEqualTo(Integer.class); }
@Test public void genericInterface() { assertThat(GenericsUtil.fromGenericType(new TypeLiteral<ParameterizedInterface<Integer>>() {}.getType(), ParameterizedInterface.class, 0)).isEqualTo(Integer.class); }
@Test public void stringSubclass() { assertThat(GenericsUtil.fromGenericType(StringSubclass.class, GenericSuperclass.class, 0)).isEqualTo(String.class); }
@Test public void integerSubclass() { assertThat(GenericsUtil.fromGenericType(IntegerSubclass.class, GenericSuperclass.class, 0)).isEqualTo(Integer.class); }
@Test(expected = IllegalStateException.class) public void rawSuperclassFails() { GenericsUtil.fromGenericType(RawSubclass.class, GenericSuperclass.class, 0); }
@Test(expected = IllegalStateException.class) public void unresolvableSuperclassFails() { GenericsUtil.fromGenericType(OuterClass.UnspecificSubclass.class, GenericSuperclass.class, 0); }
@Test(expected = IllegalStateException.class) public void superclassIsNotAssignableFromSubclass() { try { GenericsUtil.fromGenericType(Long.class, Integer.class, 0); } catch (IllegalStateException e) { assertThat(e.getMessage()).contains("is no subclass of"); throw e; } }
@Test public void multipleGenericParameters() { assertThat(GenericsUtil.fromGenericType(MultiparameterSubclass.class, MultiparameterSuperclass.class, 0)) .isEqualTo(String.class); assertThat(GenericsUtil.fromGenericType(MultiparameterSubclass.class, MultiparameterSuperclass.class, 1)) .isEqualTo(Integer.class); assertThat(GenericsUtil.fromGenericType(MultiparameterSwitchingSubclass.class, MultiparameterSuperclass.class, 0)) .isEqualTo(Integer.class); assertThat(GenericsUtil.fromGenericType(MultiparameterSwitchingSubclass.class, MultiparameterSuperclass.class, 1)) .isEqualTo(String.class); }