Types { public static ParameterizedType newParameterizedType(Type rawType, Type... typeArguments) { return new ParameterizedTypeImpl(null, rawType, typeArguments); } private  Types(); static ParameterizedType newParameterizedType(Type rawType, Type... typeArguments); static GenericArrayType arrayOf(Type componentType); static WildcardType subtypeOf(Type bound); static WildcardType supertypeOf(Type bound); static Class<?> getRawType(Type type); static Type collectionElementType(Type context, Class<?> contextRawType);  }
@Test public void newParameterizedType() throws Exception { Type type = Types.newParameterizedType(List.class, A.class); assertThat(getFirstTypeArgument(type)).isEqualTo(A.class); type = Types.newParameterizedType(A.class, B.class); assertThat(getFirstTypeArgument(type)).isEqualTo(B.class); final class D { } try { Types.newParameterizedType(D.class, A.class); fail(); } catch (IllegalArgumentException expected) { } type = Types.newParameterizedType(A.class, D.class); assertThat(getFirstTypeArgument(type)).isEqualTo(D.class); }
@Test public void getFirstTypeArgument() throws Exception { assertThat(getFirstTypeArgument(A.class)).isNull(); Type type = Types.newParameterizedType(A.class, B.class, C.class); assertThat(getFirstTypeArgument(type)).isEqualTo(B.class); }
@Test public void newParameterizedTypeObjectMethods() throws Exception { Type mapOfStringIntegerType = TypesTest.class.getDeclaredField( "mapOfStringInteger").getGenericType(); ParameterizedType newMapType = Types.newParameterizedType(Map.class, String.class, Integer.class); assertThat(newMapType).isEqualTo(mapOfStringIntegerType); assertThat(newMapType.hashCode()).isEqualTo(mapOfStringIntegerType.hashCode()); assertThat(newMapType.toString()).isEqualTo(mapOfStringIntegerType.toString()); Type arrayListOfMapOfStringIntegerType = TypesTest.class.getDeclaredField( "arrayListOfMapOfStringInteger").getGenericType(); ParameterizedType newListType = Types.newParameterizedType(ArrayList.class, newMapType); assertThat(newListType).isEqualTo(arrayListOfMapOfStringIntegerType); assertThat(newListType.hashCode()).isEqualTo(arrayListOfMapOfStringIntegerType.hashCode()); assertThat(newListType.toString()).isEqualTo(arrayListOfMapOfStringIntegerType.toString()); }