/** Returns the array type of {@code componentType}. */ static Type newArrayType(Type componentType) { if (componentType instanceof WildcardType) { WildcardType wildcard = (WildcardType) componentType; Type[] lowerBounds = wildcard.getLowerBounds(); checkArgument(lowerBounds.length <= 1, "Wildcard cannot have more than one lower bounds."); if (lowerBounds.length == 1) { return supertypeOf(newArrayType(lowerBounds[0])); } else { Type[] upperBounds = wildcard.getUpperBounds(); checkArgument(upperBounds.length == 1, "Wildcard should have only one upper bound."); return subtypeOf(newArrayType(upperBounds[0])); } } return JavaVersion.CURRENT.newArrayType(componentType); }
public void testNewParameterizedType_ownerMissing() { assertEquals( Types.newParameterizedType(Entry.class, String.class, Integer.class), Types.newParameterizedTypeWithOwner(null, Entry.class, String.class, Integer.class)); }
@Override public String toString() { return Types.toString(runtimeType); }
/** * Returns {@code ? extends X} if any of {@code bounds} is a subtype of {@code X[]}; or null * otherwise. */ private static @Nullable Type subtypeOfComponentType(Type[] bounds) { for (Type bound : bounds) { Type componentType = getComponentType(bound); if (componentType != null) { // Only the first bound can be a class or array. // Bounds after the first can only be interfaces. if (componentType instanceof Class) { Class<?> componentClass = (Class<?>) componentType; if (componentClass.isPrimitive()) { return componentClass; } } return subtypeOf(componentType); } } return null; }
public void testNewArrayType_lowerBoundedWildcard() { Type wildcard = Types.supertypeOf(Number.class); assertEquals(Types.supertypeOf(Number[].class), Types.newArrayType(wildcard)); }
return Types.newArrayType(capture(arrayType.getGenericComponentType())); return Types.newParameterizedTypeWithOwner( captureNullable(parameterizedType.getOwnerType()), (Class<?>) parameterizedType.getRawType(), String name = "capture#" + id.incrementAndGet() + "-of ? extends " + Joiner.on('&').join(upperBounds); return Types.newTypeVariable( WildcardCapturer.class, name, wildcardType.getUpperBounds()); } else {
public void testNewWildcardType_serializable() { SerializableTester.reserializeAndAssert(Types.supertypeOf(String.class)); SerializableTester.reserializeAndAssert(Types.subtypeOf(String.class)); SerializableTester.reserializeAndAssert(Types.subtypeOf(Object.class)); }
private static void doTestNewParameterizedType_staticLocalClass() { class LocalClass<T> {} Type jvmType = new LocalClass<String>() {}.getClass().getGenericSuperclass(); Type ourType = Types.newParameterizedType(LocalClass.class, String.class); assertEquals(jvmType, ourType); }
private static ParameterizedType canonicalizeWildcardsInParameterizedType( ParameterizedType type) { Class<?> rawType = (Class<?>) type.getRawType(); TypeVariable<?>[] typeVars = rawType.getTypeParameters(); Type[] typeArgs = type.getActualTypeArguments(); for (int i = 0; i < typeArgs.length; i++) { typeArgs[i] = canonicalizeTypeArg(typeVars[i], typeArgs[i]); } return Types.newParameterizedTypeWithOwner(type.getOwnerType(), rawType, typeArgs); }
if (cls.isArray()) { Type arrayOfGenericType = Types.newArrayType( TypeToken<? extends T> type = (TypeToken<? extends T>) of(Types.newParameterizedTypeWithOwner(ownerType, cls, typeParams)); return type; } else {
public void testNewArrayType_upperBoundedWildcard() { Type wildcard = Types.subtypeOf(Number.class); assertEquals(Types.subtypeOf(Number[].class), Types.newArrayType(wildcard)); }
private Type resolveGenericArrayType(GenericArrayType type) { Type componentType = type.getGenericComponentType(); Type resolvedComponentType = resolveType(componentType); return Types.newArrayType(resolvedComponentType); }
@Override void visitGenericArrayType(GenericArrayType fromArrayType) { if (to instanceof WildcardType) { return; // Okay to say A[] is <?> } Type componentType = Types.getComponentType(to); checkArgument(componentType != null, "%s is not an array type.", to); populateTypeMappings(mappings, fromArrayType.getGenericComponentType(), componentType); }
public void testNewWildcardType_primitiveTypeBound() { try { Types.subtypeOf(int.class); fail(); } catch (IllegalArgumentException expected) { } }
public void testNewArrayType() { Type jvmType1 = new TypeCapture<List<String>[]>() {}.capture(); GenericArrayType ourType1 = (GenericArrayType) Types.newArrayType(Types.newParameterizedType(List.class, String.class)); Type jvmType2 = new TypeCapture<List[]>() {}.capture(); Type ourType2 = Types.newArrayType(List.class); new EqualsTester() .addEqualityGroup(jvmType1, ourType1) .addEqualityGroup(jvmType2, ourType2) .testEquals(); assertEquals(new TypeCapture<List<String>>() {}.capture(), ourType1.getGenericComponentType()); assertEquals(jvmType1.toString(), ourType1.toString()); assertEquals(jvmType2.toString(), ourType2.toString()); }
TypeVariable<?> captureAsTypeVariable(Type[] upperBounds) { String name = "capture#" + id.incrementAndGet() + "-of ? extends " + Joiner.on('&').join(upperBounds); return Types.newArtificialTypeVariable(WildcardCapturer.class, name, upperBounds); }
public void testGetGenericInterfaces_wildcard_lowerBounded() { assertThat(TypeToken.of(Types.supertypeOf(String.class)).getGenericInterfaces()).isEmpty(); assertThat(TypeToken.of(Types.supertypeOf(String[].class)).getGenericInterfaces()).isEmpty(); }
@Override Type newArrayType(Type componentType) { if (componentType instanceof Class) { return getArrayClass((Class<?>) componentType); } else { return new GenericArrayTypeImpl(componentType); } }
/** * Resolves {@code var} using the encapsulated type mapping. If it maps to yet another * non-reified type, {@code guardedResolver} is used to do further resolution, which doesn't try * to resolve any type variable on generic declarations that are already being resolved. */ Type resolveTypeVariable(TypeVariable<?> var, TypeResolver guardedResolver) { checkNotNull(guardedResolver); Type type = typeTable.get(var); if (type == null) { Type[] bounds = var.getBounds(); if (bounds.length == 0) { return var; } return Types.newTypeVariable( var.getGenericDeclaration(), var.getName(), guardedResolver.resolveTypes(bounds)); } return guardedResolver.resolveType(type); // in case the type is yet another type variable. }
return Types.newArrayType(capture(arrayType.getGenericComponentType())); return Types.newParameterizedTypeWithOwner( captureNullable(parameterizedType.getOwnerType()), (Class<?>) parameterizedType.getRawType(), String name = "capture#" + id.incrementAndGet() + "-of ? extends " + Joiner.on('&').join(upperBounds); return Types.newTypeVariable( WildcardCapturer.class, name, wildcardType.getUpperBounds()); } else {