@Test public void testGetCollectionTypeArray() { assertEquals(int.class, getCollectionType(int[].class)); assertEquals(Integer.class, getCollectionType(Integer[].class)); assertEquals(Object.class, getCollectionType(Object[].class)); }
public final static Type getCollectionType(Type type) { if (type instanceof GenericArrayType) { return ((GenericArrayType) type).getGenericComponentType(); } else if (type instanceof Class<?>) { Class<?> clazz = (Class<?>) type; if (clazz.isArray()) return clazz.getComponentType(); else if (Collection.class.isAssignableFrom(clazz)) { return Object.class; } } else if (type instanceof ParameterizedType && Collection.class.isAssignableFrom(getRawClass(type))) { return typeOf(0, type); } throw new IllegalArgumentException( "Could not extract parametrized type, are you sure it is a Collection or an Array?"); }
TypeUtil { public final static Type getCollectionType(Type type) { if (type instanceof GenericArrayType) { return ((GenericArrayType) type).getGenericComponentType(); } else if (type instanceof Class<?>) { Class<?> clazz = (Class<?>) type; if (clazz.isArray()) return clazz.getComponentType(); else if (Collection.class.isAssignableFrom(clazz)) { return Object.class; } } else if (type instanceof ParameterizedType && Collection.class.isAssignableFrom(getRawClass(type))) { return typeOf(0, type); } throw new IllegalArgumentException( "Could not extract parametrized type, are you sure it is a Collection or an Array?"); } }
TypeUtil { public final static Type getCollectionType(Type type) { if (type instanceof GenericArrayType) { return ((GenericArrayType) type).getGenericComponentType(); } else if (type instanceof Class<?>) { Class<?> clazz = (Class<?>) type; if (clazz.isArray()) return clazz.getComponentType(); else if (Collection.class.isAssignableFrom(clazz)) { return Object.class; } } else if (type instanceof ParameterizedType && Collection.class.isAssignableFrom(getRawClass(type))) { return typeOf(0, type); } throw new IllegalArgumentException( "Could not extract parametrized type, are you sure it is a Collection or an Array?"); }  }
TypeUtil { public final static Type getCollectionType(Type type) { if (type instanceof GenericArrayType) { return ((GenericArrayType) type).getGenericComponentType(); } else if (type instanceof Class<?>) { Class<?> clazz = (Class<?>) type; if (clazz.isArray()) return clazz.getComponentType(); else if (Collection.class.isAssignableFrom(clazz)) { return Object.class; } } else if (type instanceof ParameterizedType && Collection.class.isAssignableFrom(getRawClass(type))) { return typeOf(0, type); } throw new IllegalArgumentException( "Could not extract parametrized type, are you sure it is a Collection or an Array?"); }  final static Class<?> wrap(Class<?> clazz); final static Type expandType(final Type type, final Type rootType); final static Type lookupGenericType(Class<?> ofClass, Class<?> inClass); final static Class<?> getRawClass(Type type); final static Type getCollectionType(Type type); final static Type resolveTypeVariable(TypeVariable<? extends GenericDeclaration> type, Class<?> inClass); final static boolean match(Type type, Type oType, boolean strictMatch); final static Type typeOf(int parameterIdx, Type fromType); }
TypeUtil { public final static Type getCollectionType(Type type) { if (type instanceof GenericArrayType) { return ((GenericArrayType) type).getGenericComponentType(); } else if (type instanceof Class<?>) { Class<?> clazz = (Class<?>) type; if (clazz.isArray()) return clazz.getComponentType(); else if (Collection.class.isAssignableFrom(clazz)) { return Object.class; } } else if (type instanceof ParameterizedType && Collection.class.isAssignableFrom(getRawClass(type))) { return typeOf(0, type); } throw new IllegalArgumentException( "Could not extract parametrized type, are you sure it is a Collection or an Array?"); }  final static Class<?> wrap(Class<?> clazz); final static Type expandType(final Type type, final Type rootType); final static Type lookupGenericType(Class<?> ofClass, Class<?> inClass); final static Class<?> getRawClass(Type type); final static Type getCollectionType(Type type); final static Type resolveTypeVariable(TypeVariable<? extends GenericDeclaration> type, Class<?> inClass); final static boolean match(Type type, Type oType, boolean strictMatch); final static Type typeOf(int parameterIdx, Type fromType);  }