@Test public void getParameterizedClassArgumentsTest() { ReflectionHelper.DeclaringClassInterfacePair dcip = ReflectionHelper.getClass(TestNoInterface.class, I.class); Class[] arguments = ReflectionHelper.getParameterizedClassArguments(dcip); final Class aClass = arguments[0]; dcip = ReflectionHelper.getClass(TestInterface.class, I.class); arguments = ReflectionHelper.getParameterizedClassArguments(dcip); assertEquals(aClass, arguments[0]); }
public static Class[] getParameterizedClassArguments(final DeclaringClassInterfacePair p) { if (p.genericInterface instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) p.genericInterface; final Type[] as = pt.getActualTypeArguments(); final Class[] cas = new Class[as.length]; for (int i = 0; i < as.length; i++) { final Type a = as[i]; if (a instanceof Class) { cas[i] = (Class) a; } else if (a instanceof ParameterizedType) { pt = (ParameterizedType) a; cas[i] = (Class) pt.getRawType(); } else if (a instanceof TypeVariable) { final TypeVariable tv = (TypeVariable) a; final ClassTypePair ctp = resolveTypeVariable(p.concreteClass, p.declaringClass, tv); cas[i] = (ctp != null) ? ctp.rawClass() : (Class<?>) (tv.getBounds()[0]); } else if (a instanceof GenericArrayType) { final GenericArrayType gat = (GenericArrayType) a; final Type t = gat.getGenericComponentType(); if (t instanceof Class) { cas[i] = getArrayForComponentType((Class<?>) t); } } } return cas; } else { return null; } }
ReflectionHelper { public static Class[] getParameterizedClassArguments(final DeclaringClassInterfacePair p) { if (p.genericInterface instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) p.genericInterface; final Type[] as = pt.getActualTypeArguments(); final Class[] cas = new Class[as.length]; for (int i = 0; i < as.length; i++) { final Type a = as[i]; if (a instanceof Class) { cas[i] = (Class) a; } else if (a instanceof ParameterizedType) { pt = (ParameterizedType) a; cas[i] = (Class) pt.getRawType(); } else if (a instanceof TypeVariable) { final TypeVariable tv = (TypeVariable) a; final ClassTypePair ctp = resolveTypeVariable(p.concreteClass, p.declaringClass, tv); cas[i] = (ctp != null) ? ctp.rawClass() : (Class<?>) (tv.getBounds()[0]); } else if (a instanceof GenericArrayType) { final GenericArrayType gat = (GenericArrayType) a; final Type t = gat.getGenericComponentType(); if (t instanceof Class) { cas[i] = getArrayForComponentType((Class<?>) t); } } } return cas; } else { return null; } } }
ReflectionHelper { public static Class[] getParameterizedClassArguments(final DeclaringClassInterfacePair p) { if (p.genericInterface instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) p.genericInterface; final Type[] as = pt.getActualTypeArguments(); final Class[] cas = new Class[as.length]; for (int i = 0; i < as.length; i++) { final Type a = as[i]; if (a instanceof Class) { cas[i] = (Class) a; } else if (a instanceof ParameterizedType) { pt = (ParameterizedType) a; cas[i] = (Class) pt.getRawType(); } else if (a instanceof TypeVariable) { final TypeVariable tv = (TypeVariable) a; final ClassTypePair ctp = resolveTypeVariable(p.concreteClass, p.declaringClass, tv); cas[i] = (ctp != null) ? ctp.rawClass() : (Class<?>) (tv.getBounds()[0]); } else if (a instanceof GenericArrayType) { final GenericArrayType gat = (GenericArrayType) a; final Type t = gat.getGenericComponentType(); if (t instanceof Class) { cas[i] = getArrayForComponentType((Class<?>) t); } } } return cas; } else { return null; } } private  ReflectionHelper(); }
ReflectionHelper { public static Class[] getParameterizedClassArguments(final DeclaringClassInterfacePair p) { if (p.genericInterface instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) p.genericInterface; final Type[] as = pt.getActualTypeArguments(); final Class[] cas = new Class[as.length]; for (int i = 0; i < as.length; i++) { final Type a = as[i]; if (a instanceof Class) { cas[i] = (Class) a; } else if (a instanceof ParameterizedType) { pt = (ParameterizedType) a; cas[i] = (Class) pt.getRawType(); } else if (a instanceof TypeVariable) { final TypeVariable tv = (TypeVariable) a; final ClassTypePair ctp = resolveTypeVariable(p.concreteClass, p.declaringClass, tv); cas[i] = (ctp != null) ? ctp.rawClass() : (Class<?>) (tv.getBounds()[0]); } else if (a instanceof GenericArrayType) { final GenericArrayType gat = (GenericArrayType) a; final Type t = gat.getGenericComponentType(); if (t instanceof Class) { cas[i] = getArrayForComponentType((Class<?>) t); } } } return cas; } else { return null; } } private  ReflectionHelper(); static Class<?> getDeclaringClass(final AccessibleObject ao); static String objectToString(final Object o); static String methodInstanceToString(final Object o, final Method m); static PrivilegedAction<Class<T>> classForNamePA(final String name); @SuppressWarnings("unchecked") static PrivilegedAction<Class<T>> classForNamePA(final String name, final ClassLoader cl); static PrivilegedAction<ClassLoader> getClassLoaderPA(final Class<?> clazz); static PrivilegedAction<Field[]> getDeclaredFieldsPA(final Class<?> clazz); static PrivilegedAction<Field[]> getAllFieldsPA(final Class<?> clazz); static PrivilegedAction<Collection<? extends Method>> getDeclaredMethodsPA(final Class<?> clazz); static PrivilegedExceptionAction<Class<T>> classForNameWithExceptionPEA(final String name); @SuppressWarnings("unchecked") static PrivilegedExceptionAction<Class<T>> classForNameWithExceptionPEA(final String name, final ClassLoader cl); static PrivilegedAction<ClassLoader> getContextClassLoaderPA(); static PrivilegedAction setContextClassLoaderPA(final ClassLoader classLoader); static PrivilegedAction setAccessibleMethodPA(final Method m); static List<Class<?>> getGenericTypeArgumentClasses(final Type type); static List<ClassTypePair> getTypeArgumentAndClass(final Type type); static boolean isPrimitive(final Type type); static Type[] getTypeArguments(final Type type); static Type getTypeArgument(final Type type, final int index); @SuppressWarnings("unchecked") static Class<T> erasure(final Type type); static boolean isSubClassOf(final Type subType, final Type superType); static boolean isArray(final Type type); static boolean isArrayOfType(final Type type, final Class<?> componentType); static Type getArrayComponentType(final Type type); static Class<?> getArrayForComponentType(final Class<?> c); @SuppressWarnings("unchecked") static PrivilegedAction<Method> getValueOfStringMethodPA(final Class<?> clazz); @SuppressWarnings("unchecked") static PrivilegedAction<Method> getFromStringStringMethodPA(final Class<?> clazz); static PrivilegedAction<Constructor> getStringConstructorPA(final Class<?> clazz); static PrivilegedAction<Constructor<?>[]> getDeclaredConstructorsPA(final Class<?> clazz); static PrivilegedAction<Constructor<?>> getDeclaredConstructorPA(final Class<?> clazz, final Class<?>... params); static Collection<Class<? extends Annotation>> getAnnotationTypes(final AnnotatedElement annotatedElement,
                                                                             final Class<? extends Annotation> metaAnnotation); static boolean isGetter(final Method method); static GenericType genericTypeFor(final Object instance); static boolean isSetter(final Method method); static String getPropertyName(final Method method); static Class<?> theMostSpecificTypeOf(final Set<Type> contractTypes); static Class[] getParameterizedClassArguments(final DeclaringClassInterfacePair p); static Type[] getParameterizedTypeArguments(final DeclaringClassInterfacePair p); static DeclaringClassInterfacePair getClass(final Class<?> concrete, final Class<?> iface); static ClassTypePair resolveGenericType(final Class concreteClass, final Class declaringClass,
                                                   final Class rawResolvedType, final Type genericResolvedType); static ClassTypePair resolveTypeVariable(final Class<?> c, final Class<?> dc, final TypeVariable tv); static PrivilegedAction<Method> findMethodOnClassPA(final Class<?> c, final Method m); static PrivilegedAction<Method[]> getMethodsPA(final Class<?> c); static Method findOverridingMethodOnClass(final Class<?> clazz, final Method method); static OsgiRegistry getOsgiRegistryInstance(); static InputStream getResourceAsStream(final ClassLoader loader, final Class<?> originClass, final String name); static Class<?> getRawClass(Type type); }
