public static Class<?> getTypeParameterAsClass(java.lang.reflect.Type type, int index) { Type parameter = getTypeParameter(type, index); if (parameter != null) { return asClass(parameter); } else { return null; } }
private static Class<?> asClass(Type type) { if (type instanceof WildcardType) { WildcardType wildcardType = (WildcardType) type; if (wildcardType.getUpperBounds()[0] instanceof Class) { return (Class<?>) wildcardType.getUpperBounds()[0]; } else if (wildcardType.getUpperBounds()[0] instanceof ParameterizedType) { return (Class<?>) ((ParameterizedType) wildcardType.getUpperBounds()[0]).getRawType(); } else { return Object.class; } } else if (type instanceof TypeVariable) { return asClass(((TypeVariable) type).getBounds()[0]); } else if (type instanceof ParameterizedType) { return (Class<?>) ((ParameterizedType) type).getRawType(); } else if (type instanceof GenericArrayType) { Type component = ((GenericArrayType) type).getGenericComponentType(); return Array.newInstance(asClass(component), 0).getClass(); } else if (type instanceof Class) { return (Class<?>) type; } else { throw new IllegalArgumentException(type.getClass().toString()); } }
public static Class<?> getTypeParameterAsClass(java.lang.reflect.Type type, int index) { Type parameter = getTypeParameter(type, index); if (parameter != null) { return asClass(parameter); } else { return null; } }
private static Class<?> asClass(Type type) { if (type instanceof WildcardType) { WildcardType wildcardType = (WildcardType) type; if (wildcardType.getUpperBounds()[0] instanceof Class) { return (Class<?>) wildcardType.getUpperBounds()[0]; } else if (wildcardType.getUpperBounds()[0] instanceof ParameterizedType) { return (Class<?>) ((ParameterizedType) wildcardType.getUpperBounds()[0]).getRawType(); } else { return Object.class; } } else if (type instanceof TypeVariable) { return asClass(((TypeVariable) type).getBounds()[0]); } else if (type instanceof ParameterizedType) { return (Class<?>) ((ParameterizedType) type).getRawType(); } else if (type instanceof GenericArrayType) { Type component = ((GenericArrayType) type).getGenericComponentType(); return Array.newInstance(asClass(component), 0).getClass(); } else if (type instanceof Class) { return (Class<?>) type; } else { throw new IllegalArgumentException(type.getClass().toString()); } }