public void testGenericInterface() { // test the 1st generic interface on the class Type fType = Supplier.class.getTypeParameters()[0]; assertEquals( Integer.class, TypeToken.of(IntegerStringFunction.class).resolveType(fType).getRawType()); // test the 2nd generic interface on the class Type predicateParameterType = Predicate.class.getTypeParameters()[0]; assertEquals( new TypeToken<List<String>>() {}.getType(), TypeToken.of(IntegerStringFunction.class).resolveType(predicateParameterType).getType()); }
public void testFalseRecursiveType_intermediaryTypeMappingDoesNotConfuseMe() { Type returnType = genericReturnType(SubtypeOfWithFalseRecursiveType.class, "revertKeyAndValueTypes"); TypeToken<?> keyType = TypeToken.of(returnType).resolveType(WithFalseRecursiveType.class.getTypeParameters()[0]); assertEquals("java.util.List<K1>", keyType.getType().toString()); }
public void testFalseRecursiveType_mappingOnTheSameDeclarationNotUsed() { Type returnType = genericReturnType(WithFalseRecursiveType.class, "keyShouldNotResolveToStringList"); TypeToken<?> keyType = TypeToken.of(returnType).resolveType(WithFalseRecursiveType.class.getTypeParameters()[0]); assertEquals("java.util.List<V>", keyType.getType().toString()); }
public void testInterfaceTypeParameterResolution() throws Exception { assertEquals( String.class, TypeToken.of(new TypeToken<ArrayList<String>>() {}.getType()) .resolveType(List.class.getTypeParameters()[0]) .getType()); }
Type getTargetType(String methodName) throws Exception { ParameterizedType parameterType = (ParameterizedType) WithGenericBound.class.getDeclaredMethod(methodName, List.class) .getGenericParameterTypes()[0]; parameterType = (ParameterizedType) TypeToken.of(this.getClass()).resolveType(parameterType).getType(); return parameterType.getActualTypeArguments()[0]; }
public void testResolveType() { assertEquals(String.class, TypeToken.of(this.getClass()).resolveType(String.class).getType()); assertEquals( String.class, TypeToken.of(StringIterable.class) .resolveType(Iterable.class.getTypeParameters()[0]) .getType()); assertEquals( String.class, TypeToken.of(StringIterable.class) .resolveType(Iterable.class.getTypeParameters()[0]) .getType()); try { TypeToken.of(this.getClass()).resolveType(null); fail(); } catch (NullPointerException expected) { } }
public void testConextIsParameterizedType() throws Exception { class Context { @SuppressWarnings("unused") // used by reflection Map<String, Integer> returningMap() { throw new AssertionError(); } } Type context = Context.class.getDeclaredMethod("returningMap").getGenericReturnType(); Type keyType = Map.class.getTypeParameters()[0]; Type valueType = Map.class.getTypeParameters()[1]; // context is parameterized type assertEquals(String.class, TypeToken.of(context).resolveType(keyType).getType()); assertEquals(Integer.class, TypeToken.of(context).resolveType(valueType).getType()); // context is type variable assertEquals(keyType, TypeToken.of(keyType).resolveType(keyType).getType()); assertEquals(valueType, TypeToken.of(valueType).resolveType(valueType).getType()); }
public void testFalseRecursiveType_referenceOfSubtypeDoesNotConfuseMe() { Type returnType = genericReturnType(WithFalseRecursiveType.class, "evenSubtypeWorks"); TypeToken<?> keyType = TypeToken.of(returnType).resolveType(WithFalseRecursiveType.class.getTypeParameters()[0]); assertEquals("java.util.List<java.util.List<V>>", keyType.getType().toString()); }
public void testFalseRecursiveType_notRealRecursiveMapping() { Type returnType = genericReturnType(WithFalseRecursiveType.class, "shouldNotCauseInfiniteLoop"); TypeToken<?> keyType = TypeToken.of(returnType).resolveType(WithFalseRecursiveType.class.getTypeParameters()[0]); assertEquals("java.util.List<K>", keyType.getType().toString()); }
public void testClassWrapper() { TypeToken<String> typeExpression = TypeToken.of(String.class); assertEquals(String.class, typeExpression.getType()); assertEquals(String.class, typeExpression.getRawType()); }
public void testInnerClassWithParameterizedOwner() throws Exception { Type fieldType = ParameterizedOuter.class.getField("field").getGenericType(); assertEquals( fieldType, TypeToken.of(ParameterizedOuter.class).resolveType(fieldType).getType()); }