@Test public void testLookupWithGenerics() { assertNotNull(lookupGenericType(Collection.class, List.class)); assertNull(lookupGenericType(List.class, Collection.class)); }
public final static Type lookupGenericType(Class<?> ofClass, Class<?> inClass) { if (ofClass == null || inClass == null || !ofClass.isAssignableFrom(inClass)) return null; if (ofClass.equals(inClass)) return inClass; if (ofClass.isInterface()) { Class<?>[] interfaces = inClass.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { if (ofClass.equals(interfaces[i])) { return inClass.getGenericInterfaces()[i]; } else { Type superType = lookupGenericType(ofClass, interfaces[i]); if (superType != null) return superType; } } } Class<?> superClass = inClass.getSuperclass(); if (ofClass.equals(superClass)) return inClass.getGenericSuperclass(); return lookupGenericType(ofClass, inClass.getSuperclass()); }
TypeUtil { public final static Type lookupGenericType(Class<?> ofClass, Class<?> inClass) { if (ofClass == null || inClass == null || !ofClass.isAssignableFrom(inClass)) return null; if (ofClass.equals(inClass)) return inClass; if (ofClass.isInterface()) { Class<?>[] interfaces = inClass.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { if (ofClass.equals(interfaces[i])) { return inClass.getGenericInterfaces()[i]; } else { Type superType = lookupGenericType(ofClass, interfaces[i]); if (superType != null) return superType; } } } Class<?> superClass = inClass.getSuperclass(); if (ofClass.equals(superClass)) return inClass.getGenericSuperclass(); return lookupGenericType(ofClass, inClass.getSuperclass()); } }
TypeUtil { public final static Type lookupGenericType(Class<?> ofClass, Class<?> inClass) { if (ofClass == null || inClass == null || !ofClass.isAssignableFrom(inClass)) return null; if (ofClass.equals(inClass)) return inClass; if (ofClass.isInterface()) { Class<?>[] interfaces = inClass.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { if (ofClass.equals(interfaces[i])) { return inClass.getGenericInterfaces()[i]; } else { Type superType = lookupGenericType(ofClass, interfaces[i]); if (superType != null) return superType; } } } Class<?> superClass = inClass.getSuperclass(); if (ofClass.equals(superClass)) return inClass.getGenericSuperclass(); return lookupGenericType(ofClass, inClass.getSuperclass()); }  }
TypeUtil { public final static Type lookupGenericType(Class<?> ofClass, Class<?> inClass) { if (ofClass == null || inClass == null || !ofClass.isAssignableFrom(inClass)) return null; if (ofClass.equals(inClass)) return inClass; if (ofClass.isInterface()) { Class<?>[] interfaces = inClass.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { if (ofClass.equals(interfaces[i])) { return inClass.getGenericInterfaces()[i]; } else { Type superType = lookupGenericType(ofClass, interfaces[i]); if (superType != null) return superType; } } } Class<?> superClass = inClass.getSuperclass(); if (ofClass.equals(superClass)) return inClass.getGenericSuperclass(); return lookupGenericType(ofClass, inClass.getSuperclass()); }  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 lookupGenericType(Class<?> ofClass, Class<?> inClass) { if (ofClass == null || inClass == null || !ofClass.isAssignableFrom(inClass)) return null; if (ofClass.equals(inClass)) return inClass; if (ofClass.isInterface()) { Class<?>[] interfaces = inClass.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { if (ofClass.equals(interfaces[i])) { return inClass.getGenericInterfaces()[i]; } else { Type superType = lookupGenericType(ofClass, interfaces[i]); if (superType != null) return superType; } } } Class<?> superClass = inClass.getSuperclass(); if (ofClass.equals(superClass)) return inClass.getGenericSuperclass(); return lookupGenericType(ofClass, inClass.getSuperclass()); }  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);  }