use of java.lang.reflect.WildcardType in project guava by google.
the class TypeTokenTest method testAssignableWildcardBoundedByIntArrayToArrayClass.
public void testAssignableWildcardBoundedByIntArrayToArrayClass() {
Type wildcardType = Types.subtypeOf(int[].class);
assertTrue(TypeToken.of(int[].class).isSupertypeOf(wildcardType));
assertTrue(TypeToken.of(Object.class).isSupertypeOf(wildcardType));
assertFalse(TypeToken.of(wildcardType).isSupertypeOf(wildcardType));
assertFalse(TypeToken.of(Object[].class).isSupertypeOf(wildcardType));
}
use of java.lang.reflect.WildcardType in project guava by google.
the class TypeTokenTest method testAssignableWildcardBoundedByArrayToArrayClass.
public void testAssignableWildcardBoundedByArrayToArrayClass() {
Type wildcardType = Types.subtypeOf(Object[].class);
assertTrue(TypeToken.of(Object[].class).isSupertypeOf(wildcardType));
assertTrue(TypeToken.of(Object.class).isSupertypeOf(wildcardType));
assertFalse(TypeToken.of(wildcardType).isSupertypeOf(wildcardType));
assertFalse(TypeToken.of(int[].class).isSupertypeOf(wildcardType));
}
use of java.lang.reflect.WildcardType in project guava by google.
the class TypeToken method getGenericInterfaces.
/**
* Returns the generic interfaces that this type directly {@code implements}. This method is
* similar but different from {@link Class#getGenericInterfaces()}. For example, {@code new
* TypeToken<List<String>>() {}.getGenericInterfaces()} will return a list that contains {@code
* new TypeToken<Iterable<String>>() {}}; while {@code List.class.getGenericInterfaces()} will
* return an array that contains {@code Iterable<T>}, where the {@code T} is the type variable
* declared by interface {@code Iterable}.
*
* <p>If this type is a type variable or wildcard, its upper bounds are examined and those that
* are either an interface or upper-bounded only by interfaces are returned. This means that the
* returned types could include type variables too.
*/
final ImmutableList<TypeToken<? super T>> getGenericInterfaces() {
if (runtimeType instanceof TypeVariable) {
return boundsAsInterfaces(((TypeVariable<?>) runtimeType).getBounds());
}
if (runtimeType instanceof WildcardType) {
return boundsAsInterfaces(((WildcardType) runtimeType).getUpperBounds());
}
ImmutableList.Builder<TypeToken<? super T>> builder = ImmutableList.builder();
for (Type interfaceType : getRawType().getGenericInterfaces()) {
// interface of T
@SuppressWarnings("unchecked") TypeToken<? super T> resolvedInterface = (TypeToken<? super T>) resolveSupertype(interfaceType);
builder.add(resolvedInterface);
}
return builder.build();
}
use of java.lang.reflect.WildcardType in project guava by google.
the class TypeToken method canonicalizeWildcardType.
// WARNING: the returned type may have empty upper bounds, which may violate common expectations
// by user code or even some of our own code. It's fine for the purpose of checking subtypes.
// Just don't ever let the user access it.
private static WildcardType canonicalizeWildcardType(TypeVariable<?> declaration, WildcardType type) {
Type[] declared = declaration.getBounds();
List<Type> upperBounds = new ArrayList<>();
for (Type bound : type.getUpperBounds()) {
if (!any(declared).isSubtypeOf(bound)) {
upperBounds.add(canonicalizeWildcardsInType(bound));
}
}
return new Types.WildcardTypeImpl(type.getLowerBounds(), upperBounds.toArray(new Type[0]));
}
use of java.lang.reflect.WildcardType in project dbeaver by serge-rider.
the class BeanUtilsTest method testGetCollectionType2.
@Test
public void testGetCollectionType2() {
WildcardType wildcardType = new WildcardType() {
@Override
public Type[] getUpperBounds() {
return new Type[] { String.class, Integer.class };
}
@Override
public Type[] getLowerBounds() {
return new Type[0];
}
};
ParameterizedType parameterizedType = new ParameterizedType() {
@Override
public Type[] getActualTypeArguments() {
return new Type[] { wildcardType };
}
@Override
public Type getRawType() {
return null;
}
@Override
public Type getOwnerType() {
return null;
}
};
Assert.assertEquals(String.class, BeanUtils.getCollectionType(parameterizedType));
}
Aggregations