/** Extracts the bound <code>Type</code> for a given {@link TypeVariable}. */ static Type extractBoundForTypeVariable(TypeVariable<?> typeVariable) { Type[] bounds = typeVariable.getBounds(); if (bounds.length == 0) { return Object.class; } Type bound = bounds[0]; if (bound instanceof TypeVariable) { bound = extractBoundForTypeVariable((TypeVariable<?>) bound); } return bound; }
/** * Determine the raw type for the given generic parameter type. * * @param genericType the generic type to resolve * @param typeVariableMap the TypeVariable Map to resolved against * @return the resolved raw type */ static Type getRawType(Type genericType, Map<TypeVariable<?>, Type> typeVariableMap) { Type resolvedType = genericType; if (genericType instanceof TypeVariable) { TypeVariable<?> tv = (TypeVariable<?>) genericType; resolvedType = typeVariableMap.get(tv); if (resolvedType == null) { resolvedType = extractBoundForTypeVariable(tv); } } if (resolvedType instanceof ParameterizedType) { return ((ParameterizedType) resolvedType).getRawType(); } else { return resolvedType; } }
Type resolvedType = typeVariableMap.get(typeVariableArgument); if (resolvedType == null) { resolvedType = extractBoundForTypeVariable(typeVariableArgument);
/** Extract a class instance from given Type. */ private static Class<?> extractClass(Class<?> ownerClass, Type arg) { if (arg instanceof ParameterizedType) { return extractClass(ownerClass, ((ParameterizedType) arg).getRawType()); } else if (arg instanceof GenericArrayType) { GenericArrayType gat = (GenericArrayType) arg; Type gt = gat.getGenericComponentType(); Class<?> componentClass = extractClass(ownerClass, gt); return Array.newInstance(componentClass, 0).getClass(); } else if (arg instanceof TypeVariable) { TypeVariable<?> tv = (TypeVariable<?>) arg; arg = getTypeVariableMap(ownerClass).get(tv); if (arg == null) { arg = extractBoundForTypeVariable(tv); } else { arg = extractClass(ownerClass, arg); } } return (arg instanceof Class ? (Class<?>) arg : Object.class); }