本文整理匯總了Java中java.lang.reflect.WildcardType類的典型用法代碼示例。如果您正苦於以下問題:Java WildcardType類的具體用法?Java WildcardType怎麽用?Java WildcardType使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
WildcardType類屬於java.lang.reflect包,在下文中一共展示了WildcardType類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: canonicalize
import java.lang.reflect.WildcardType; //導入依賴的package包/類
/**
* Returns a type that is functionally equal but not necessarily equal according to {@link
* Object#equals(Object) Object.equals()}.
*/
static Type canonicalize(Type type) {
if (type instanceof Class) {
Class<?> c = (Class<?>) type;
return c.isArray() ? new GenericArrayTypeImpl(canonicalize(c.getComponentType())) : c;
} else if (type instanceof ParameterizedType) {
if (type instanceof ParameterizedTypeImpl) return type;
ParameterizedType p = (ParameterizedType) type;
return new ParameterizedTypeImpl(p.getOwnerType(),
p.getRawType(), p.getActualTypeArguments());
} else if (type instanceof GenericArrayType) {
if (type instanceof GenericArrayTypeImpl) return type;
GenericArrayType g = (GenericArrayType) type;
return new GenericArrayTypeImpl(g.getGenericComponentType());
} else if (type instanceof WildcardType) {
if (type instanceof WildcardTypeImpl) return type;
WildcardType w = (WildcardType) type;
return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds());
} else {
return type; // This type is unsupported!
}
}
示例2: isAssignable
import java.lang.reflect.WildcardType; //導入依賴的package包/類
private static boolean isAssignable(ParameterizedType lhsType, ParameterizedType rhsType){
if (lhsType.equals(rhsType)) {
return true;
}
Type[] lhsTypeArguments = lhsType.getActualTypeArguments();
Type[] rhsTypeArguments = rhsType.getActualTypeArguments();
if (lhsTypeArguments.length != rhsTypeArguments.length) {
return false;
}
for (int size = lhsTypeArguments.length, i = 0; i < size; ++i) {
Type lhsArg = lhsTypeArguments[i];
Type rhsArg = rhsTypeArguments[i];
if (!lhsArg.equals(rhsArg) &&
!(lhsArg instanceof WildcardType && isAssignable((WildcardType) lhsArg, rhsArg))) {
return false;
}
}
return true;
}
示例3: hasUnresolvableType
import java.lang.reflect.WildcardType; //導入依賴的package包/類
static boolean hasUnresolvableType(Type type) {
if (type instanceof Class<?>) {
return false;
}
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
for (Type typeArgument : parameterizedType.getActualTypeArguments()) {
if (hasUnresolvableType(typeArgument)) {
return true;
}
}
return false;
}
if (type instanceof GenericArrayType) {
return hasUnresolvableType(((GenericArrayType) type).getGenericComponentType());
}
if (type instanceof TypeVariable) {
return true;
}
if (type instanceof WildcardType) {
return true;
}
String className = type == null ? "null" : type.getClass().getName();
throw new IllegalArgumentException("Expected a Class, ParameterizedType, or "
+ "GenericArrayType, but <" + type + "> is of type " + className);
}
示例4: testGetSubtype_recursiveTypeBoundInSubtypeTranslatedAsIs
import java.lang.reflect.WildcardType; //導入依賴的package包/類
public void testGetSubtype_recursiveTypeBoundInSubtypeTranslatedAsIs() {
class BaseWithTypeVar<T> {}
class Outer<O> {
class Sub<X> extends BaseWithTypeVar<List<X>> {}
class Sub2<Y extends Sub2<Y>> extends BaseWithTypeVar<List<Y>> {}
}
ParameterizedType subtype = (ParameterizedType) new TypeToken<BaseWithTypeVar<List<?>>>() {}
.getSubtype(Outer.Sub.class)
.getType();
assertEquals(Outer.Sub.class, subtype.getRawType());
assertThat(subtype.getActualTypeArguments()[0]).isInstanceOf(WildcardType.class);
ParameterizedType owner = (ParameterizedType) subtype.getOwnerType();
assertEquals(Outer.class, owner.getRawType());
// This returns a strange ? extends Sub2<Y> type, which isn't ideal.
TypeToken<?> unused = new TypeToken<BaseWithTypeVar<List<?>>>() {}.getSubtype(Outer.Sub2.class);
}
示例5: canonicalize
import java.lang.reflect.WildcardType; //導入依賴的package包/類
public static Type canonicalize(Type type) {
if (type instanceof Class) {
GenericArrayTypeImpl genericArrayTypeImpl;
Class<?> c = (Class) type;
if (c.isArray()) {
genericArrayTypeImpl = new GenericArrayTypeImpl(C$Gson$Types.canonicalize(c
.getComponentType()));
} else {
Object obj = c;
}
return genericArrayTypeImpl;
} else if (type instanceof ParameterizedType) {
ParameterizedType p = (ParameterizedType) type;
return new ParameterizedTypeImpl(p.getOwnerType(), p.getRawType(), p
.getActualTypeArguments());
} else if (type instanceof GenericArrayType) {
return new GenericArrayTypeImpl(((GenericArrayType) type).getGenericComponentType());
} else {
if (!(type instanceof WildcardType)) {
return type;
}
WildcardType w = (WildcardType) type;
return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds());
}
}
示例6: canonicalize
import java.lang.reflect.WildcardType; //導入依賴的package包/類
/**
* Returns a type that is functionally equal but not necessarily equal
* according to {@link Object#equals(Object) Object.equals()}. The returned
* type is {@link java.io.Serializable}.
*/
public static Type canonicalize(Type type) {
if (type instanceof Class) {
Class<?> c = (Class<?>) type;
return c.isArray() ? new GenericArrayTypeImpl(canonicalize(c.getComponentType())) : c;
} else if (type instanceof ParameterizedType) {
ParameterizedType p = (ParameterizedType) type;
return new ParameterizedTypeImpl(p.getOwnerType(),
p.getRawType(), p.getActualTypeArguments());
} else if (type instanceof GenericArrayType) {
GenericArrayType g = (GenericArrayType) type;
return new GenericArrayTypeImpl(g.getGenericComponentType());
} else if (type instanceof WildcardType) {
WildcardType w = (WildcardType) type;
return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds());
} else {
// type is either serializable as-is or unsupported
return type;
}
}
示例7: is
import java.lang.reflect.WildcardType; //導入依賴的package包/類
/**
* Return true if any of the following conditions is met:
*
* <ul>
* <li>'this' and {@code formalType} are equal
* <li>{@code formalType} is {@code <? extends Foo>} and 'this' is a subtype of {@code Foo}
* <li>{@code formalType} is {@code <? super Foo>} and 'this' is a supertype of {@code Foo}
* </ul>
*/
private boolean is(Type formalType) {
if (runtimeType.equals(formalType)) {
return true;
}
if (formalType instanceof WildcardType) {
// if "formalType" is <? extends Foo>, "this" can be:
// Foo, SubFoo, <? extends Foo>, <? extends SubFoo>, <T extends Foo> or
// <T extends SubFoo>.
// if "formalType" is <? super Foo>, "this" can be:
// Foo, SuperFoo, <? super Foo> or <? super SuperFoo>.
return every(((WildcardType) formalType).getUpperBounds()).isSupertypeOf(runtimeType)
&& every(((WildcardType) formalType).getLowerBounds()).isSubtypeOf(runtimeType);
}
return false;
}
示例8: hasUnresolvableType
import java.lang.reflect.WildcardType; //導入依賴的package包/類
static boolean hasUnresolvableType(Type type) {
if (type instanceof Class<?>) {
return false;
}
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
for (Type typeArgument : parameterizedType.getActualTypeArguments()) {
if (hasUnresolvableType(typeArgument)) {
return true;
}
}
return false;
}
if (type instanceof GenericArrayType) {
return hasUnresolvableType(((GenericArrayType) type).getGenericComponentType());
}
if (type instanceof TypeVariable) {
return true;
}
if (type instanceof WildcardType) {
return true;
}
String className = type == null ? "null" : type.getClass().getName();
throw new IllegalArgumentException("Expected a Class, ParameterizedType, or "
+ "GenericArrayType, but <" + type + "> is of type " + className);
}
示例9: getGenericInterfaces
import java.lang.reflect.WildcardType; //導入依賴的package包/類
/**
* 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()) {
@SuppressWarnings("unchecked") // interface of T
TypeToken<? super T> resolvedInterface =
(TypeToken<? super T>) resolveSupertype(interfaceType);
builder.add(resolvedInterface);
}
return builder.build();
}
示例10: getCollectionItemType
import java.lang.reflect.WildcardType; //導入依賴的package包/類
public static Type getCollectionItemType(Type fieldType){
Type itemType = null;
Class<?> clazz = null;
if(fieldType instanceof ParameterizedType){
Type actualTypeArgument = ((ParameterizedType) fieldType).getActualTypeArguments()[0];
if(actualTypeArgument instanceof WildcardType){
WildcardType wildcardType = (WildcardType) actualTypeArgument;
Type[] upperBounds = wildcardType.getUpperBounds();
if(upperBounds.length == 1){
actualTypeArgument = upperBounds[0];
}
}
itemType = actualTypeArgument;
} else if(fieldType instanceof Class<?> //
&& !(clazz = (Class<?>) fieldType).getName().startsWith("java.")){
Type superClass = clazz.getGenericSuperclass();
itemType = TypeUtils.getCollectionItemType(superClass);
}
if(itemType == null){
itemType = Object.class;
}
return itemType;
}
示例11: getCollectionItemClass
import java.lang.reflect.WildcardType; //導入依賴的package包/類
public static Class<?> getCollectionItemClass(Type fieldType){
if(fieldType instanceof ParameterizedType){
Class<?> itemClass;
Type actualTypeArgument = ((ParameterizedType) fieldType).getActualTypeArguments()[0];
if(actualTypeArgument instanceof WildcardType){
WildcardType wildcardType = (WildcardType) actualTypeArgument;
Type[] upperBounds = wildcardType.getUpperBounds();
if(upperBounds.length == 1){
actualTypeArgument = upperBounds[0];
}
}
if(actualTypeArgument instanceof Class){
itemClass = (Class<?>) actualTypeArgument;
if(!Modifier.isPublic(itemClass.getModifiers())){
throw new JSONException("can not create ASMParser");
}
} else{
throw new JSONException("can not create ASMParser");
}
return itemClass;
}
return Object.class;
}
示例12: hashCode
import java.lang.reflect.WildcardType; //導入依賴的package包/類
/**
* Returns the hashCode of {@code type}.
*/
public static int hashCode(Type type) {
if (type instanceof Class) {
// Class specifies hashCode().
return type.hashCode();
} else if (type instanceof ParameterizedType) {
ParameterizedType p = (ParameterizedType) type;
return Arrays.hashCode(p.getActualTypeArguments())
^ p.getRawType().hashCode()
^ hashCodeOrZero(p.getOwnerType());
} else if (type instanceof GenericArrayType) {
return hashCode(((GenericArrayType) type).getGenericComponentType());
} else if (type instanceof WildcardType) {
WildcardType w = (WildcardType) type;
return Arrays.hashCode(w.getLowerBounds()) ^ Arrays.hashCode(w.getUpperBounds());
} else {
// This isn't a type we support. Probably a type variable
return hashCodeOrZero(type);
}
}
示例13: responseType
import java.lang.reflect.WildcardType; //導入依賴的package包/類
@Override
public Type responseType() {
Type rawType = ((ParameterizedType) returnType).getRawType();
if (rawType.equals(Call.class) || rawType.equals(MockableCall.class) || rawType.equals(com.gabrielsamojlo.offit.Call.class)) {
Type[] types = ((ParameterizedType) returnType).getActualTypeArguments();
Type paramType = types[0];
if (paramType instanceof WildcardType) {
return ((WildcardType) paramType).getUpperBounds()[0];
}
this.type = paramType;
return paramType;
} else {
CallAdapter callAdapter = retrofit.nextCallAdapter(factory, returnType, annotations);
return callAdapter.responseType();
}
}
示例14: newArrayType
import java.lang.reflect.WildcardType; //導入依賴的package包/類
/** 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);
}
示例15: getRawType
import java.lang.reflect.WildcardType; //導入依賴的package包/類
static Class<?> getRawType(Type type) {
if (type == null) throw new NullPointerException("type == null");
if (type instanceof Class<?>) {
// Type is a normal class.
return (Class<?>) type;
}
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
// I'm not exactly sure why getRawType() returns Type instead of Class. Neal isn't either but
// suspects some pathological case related to nested classes exists.
Type rawType = parameterizedType.getRawType();
if (!(rawType instanceof Class)) throw new IllegalArgumentException();
return (Class<?>) rawType;
}
if (type instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) type).getGenericComponentType();
return Array.newInstance(getRawType(componentType), 0).getClass();
}
if (type instanceof TypeVariable) {
// We could use the variable's bounds, but that won't work if there are multiple. Having a raw
// type that's more general than necessary is okay.
return Object.class;
}
if (type instanceof WildcardType) {
return getRawType(((WildcardType) type).getUpperBounds()[0]);
}
throw new IllegalArgumentException("Expected a Class, ParameterizedType, or "
+ "GenericArrayType, but <" + type + "> is of type " + type.getClass().getName());
}