本文整理汇总了Java中java.lang.reflect.TypeVariable.getBounds方法的典型用法代码示例。如果您正苦于以下问题:Java TypeVariable.getBounds方法的具体用法?Java TypeVariable.getBounds怎么用?Java TypeVariable.getBounds使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.reflect.TypeVariable
的用法示例。
在下文中一共展示了TypeVariable.getBounds方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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()));
}
}
示例2: deserialze
import java.lang.reflect.TypeVariable; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
if (type instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) type).getGenericComponentType();
if (componentType instanceof TypeVariable) {
TypeVariable<?> componentVar = (TypeVariable<?>) componentType;
componentType = componentVar.getBounds()[0];
}
List<Object> list = new ArrayList<Object>();
parser.parseArray(componentType, list);
Class<?> componentClass;
if (componentType instanceof Class) {
componentClass = (Class<?>) componentType;
Object[] array = (Object[]) Array.newInstance(componentClass, list.size());
list.toArray(array);
return (T) array;
} else {
return (T) list.toArray();
}
}
if (type instanceof Class && type != Object.class && type != Serializable.class) {
return (T) parser.parseObject(type);
}
return (T) parser.parse(fieldName);
}
示例3: main
import java.lang.reflect.TypeVariable; //导入方法依赖的package包/类
public static void main(String[] args) throws NoSuchMethodException, SecurityException, NoSuchFieldException {
for (TypeVariable<Class<Generic>> var : Generic.class.getTypeParameters()) {
System.out.println(var.getName());
Type bound = var.getBounds()[0];
System.out.println(((Class<?>) bound).getName().equals(AA.class.getName()));
}
Field f = Generic.class.getField("f");
ParameterizedType fieldType = (java.lang.reflect.ParameterizedType)f.getGenericType();
checkOneParameterType(fieldType, Generic.class, AA.class);
ParameterizedType methodReturnType =
(ParameterizedType) Generic.class.getMethod("get").getGenericReturnType();
checkOneParameterType(methodReturnType, Generic.class, AA.class);
}
示例4: extractType
import java.lang.reflect.TypeVariable; //导入方法依赖的package包/类
private static Class extractType(TypeVariable typeVariable) {
java.lang.reflect.Type[] boundTypes = typeVariable.getBounds();
if ( boundTypes == null || boundTypes.length != 1 ) {
return null;
}
return (Class) boundTypes[0];
}
示例5: VariableJavaType
import java.lang.reflect.TypeVariable; //导入方法依赖的package包/类
VariableJavaType(final ImplementClass implementClass, final TypeVariable<?> typeVariable) {
this.implementClass = implementClass;
this.variableName = typeVariable.getName();
this.bounds = typeVariable.getBounds();
this.javaTypeBounds = new JavaType[this.bounds.length];
this.general = getJavaType(0);
}
示例6: deserialze
import java.lang.reflect.TypeVariable; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
if (type instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) type).getGenericComponentType();
if (componentType instanceof TypeVariable) {
TypeVariable<?> componentVar = (TypeVariable<?>) componentType;
componentType = componentVar.getBounds()[0];
}
List<Object> list = new ArrayList<Object>();
parser.parseArray(componentType, list);
Class<?> componentClass;
if (componentType instanceof Class) {
componentClass = (Class<?>) componentType;
if (componentClass == boolean.class) {
return (T) TypeUtils.cast(list, boolean[].class, parser.getConfig());
} else if (componentClass == short.class) {
return (T) TypeUtils.cast(list, short[].class, parser.getConfig());
} else if (componentClass == int.class) {
return (T) TypeUtils.cast(list, int[].class, parser.getConfig());
} else if (componentClass == long.class) {
return (T) TypeUtils.cast(list, long[].class, parser.getConfig());
} else if (componentClass == float.class) {
return (T) TypeUtils.cast(list, float[].class, parser.getConfig());
} else if (componentClass == double.class) {
return (T) TypeUtils.cast(list, double[].class, parser.getConfig());
}
Object[] array = (Object[]) Array.newInstance(componentClass, list.size());
list.toArray(array);
return (T) array;
} else {
return (T) list.toArray();
}
}
return (T) parser.parse(fieldName);
}
示例7: deserialize
import java.lang.reflect.TypeVariable; //导入方法依赖的package包/类
@SuppressWarnings({"unchecked", "rawtypes"})
public <T> T deserialize(Object object, Type type) {
JSONArray jsonArray;
if (object instanceof JSONArray) {
jsonArray = (JSONArray) object;
} else {
jsonArray = new JSONArray(object);
}
Class componentClass = null;
Type componentType = null;
if (type instanceof GenericArrayType) {
componentType = ((GenericArrayType) type).getGenericComponentType();
if (componentType instanceof TypeVariable) {
TypeVariable<?> componentVar = (TypeVariable<?>) componentType;
componentType = componentVar.getBounds()[0];
}
if (componentType instanceof Class<?>) {
componentClass = (Class<?>) componentType;
}
} else {
Class clazz = (Class) type;
componentType = componentClass = clazz.getComponentType();
}
int size = jsonArray.size();
Object array = Array.newInstance(componentClass, size);
for (int i = 0; i < size; i++) {
Object value = jsonArray.get(i);
Deserializer deserializer = JSONParser.getDeserializer(componentClass);
Array.set(array, i, deserializer.deserialize(value, componentType));
}
return (T) array;
}
示例8: resolveInternal
import java.lang.reflect.TypeVariable; //导入方法依赖的package包/类
/**
* Resolves {@code var} using the encapsulated type mapping. If it maps to yet another
* non-reified type or has bounds, {@code forDependants} is used to do further resolution, which
* doesn't try to resolve any type variable on generic declarations that are already being
* resolved.
*
* <p>Should only be called and overridden by {@link #resolve(TypeVariable)}.
*/
Type resolveInternal(TypeVariable<?> var, TypeTable forDependants) {
Type type = map.get(new TypeVariableKey(var));
if (type == null) {
Type[] bounds = var.getBounds();
if (bounds.length == 0) {
return var;
}
Type[] resolvedBounds = new TypeResolver(forDependants).resolveTypes(bounds);
/*
* We'd like to simply create our own TypeVariable with the newly resolved bounds. There's
* just one problem: Starting with JDK 7u51, the JDK TypeVariable's equals() method doesn't
* recognize instances of our TypeVariable implementation. This is a problem because users
* compare TypeVariables from the JDK against TypeVariables returned by TypeResolver. To
* work with all JDK versions, TypeResolver must return the appropriate TypeVariable
* implementation in each of the three possible cases:
*
* 1. Prior to JDK 7u51, the JDK TypeVariable implementation interoperates with ours.
* Therefore, we can always create our own TypeVariable.
*
* 2. Starting with JDK 7u51, the JDK TypeVariable implementations does not interoperate
* with ours. Therefore, we have to be careful about whether we create our own TypeVariable:
*
* 2a. If the resolved types are identical to the original types, then we can return the
* original, identical JDK TypeVariable. By doing so, we sidestep the problem entirely.
*
* 2b. If the resolved types are different from the original types, things are trickier. The
* only way to get a TypeVariable instance for the resolved types is to create our own. The
* created TypeVariable will not interoperate with any JDK TypeVariable. But this is OK: We
* don't _want_ our new TypeVariable to be equal to the JDK TypeVariable because it has
* _different bounds_ than the JDK TypeVariable. And it wouldn't make sense for our new
* TypeVariable to be equal to any _other_ JDK TypeVariable, either, because any other JDK
* TypeVariable must have a different declaration or name. The only TypeVariable that our
* new TypeVariable _will_ be equal to is an equivalent TypeVariable that was also created
* by us. And that equality is guaranteed to hold because it doesn't involve the JDK
* TypeVariable implementation at all.
*/
if (Types.NativeTypeVariableEquals.NATIVE_TYPE_VARIABLE_ONLY
&& Arrays.equals(bounds, resolvedBounds)) {
return var;
}
return Types.newArtificialTypeVariable(
var.getGenericDeclaration(), var.getName(), resolvedBounds);
}
// in case the type is yet another type variable.
return new TypeResolver(forDependants).resolveType(type);
}
示例9: getReifiedType
import java.lang.reflect.TypeVariable; //导入方法依赖的package包/类
private static ReifiedType getReifiedType(Type targetType, Collection<Type> variableTypes) {
if (targetType instanceof Class) {
if (Object.class.equals(targetType)) {
return OBJECT;
}
return new GenericsReifiedType((Class<?>) targetType);
}
if (targetType instanceof ParameterizedType) {
Type ata = ((ParameterizedType) targetType).getActualTypeArguments()[0];
return getReifiedType(ata, variableTypes);
}
if (targetType instanceof WildcardType) {
WildcardType wt = (WildcardType) targetType;
Type[] lowerBounds = wt.getLowerBounds();
if (ObjectUtils.isEmpty(lowerBounds)) {
// there's always an upper bound (Object)
Type upperBound = wt.getUpperBounds()[0];
return getReifiedType(upperBound, variableTypes);
}
return getReifiedType(lowerBounds[0], variableTypes);
}
if (targetType instanceof TypeVariable) {
TypeVariable<?> typeVariable = (TypeVariable<?>) targetType;
if (variableTypes.contains(targetType)) {
//Looped around on itself via a recursive Generics definition
return OBJECT;
}
variableTypes.add(targetType);
Type[] bounds = typeVariable.getBounds();
return getReifiedType(bounds[0], variableTypes);
}
if (targetType instanceof GenericArrayType) {
return getReifiedType(((GenericArrayType) targetType).getGenericComponentType(), variableTypes);
}
throw new IllegalArgumentException("Unknown type " + targetType.getClass());
}