ReflectionHelper { public static Class[] getParameterizedClassArguments(final DeclaringClassInterfacePair p) { if (p.genericInterface instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) p.genericInterface; final Type[] as = pt.getActualTypeArguments(); final Class[] cas = new Class[as.length]; for (int i = 0; i < as.length; i++) { final Type a = as[i]; if (a instanceof Class) { cas[i] = (Class) a; } else if (a instanceof ParameterizedType) { pt = (ParameterizedType) a; cas[i] = (Class) pt.getRawType(); } else if (a instanceof TypeVariable) { final TypeVariable tv = (TypeVariable) a; final ClassTypePair ctp = resolveTypeVariable(p.concreteClass, p.declaringClass, tv); cas[i] = (ctp != null) ? ctp.rawClass() : (Class<?>) (tv.getBounds()[0]); } else if (a instanceof GenericArrayType) { final GenericArrayType gat = (GenericArrayType) a; final Type t = gat.getGenericComponentType(); if (t instanceof Class) { cas[i] = getArrayForComponentType((Class<?>) t); } } } return cas; } else { return null; } } private  ReflectionHelper(); static Class<?> getDeclaringClass(final AccessibleObject ao); static String objectToString(final Object o); static String methodInstanceToString(final Object o, final Method m); static PrivilegedAction<Class<T>> classForNamePA(final String name); @SuppressWarnings("unchecked") static PrivilegedAction<Class<T>> classForNamePA(final String name, final ClassLoader cl); static PrivilegedAction<ClassLoader> getClassLoaderPA(final Class<?> clazz); static PrivilegedAction<Field[]> getDeclaredFieldsPA(final Class<?> clazz); static PrivilegedAction<Field[]> getAllFieldsPA(final Class<?> clazz); static PrivilegedAction<Collection<? extends Method>> getDeclaredMethodsPA(final Class<?> clazz); static PrivilegedExceptionAction<Class<T>> classForNameWithExceptionPEA(final String name); @SuppressWarnings("unchecked") static PrivilegedExceptionAction<Class<T>> classForNameWithExceptionPEA(final String name, final ClassLoader cl); static PrivilegedAction<ClassLoader> getContextClassLoaderPA(); static PrivilegedAction setContextClassLoaderPA(final ClassLoader classLoader); static PrivilegedAction setAccessibleMethodPA(final Method m); static List<Class<?>> getGenericTypeArgumentClasses(final Type type); static List<ClassTypePair> getTypeArgumentAndClass(final Type type); static boolean isPrimitive(final Type type); static Type[] getTypeArguments(final Type type); static Type getTypeArgument(final Type type, final int index); @SuppressWarnings("unchecked") static Class<T> erasure(final Type type); static boolean isSubClassOf(final Type subType, final Type superType); static boolean isArray(final Type type); static boolean isArrayOfType(final Type type, final Class<?> componentType); static Type getArrayComponentType(final Type type); static Class<?> getArrayForComponentType(final Class<?> c); @SuppressWarnings("unchecked") static PrivilegedAction<Method> getValueOfStringMethodPA(final Class<?> clazz); @SuppressWarnings("unchecked") static PrivilegedAction<Method> getFromStringStringMethodPA(final Class<?> clazz); static PrivilegedAction<Constructor> getStringConstructorPA(final Class<?> clazz); static PrivilegedAction<Constructor<?>[]> getDeclaredConstructorsPA(final Class<?> clazz); static PrivilegedAction<Constructor<?>> getDeclaredConstructorPA(final Class<?> clazz, final Class<?>... params); static Collection<Class<? extends Annotation>> getAnnotationTypes(final AnnotatedElement annotatedElement,
                                                                             final Class<? extends Annotation> metaAnnotation); static boolean isGetter(final Method method); static GenericType genericTypeFor(final Object instance); static boolean isSetter(final Method method); static String getPropertyName(final Method method); static Class<?> theMostSpecificTypeOf(final Set<Type> contractTypes); static Class[] getParameterizedClassArguments(final DeclaringClassInterfacePair p); static Type[] getParameterizedTypeArguments(final DeclaringClassInterfacePair p); static DeclaringClassInterfacePair getClass(final Class<?> concrete, final Class<?> iface); static ClassTypePair resolveGenericType(final Class concreteClass, final Class declaringClass,
                                                   final Class rawResolvedType, final Type genericResolvedType); static ClassTypePair resolveTypeVariable(final Class<?> c, final Class<?> dc, final TypeVariable tv); static PrivilegedAction<Method> findMethodOnClassPA(final Class<?> c, final Method m); static PrivilegedAction<Method[]> getMethodsPA(final Class<?> c); static Method findOverridingMethodOnClass(final Class<?> clazz, final Method method); static OsgiRegistry getOsgiRegistryInstance(); static InputStream getResourceAsStream(final ClassLoader loader, final Class<?> originClass, final String name); static Class<?> getRawClass(Type type);  }