コード例 #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());
  }
コード例 #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());
 }
コード例 #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());
 }
コード例 #4
0
 public void testInterfaceTypeParameterResolution() throws Exception {
   assertEquals(
       String.class,
       TypeToken.of(new TypeToken<ArrayList<String>>() {}.getType())
           .resolveType(List.class.getTypeParameters()[0])
           .getType());
 }
コード例 #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];
 }
コード例 #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) {
   }
 }
コード例 #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());
  }
コード例 #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());
 }
コード例 #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());
 }
コード例 #10
0
 public void testClassWrapper() {
   TypeToken<String> typeExpression = TypeToken.of(String.class);
   assertEquals(String.class, typeExpression.getType());
   assertEquals(String.class, typeExpression.getRawType());
 }
コード例 #11
0
 public void testInnerClassWithParameterizedOwner() throws Exception {
   Type fieldType = ParameterizedOuter.class.getField("field").getGenericType();
   assertEquals(
       fieldType, TypeToken.of(ParameterizedOuter.class).resolveType(fieldType).getType());
 }