@Test public void testGetCollectionType() throws SecurityException, NoSuchFieldException { assertEquals(Object.class, getCollectionType(Collection.class)); assertEquals(Number.class, getCollectionType(tListN)); assertEquals(Number.class, expandType(getCollectionType(tListE), ParametrizedClass.class)); assertEquals(Object.class, expandType(getCollectionType(tListF), ParametrizedClass.class)); assertEquals(Object.class, expandType(getCollectionType(tListI), ParametrizedClass.class)); assertEquals(Number.class, expandType(getCollectionType(tListIEN), ParametrizedClass.class)); ParameterizedType colType = (ParameterizedType) expandType(getCollectionType(tListCN), ParametrizedClass.class); assertEquals(Collection.class, colType.getRawType()); }
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);  }