@Test public void testComplexParameterizedType() { Set<Type> allTypes = ReflectionHelper.getAllTypes(ConcreteServiceTwo.class); Assert.assertEquals(4, allTypes.size()); int lcv = 0; for (Type type : allTypes) { switch(lcv) { case 0: { Assert.assertEquals(ConcreteServiceTwo.class, type); break; } case 1: { Assert.assertTrue(type instanceof ParameterizedType); ParameterizedType pt = (ParameterizedType) type; Assert.assertEquals(ParameterizedClassOne.class, pt.getRawType()); Assert.assertEquals(1, pt.getActualTypeArguments().length); Assert.assertEquals(String.class, pt.getActualTypeArguments()[0]); break; } case 2: { Assert.assertEquals(Object.class, type); break; } case 3: { Assert.assertTrue(type instanceof ParameterizedType); ParameterizedType pt = (ParameterizedType) type; Assert.assertEquals(InterfaceFive.class, pt.getRawType()); Assert.assertEquals(1, pt.getActualTypeArguments().length); Type iFaceTypePT = pt.getActualTypeArguments()[0]; Assert.assertTrue(iFaceTypePT instanceof ParameterizedType); ParameterizedType iFacePT = (ParameterizedType) iFaceTypePT; Assert.assertEquals(List.class, iFacePT.getRawType()); Assert.assertEquals(1, iFacePT.getActualTypeArguments().length); Assert.assertEquals(String.class, iFacePT.getActualTypeArguments()[0]); break; } default: Assert.fail("Should never get here"); } lcv++; } }
public static Set<Type> getAllTypes(Type t) { LinkedHashSet<Type> retVal = new LinkedHashSet<Type>(); retVal.add(t); Class<?> rawClass = ReflectionHelper.getRawClass(t); if (rawClass == null) return retVal; Type genericSuperclass = rawClass.getGenericSuperclass(); while (genericSuperclass != null) { Class<?> rawSuperclass = ReflectionHelper.getRawClass(genericSuperclass); if (rawSuperclass == null) break; retVal.add(genericSuperclass); genericSuperclass = rawSuperclass.getGenericSuperclass(); } while (rawClass != null) { for (Type iface : rawClass.getGenericInterfaces()) { addAllInterfaceContracts(iface, retVal); } rawClass = rawClass.getSuperclass(); } LinkedHashSet<Type> altRetVal = new LinkedHashSet<Type>(); HashMap<Class<?>, ParameterizedType> class2TypeMap = new HashMap<Class<?>, ParameterizedType>(); for (Type foundType : retVal) { if (!(foundType instanceof ParameterizedType)) { altRetVal.add(foundType); continue; } ParameterizedType originalPt = (ParameterizedType) foundType; Class<?> rawType = getRawClass(foundType); class2TypeMap.put(rawType, originalPt); if (isFilledIn(originalPt)) { altRetVal.add(foundType); continue; } ParameterizedType pti = fillInPT(originalPt, class2TypeMap); altRetVal.add(pti); class2TypeMap.put(rawType, pti); } return altRetVal; }
ReflectionHelper { public static Set<Type> getAllTypes(Type t) { LinkedHashSet<Type> retVal = new LinkedHashSet<Type>(); retVal.add(t); Class<?> rawClass = ReflectionHelper.getRawClass(t); if (rawClass == null) return retVal; Type genericSuperclass = rawClass.getGenericSuperclass(); while (genericSuperclass != null) { Class<?> rawSuperclass = ReflectionHelper.getRawClass(genericSuperclass); if (rawSuperclass == null) break; retVal.add(genericSuperclass); genericSuperclass = rawSuperclass.getGenericSuperclass(); } while (rawClass != null) { for (Type iface : rawClass.getGenericInterfaces()) { addAllInterfaceContracts(iface, retVal); } rawClass = rawClass.getSuperclass(); } LinkedHashSet<Type> altRetVal = new LinkedHashSet<Type>(); HashMap<Class<?>, ParameterizedType> class2TypeMap = new HashMap<Class<?>, ParameterizedType>(); for (Type foundType : retVal) { if (!(foundType instanceof ParameterizedType)) { altRetVal.add(foundType); continue; } ParameterizedType originalPt = (ParameterizedType) foundType; Class<?> rawType = getRawClass(foundType); class2TypeMap.put(rawType, originalPt); if (isFilledIn(originalPt)) { altRetVal.add(foundType); continue; } ParameterizedType pti = fillInPT(originalPt, class2TypeMap); altRetVal.add(pti); class2TypeMap.put(rawType, pti); } return altRetVal; } }
ReflectionHelper { public static Set<Type> getAllTypes(Type t) { LinkedHashSet<Type> retVal = new LinkedHashSet<Type>(); retVal.add(t); Class<?> rawClass = ReflectionHelper.getRawClass(t); if (rawClass == null) return retVal; Type genericSuperclass = rawClass.getGenericSuperclass(); while (genericSuperclass != null) { Class<?> rawSuperclass = ReflectionHelper.getRawClass(genericSuperclass); if (rawSuperclass == null) break; retVal.add(genericSuperclass); genericSuperclass = rawSuperclass.getGenericSuperclass(); } while (rawClass != null) { for (Type iface : rawClass.getGenericInterfaces()) { addAllInterfaceContracts(iface, retVal); } rawClass = rawClass.getSuperclass(); } LinkedHashSet<Type> altRetVal = new LinkedHashSet<Type>(); HashMap<Class<?>, ParameterizedType> class2TypeMap = new HashMap<Class<?>, ParameterizedType>(); for (Type foundType : retVal) { if (!(foundType instanceof ParameterizedType)) { altRetVal.add(foundType); continue; } ParameterizedType originalPt = (ParameterizedType) foundType; Class<?> rawType = getRawClass(foundType); class2TypeMap.put(rawType, originalPt); if (isFilledIn(originalPt)) { altRetVal.add(foundType); continue; } ParameterizedType pti = fillInPT(originalPt, class2TypeMap); altRetVal.add(pti); class2TypeMap.put(rawType, pti); } return altRetVal; }  }
ReflectionHelper { public static Set<Type> getAllTypes(Type t) { LinkedHashSet<Type> retVal = new LinkedHashSet<Type>(); retVal.add(t); Class<?> rawClass = ReflectionHelper.getRawClass(t); if (rawClass == null) return retVal; Type genericSuperclass = rawClass.getGenericSuperclass(); while (genericSuperclass != null) { Class<?> rawSuperclass = ReflectionHelper.getRawClass(genericSuperclass); if (rawSuperclass == null) break; retVal.add(genericSuperclass); genericSuperclass = rawSuperclass.getGenericSuperclass(); } while (rawClass != null) { for (Type iface : rawClass.getGenericInterfaces()) { addAllInterfaceContracts(iface, retVal); } rawClass = rawClass.getSuperclass(); } LinkedHashSet<Type> altRetVal = new LinkedHashSet<Type>(); HashMap<Class<?>, ParameterizedType> class2TypeMap = new HashMap<Class<?>, ParameterizedType>(); for (Type foundType : retVal) { if (!(foundType instanceof ParameterizedType)) { altRetVal.add(foundType); continue; } ParameterizedType originalPt = (ParameterizedType) foundType; Class<?> rawType = getRawClass(foundType); class2TypeMap.put(rawType, originalPt); if (isFilledIn(originalPt)) { altRetVal.add(foundType); continue; } ParameterizedType pti = fillInPT(originalPt, class2TypeMap); altRetVal.add(pti); class2TypeMap.put(rawType, pti); } return altRetVal; }  static Class<?> getRawClass(Type type); static Type resolveField(Class<?> topclass, Field field); static Type resolveMember(Class<?> topclass, Type lookingForType, Class<?> declaringClass); static Type resolveKnownType(TypeVariable<?> userType, ParameterizedType knownType, Class<?> knownDeclaringClass); static Type getFirstTypeArgument(Type type); static String getName(Class<?> implClass); static Set<Type> getTypeClosure(Type ofType, Set<String> contracts); static Set<Type> getAdvertisedTypesFromClass(Type type, Class<? extends Annotation> markerAnnotation); static Set<Type> getAdvertisedTypesFromObject(Object t, Class<? extends Annotation> markerAnnotation); static Set<String> getContractsFromClass(Class<?> clazz, Class<? extends Annotation> markerAnnotation); static Annotation getScopeAnnotationFromObject(Object t); static Annotation getScopeAnnotationFromClass(Class<?> clazz); static Annotation getScopeFromObject(Object t, Annotation annoDefault); static Annotation getScopeFromClass(Class<?> clazz, Annotation annoDefault); static boolean isAnnotationAQualifier(Annotation anno); static Set<Annotation> getQualifiersFromObject(Object t); static Set<String> getQualifiersFromClass(Class<?> clazz); static Set<Annotation> getQualifierAnnotations(final AnnotatedElement annotatedGuy); static String writeSet(Set<?> set); static String writeSet(Set<?> set, Object excludeMe); static void readSet(String line, Collection<String> addToMe); static void readMetadataMap(String line, Map<String, List<String>> addToMe); static String writeMetadata(Map<String, List<String>> metadata); static void addMetadata(Map<String, List<String>> metadatas, String key, String value); static boolean removeMetadata(Map<String, List<String>> metadatas, String key, String value); static boolean removeAllMetadata(Map<String, List<String>> metadatas, String key); static Map<String, List<String>> deepCopyMetadata(Map<String, List<String>> copyMe); static void setField(Field field, Object instance, Object value); static Object invoke(Object o, Method m, Object args[], boolean neutralCCL); static boolean isStatic(Member member); static Object makeMe(Constructor<?> c, Object args[], boolean neutralCCL); static void parseServiceMetadataString(String metadataField, Map<String, List<String>> metadata); static String getNameFromAllQualifiers(Set<Annotation> qualifiers, AnnotatedElement parent); static boolean annotationContainsAll(final Set<Annotation> candidateAnnotations, final Set<Annotation> requiredAnnotations); static Class<?> translatePrimitiveType(Class<?> type); static boolean isPrivate(Member member); static Set<Type> getAllTypes(Type t); static MethodWrapper createMethodWrapper(Method wrapMe); @SuppressWarnings("unchecked") static T cast(Object o); }
ReflectionHelper { public static Set<Type> getAllTypes(Type t) { LinkedHashSet<Type> retVal = new LinkedHashSet<Type>(); retVal.add(t); Class<?> rawClass = ReflectionHelper.getRawClass(t); if (rawClass == null) return retVal; Type genericSuperclass = rawClass.getGenericSuperclass(); while (genericSuperclass != null) { Class<?> rawSuperclass = ReflectionHelper.getRawClass(genericSuperclass); if (rawSuperclass == null) break; retVal.add(genericSuperclass); genericSuperclass = rawSuperclass.getGenericSuperclass(); } while (rawClass != null) { for (Type iface : rawClass.getGenericInterfaces()) { addAllInterfaceContracts(iface, retVal); } rawClass = rawClass.getSuperclass(); } LinkedHashSet<Type> altRetVal = new LinkedHashSet<Type>(); HashMap<Class<?>, ParameterizedType> class2TypeMap = new HashMap<Class<?>, ParameterizedType>(); for (Type foundType : retVal) { if (!(foundType instanceof ParameterizedType)) { altRetVal.add(foundType); continue; } ParameterizedType originalPt = (ParameterizedType) foundType; Class<?> rawType = getRawClass(foundType); class2TypeMap.put(rawType, originalPt); if (isFilledIn(originalPt)) { altRetVal.add(foundType); continue; } ParameterizedType pti = fillInPT(originalPt, class2TypeMap); altRetVal.add(pti); class2TypeMap.put(rawType, pti); } return altRetVal; }  static Class<?> getRawClass(Type type); static Type resolveField(Class<?> topclass, Field field); static Type resolveMember(Class<?> topclass, Type lookingForType, Class<?> declaringClass); static Type resolveKnownType(TypeVariable<?> userType, ParameterizedType knownType, Class<?> knownDeclaringClass); static Type getFirstTypeArgument(Type type); static String getName(Class<?> implClass); static Set<Type> getTypeClosure(Type ofType, Set<String> contracts); static Set<Type> getAdvertisedTypesFromClass(Type type, Class<? extends Annotation> markerAnnotation); static Set<Type> getAdvertisedTypesFromObject(Object t, Class<? extends Annotation> markerAnnotation); static Set<String> getContractsFromClass(Class<?> clazz, Class<? extends Annotation> markerAnnotation); static Annotation getScopeAnnotationFromObject(Object t); static Annotation getScopeAnnotationFromClass(Class<?> clazz); static Annotation getScopeFromObject(Object t, Annotation annoDefault); static Annotation getScopeFromClass(Class<?> clazz, Annotation annoDefault); static boolean isAnnotationAQualifier(Annotation anno); static Set<Annotation> getQualifiersFromObject(Object t); static Set<String> getQualifiersFromClass(Class<?> clazz); static Set<Annotation> getQualifierAnnotations(final AnnotatedElement annotatedGuy); static String writeSet(Set<?> set); static String writeSet(Set<?> set, Object excludeMe); static void readSet(String line, Collection<String> addToMe); static void readMetadataMap(String line, Map<String, List<String>> addToMe); static String writeMetadata(Map<String, List<String>> metadata); static void addMetadata(Map<String, List<String>> metadatas, String key, String value); static boolean removeMetadata(Map<String, List<String>> metadatas, String key, String value); static boolean removeAllMetadata(Map<String, List<String>> metadatas, String key); static Map<String, List<String>> deepCopyMetadata(Map<String, List<String>> copyMe); static void setField(Field field, Object instance, Object value); static Object invoke(Object o, Method m, Object args[], boolean neutralCCL); static boolean isStatic(Member member); static Object makeMe(Constructor<?> c, Object args[], boolean neutralCCL); static void parseServiceMetadataString(String metadataField, Map<String, List<String>> metadata); static String getNameFromAllQualifiers(Set<Annotation> qualifiers, AnnotatedElement parent); static boolean annotationContainsAll(final Set<Annotation> candidateAnnotations, final Set<Annotation> requiredAnnotations); static Class<?> translatePrimitiveType(Class<?> type); static boolean isPrivate(Member member); static Set<Type> getAllTypes(Type t); static MethodWrapper createMethodWrapper(Method wrapMe); @SuppressWarnings("unchecked") static T cast(Object o);  }