Exemplo n.º 1
0
  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());
  }
Exemplo n.º 2
0
 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());
 }
Exemplo n.º 3
0
 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());
 }
Exemplo n.º 4
0
 public void testInterfaceTypeParameterResolution() throws Exception {
   assertEquals(
       String.class,
       TypeToken.of(new TypeToken<ArrayList<String>>() {}.getType())
           .resolveType(List.class.getTypeParameters()[0])
           .getType());
 }
Exemplo n.º 5
0
 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];
 }
Exemplo n.º 6
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) {
   }
 }
Exemplo n.º 7
0
  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());
  }
Exemplo n.º 8
0
 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());
 }
Exemplo n.º 9
0
 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());
 }
Exemplo n.º 10
0
 public void testClassWrapper() {
   TypeToken<String> typeExpression = TypeToken.of(String.class);
   assertEquals(String.class, typeExpression.getType());
   assertEquals(String.class, typeExpression.getRawType());
 }
Exemplo n.º 11
0
 public void testInnerClassWithParameterizedOwner() throws Exception {
   Type fieldType = ParameterizedOuter.class.getField("field").getGenericType();
   assertEquals(
       fieldType, TypeToken.of(ParameterizedOuter.class).resolveType(fieldType).getType());
 }