use of java.lang.reflect.WildcardType in project guava by hceylan.
the class TypeToken method isAssignableBySubtypeBound.
private static boolean isAssignableBySubtypeBound(Type from, WildcardType to) {
Type toSubtypeBound = subtypeBound(to);
if (toSubtypeBound == null) {
return true;
}
Type fromSubtypeBound = subtypeBound(from);
if (fromSubtypeBound == null) {
return false;
}
return isAssignable(toSubtypeBound, fromSubtypeBound);
}
use of java.lang.reflect.WildcardType in project guava by hceylan.
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 hceylan.
the class TypeToken method getGenericSuperclass.
/**
* Returns the generic superclass of this type or {@code null} if the type represents
* {@link Object} or an interface. This method is similar but different from {@link
* Class#getGenericSuperclass}. For example, {@code
* new TypeToken<StringArrayList>() {}.getGenericSuperclass()} will return {@code
* new TypeToken<ArrayList<String>>() {}}; while {@code
* StringArrayList.class.getGenericSuperclass()} will return {@code ArrayList<E>}, where {@code E}
* is the type variable declared by class {@code ArrayList}.
*
* <p>If this type is a type variable or wildcard, its first upper bound is examined and returned
* if the bound is a class or extends from a class. This means that the returned type could be a
* type variable too.
*/
@Nullable
final TypeToken<? super T> getGenericSuperclass() {
if (runtimeType instanceof TypeVariable) {
// First bound is always the super class, if one exists.
return boundAsSuperclass(((TypeVariable<?>) runtimeType).getBounds()[0]);
}
if (runtimeType instanceof WildcardType) {
// wildcard has one and only one upper bound.
return boundAsSuperclass(((WildcardType) runtimeType).getUpperBounds()[0]);
}
Type superclass = getRawType().getGenericSuperclass();
if (superclass == null) {
return null;
}
// super class of T
@SuppressWarnings("unchecked") TypeToken<? super T> superToken = (TypeToken<? super T>) resolveSupertype(superclass);
return superToken;
}
use of java.lang.reflect.WildcardType in project tomee by apache.
the class InjectionUtils method getActualType.
public static Class<?> getActualType(Type genericType, int pos) {
if (genericType == null) {
return null;
}
if (genericType == Object.class) {
return (Class<?>) genericType;
}
if (!ParameterizedType.class.isAssignableFrom(genericType.getClass())) {
if (genericType instanceof TypeVariable) {
genericType = getType(((TypeVariable<?>) genericType).getBounds(), pos);
} else if (genericType instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) genericType;
Type[] bounds = wildcardType.getLowerBounds();
if (bounds.length == 0) {
bounds = wildcardType.getUpperBounds();
}
genericType = getType(bounds, pos);
} else if (genericType instanceof GenericArrayType) {
genericType = ((GenericArrayType) genericType).getGenericComponentType();
}
final Class<?> cls;
if (!(genericType instanceof ParameterizedType)) {
cls = (Class<?>) genericType;
} else {
cls = (Class<?>) ((ParameterizedType) genericType).getRawType();
}
return cls.isArray() ? cls.getComponentType() : cls;
}
ParameterizedType paramType = (ParameterizedType) genericType;
Type t = getType(paramType.getActualTypeArguments(), pos);
return t instanceof Class ? (Class<?>) t : getActualType(t, 0);
}
use of java.lang.reflect.WildcardType in project tomee by apache.
the class GenericArgumentComparator method asClass.
private Class<?> asClass(final Type aType) {
if (aType instanceof Class) {
return (Class<?>) aType;
}
if (aType instanceof ParameterizedType) {
final ParameterizedType parameterizedType = (ParameterizedType) aType;
return asClass(parameterizedType.getRawType());
}
if (aType instanceof TypeVariable) {
final TypeVariable typeVariable = (TypeVariable) aType;
final Type[] bounds = typeVariable.getBounds();
if (bounds == null || bounds.length == 0) {
return Object.class;
} else {
return asClass(bounds[0]);
}
}
if (aType instanceof WildcardType) {
// todo
return Object.class;
}
return Object.class;
}
Aggregations