本文整理匯總了Java中java.lang.reflect.TypeVariable類的典型用法代碼示例。如果您正苦於以下問題:Java TypeVariable類的具體用法?Java TypeVariable怎麽用?Java TypeVariable使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
TypeVariable類屬於java.lang.reflect包,在下文中一共展示了TypeVariable類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: resolveVariable
import java.lang.reflect.TypeVariable; //導入依賴的package包/類
private static ResolvableType resolveVariable(TypeVariable<?> typeVariable, ResolvableType contextType) {
ResolvableType resolvedType;
if (contextType.hasGenerics()) {
resolvedType = ResolvableType.forType(typeVariable, contextType);
if (resolvedType.resolve() != null) {
return resolvedType;
}
}
ResolvableType superType = contextType.getSuperType();
if (superType != ResolvableType.NONE) {
resolvedType = resolveVariable(typeVariable, superType);
if (resolvedType.resolve() != null) {
return resolvedType;
}
}
for (ResolvableType ifc : contextType.getInterfaces()) {
resolvedType = resolveVariable(typeVariable, ifc);
if (resolvedType.resolve() != null) {
return resolvedType;
}
}
return ResolvableType.NONE;
}
示例2: toString
import java.lang.reflect.TypeVariable; //導入依賴的package包/類
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Class<?> rawType = getRawType();
if (!rawType.isArray()) {
sb.append(rawType.getName());
} else {
sb.append(rawType.getComponentType().getName());
}
TypeVariable<? extends Class<?>>[] typeParameters = rawType.getTypeParameters();
if (typeParameters.length > 0) {
sb.append("<");
for (int i = 0; i < typeParameters.length; i++) {
if (i > 0) {
sb.append(", ");
}
String s = this.getParam(i).toParamString();
sb.append(s);
}
sb.append(">");
}
if (rawType.isArray()) {
sb.append("[]");
}
return sb.toString();
}
示例3: getClass
import java.lang.reflect.TypeVariable; //導入依賴的package包/類
public static Class<?> getClass(Type type){
if(type.getClass() == Class.class){
return (Class<?>) type;
}
if(type instanceof ParameterizedType){
return getClass(((ParameterizedType) type).getRawType());
}
if(type instanceof TypeVariable){
Type boundType = ((TypeVariable<?>) type).getBounds()[0];
return (Class<?>) boundType;
}
if(type instanceof WildcardType){
Type[] upperBounds = ((WildcardType) type).getUpperBounds();
if (upperBounds.length == 1) {
return getClass(upperBounds[0]);
}
}
return Object.class;
}
示例4: getTypeClass
import java.lang.reflect.TypeVariable; //導入依賴的package包/類
protected static Class getTypeClass(final Type type, final Map<TypeVariable<?>, Type> typeArgs) {
Type rawType = type;
if (type instanceof ParameterizedType) {
rawType = ((ParameterizedType) type).getRawType();
}
if (rawType instanceof Class) {
return (Class) rawType;
}
if (rawType instanceof TypeVariable) {
final Type t = typeArgs.get(rawType);
if (null != t) {
return getTypeClass(t, typeArgs);
}
}
// cannot resolve - default to UnknownGenericType;
return UnknownGenericType.class;
}
示例5: checkOneParameterType
import java.lang.reflect.TypeVariable; //導入依賴的package包/類
private static void checkOneParameterType(ParameterizedType toCheck, Class<?> rawType,
Class<?>... bounds) {
System.out.println(((Class<?>) toCheck.getRawType()).getName()
.equals(rawType.getName()));
Type[] parameters = toCheck.getActualTypeArguments();
System.out.println(parameters.length);
TypeVariable<?> parameter = (TypeVariable<?>) parameters[0];
System.out.println(parameter.getName());
Type[] actualBounds = parameter.getBounds();
for (int i = 0; i < bounds.length; i++) {
System.out.println(((Class<?>) actualBounds[i]).getName().equals(bounds[i].getName()));
}
}
示例6: TypeLiteral
import java.lang.reflect.TypeVariable; //導入依賴的package包/類
/**
* Constructs a new type literal. Derives represented class from type
* parameter.
*
* <p>
* Clients create an empty anonymous subclass. Doing so embeds the type
* parameter in the anonymous class's type hierarchy so we can reconstitute
* it at runtime despite erasure.
*/
public TypeLiteral() {
ParameterizedType genClass = (ParameterizedType) getClass().getGenericSuperclass();
Type[] types = genClass.getActualTypeArguments();
if (types == null || types.length == 0) {
throw new RuntimeException("TypeLiteral<T> must have a specfied type <T>");
}
this.type = types[0];
if (this.type instanceof GenericArrayType)
throw new RuntimeException("TypeLiteral does not support GenericArrayTypes, use Injector.get(ComponentType[].class)");
if (this.type instanceof TypeVariable)
throw new RuntimeException("TypeLiteral does not support TypeVariables");
if (type instanceof Class) {
this.rawType = (Class<T>) this.type;
} else if (type instanceof ParameterizedType) {
this.rawType = (Class<T>) ((ParameterizedType) type).getRawType();
} else {
this.rawType = Object.class;
}
}
示例7: getGenericClass
import java.lang.reflect.TypeVariable; //導入依賴的package包/類
public static Class getGenericClass(ParameterizedType parameterizedType, int i) {
Object genericClass = parameterizedType.getActualTypeArguments()[i];
if (genericClass instanceof ParameterizedType) { // 處理多級泛型
return (Class) ((ParameterizedType) genericClass).getRawType();
} else if (genericClass instanceof GenericArrayType) { // 處理數組泛型
return (Class) ((GenericArrayType) genericClass).getGenericComponentType();
} else if (genericClass instanceof TypeVariable) { // 處理泛型擦拭對象
return (Class) getClass(((TypeVariable) genericClass).getBounds()[0], 0);
} else {
return (Class) genericClass;
}
}
示例8: testNewTypeVariable
import java.lang.reflect.TypeVariable; //導入依賴的package包/類
public void testNewTypeVariable() throws Exception {
TypeVariable<?> noBoundJvmType =
WithTypeVariable.getTypeVariable("withoutBound");
TypeVariable<?> objectBoundJvmType =
WithTypeVariable.getTypeVariable("withObjectBound");
TypeVariable<?> upperBoundJvmType =
WithTypeVariable.getTypeVariable("withUpperBound");
TypeVariable<?> noBound = withBounds(noBoundJvmType);
TypeVariable<?> objectBound = withBounds(objectBoundJvmType, Object.class);
TypeVariable<?> upperBound = withBounds(
upperBoundJvmType, Number.class, CharSequence.class);
assertEqualTypeVariable(noBoundJvmType, noBound);
assertEqualTypeVariable(noBoundJvmType,
withBounds(noBoundJvmType, Object.class));
assertEqualTypeVariable(objectBoundJvmType, objectBound);
assertEqualTypeVariable(upperBoundJvmType, upperBound);
new TypeVariableEqualsTester()
.addEqualityGroup(noBoundJvmType, noBound)
.addEqualityGroup(objectBoundJvmType, objectBound)
.addEqualityGroup(upperBoundJvmType, upperBound)
.testEquals();
}
示例9: hasUnresolvableType
import java.lang.reflect.TypeVariable; //導入依賴的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);
}
示例10: getInheritGenericType
import java.lang.reflect.TypeVariable; //導入依賴的package包/類
public static Type getInheritGenericType(Class<?> clazz, TypeVariable<?> tv) {
Type type = null;
GenericDeclaration gd = tv.getGenericDeclaration();
do {
type = clazz.getGenericSuperclass();
if (type == null) {
return null;
}
if (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
if (ptype.getRawType() == gd) {
TypeVariable<?>[] tvs = gd.getTypeParameters();
Type[] types = ptype.getActualTypeArguments();
for (int i = 0; i < tvs.length; i++) {
if (tvs[i] == tv) return types[i];
}
return null;
}
}
clazz = TypeUtils.getClass(type);
} while (type != null);
return null;
}
示例11: resolveType
import java.lang.reflect.TypeVariable; //導入依賴的package包/類
/**
* Resolves all type variables in {@code type} and all downstream types and returns a
* corresponding type with type variables resolved.
*/
public Type resolveType(Type type) {
checkNotNull(type);
if (type instanceof TypeVariable) {
return typeTable.resolve((TypeVariable<?>) type);
} else if (type instanceof ParameterizedType) {
return resolveParameterizedType((ParameterizedType) type);
} else if (type instanceof GenericArrayType) {
return resolveGenericArrayType((GenericArrayType) type);
} else if (type instanceof WildcardType) {
return resolveWildcardType((WildcardType) type);
} else {
// if Class<?>, no resolution needed, we are done.
return type;
}
}
示例12: forTypeVariable
import java.lang.reflect.TypeVariable; //導入依賴的package包/類
private WildcardCapturer forTypeVariable(final TypeVariable<?> typeParam) {
return new WildcardCapturer(id) {
@Override TypeVariable<?> captureAsTypeVariable(Type[] upperBounds) {
Set<Type> combined = new LinkedHashSet<>(asList(upperBounds));
// Since this is an artifically generated type variable, we don't bother checking
// subtyping between declared type bound and actual type bound. So it's possible that we
// may generate something like <capture#1-of ? extends Foo&SubFoo>.
// Checking subtype between declared and actual type bounds
// adds recursive isSubtypeOf() call and feels complicated.
// There is no contract one way or another as long as isSubtypeOf() works as expected.
combined.addAll(asList(typeParam.getBounds()));
if (combined.size() > 1) { // Object is implicit and only useful if it's the only bound.
combined.remove(Object.class);
}
return super.captureAsTypeVariable(combined.toArray(new Type[0]));
}
};
}
示例13: createSignature
import java.lang.reflect.TypeVariable; //導入依賴的package包/類
private static Signature createSignature(final Object input, final Type type, final Map<TypeVariable<?>, Type> typeArgs, final boolean isInput) {
final Class clazz = getTypeClass(type, typeArgs);
if (Tuple.class.isAssignableFrom(clazz)) {
final TypeVariable[] tupleTypes = getTypeClass(type, typeArgs).getTypeParameters();
final Map<TypeVariable<?>, Type> classTypeArgs = TypeUtils.getTypeArguments(type, clazz);
Collection<? extends Type> types = TypeUtils.getTypeArguments(type, clazz).values();
Class[] classes = new Class[types.size()];
int i = 0;
for (final TypeVariable tupleType : tupleTypes) {
classes[i++] = getTypeClass(classTypeArgs.get(tupleType), typeArgs);
}
return new TupleSignature(input, clazz, classes, isInput);
}
return new SingletonSignature(input, clazz, isInput);
}
示例14: substituteTypeVariables
import java.lang.reflect.TypeVariable; //導入依賴的package包/類
private static Type substituteTypeVariables(Map<String, Type> lookup, Type type) {
if (type instanceof TypeVariable) {
return translateTypeVariable(lookup, (TypeVariable) type);
}
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
Type[] args = pType.getActualTypeArguments();
for (int i = 0; i < args.length; i++) {
args[i] = substituteTypeVariables(lookup, args[i]);
}
return new ParameterizedTypeImpl(args, pType.getOwnerType(), pType.getRawType());
}
if (type instanceof GenericArrayType) {
GenericArrayType gaType = (GenericArrayType) type;
return new GenericArrayTypeImpl(substituteTypeVariables(lookup, gaType.getGenericComponentType()));
}
return type;
}
示例15: getGenericSuperclass
import java.lang.reflect.TypeVariable; //導入依賴的package包/類
/**
* 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;
}
@SuppressWarnings("unchecked") // super class of T
TypeToken<? super T> superToken = (TypeToken<? super T>) resolveSupertype(superclass);
return superToken;
}