ReflectionUtils { public static Type unserializeType(String serializedType, ClassLoader classLoader) throws ClassNotFoundException { String sType = serializedType.replaceAll(" ", ""); Type type = null; if (sType.contains(OPEN_GENERIC)) { int firstInferior = sType.indexOf(OPEN_GENERIC); int lastSuperior = sType.lastIndexOf(CLOSE_GENERIC); String rawType = sType.substring(0, firstInferior); String sArguments = sType.substring(firstInferior + 1, lastSuperior); List<Type> argumentTypes = new ArrayList<>(); int nestedArgsDepth = 0; int previousSplit = 0; for (int i = 0; i < sArguments.length(); i++) { char current = sArguments.charAt(i); switch (current) { case '<': nestedArgsDepth++; break; case '>': nestedArgsDepth--; break; case ',': if (nestedArgsDepth == 0) { argumentTypes.add(unserializeType(sArguments.substring(previousSplit, i), classLoader)); previousSplit = i + 1; } break; default: break; } if (i == sArguments.length() - 1) { argumentTypes.add(unserializeType(sArguments.substring(previousSplit), classLoader)); } } type = new DefaultParameterizedType(null, Class.forName(rawType, false, classLoader), argumentTypes.toArray(new Type[1])); } else { type = Class.forName(sType, false, classLoader); } return type; } private  ReflectionUtils(); static Collection<Field> getAllFields(Class<?> clazz); static Field getField(Class<?> clazz, String fieldName); static Class getTypeClass(Type type); static void setFieldValue(Object instanceContainingField, String fieldName, Object fieldValue); static Class<?> getLastGenericFieldType(Field field); static Type getLastFieldGenericArgument(Field field); static Type getLastTypeGenericArgument(Type type); static Class<?> getLastGenericClassType(Class clazz, Class filterClass); static Type getGenericClassType(Class clazz, Class filterClass); static Type[] resolveSuperArguments(Type[] parameters, Type childType); static Type[] resolveSuperArguments(Type[] parameters, Class childClass, Type[] childParameters); static Type resolveType(Type type, Map<TypeVariable, Type> typeMapping); static Type resolveType(Type targetType, Type rootType); static Type unserializeType(String serializedType, ClassLoader classLoader); static T getDirectAnnotation(Class<T> annotationClass, AnnotatedElement element); static List<Type> getDirectTypes(Type type); @Unstable static String serializeType(Type type); @Unstable static Collection<Method> getAllMethods(Class<?> clazz);  }
@Test void unserializeType() throws Exception { Type simpleType = ComponentRole.class; assertEquals(simpleType, ReflectionUtils.unserializeType("org.xwiki.component.descriptor.ComponentRole", Thread.currentThread().getContextClassLoader())); }
@Test void unserializeTypeWithGenerics() throws Exception { Type genericsType = new DefaultParameterizedType(null, ComponentRole.class, String.class); assertEquals(genericsType, ReflectionUtils.unserializeType("org.xwiki.component.descriptor.ComponentRole<java.lang.String>", Thread.currentThread().getContextClassLoader())); }
@Test void unserializeListType() throws Exception { Type listType = new DefaultParameterizedType(null, List.class, ComponentRole.class); assertEquals(listType, ReflectionUtils.unserializeType("java.util.List<org.xwiki.component.descriptor.ComponentRole>", Thread.currentThread().getContextClassLoader())); }
@Test void unserializeMapType() throws Exception { Type mapType = new DefaultParameterizedType(null, Map.class, String.class, ComponentRole.class); assertEquals(mapType, ReflectionUtils.unserializeType( "java.util.Map<java.lang.String, " + "org.xwiki.component.descriptor.ComponentRole>", Thread.currentThread().getContextClassLoader())); }
@Test void unserializeMapTypeWithGenerics() throws Exception { Type annotatedType = new DefaultParameterizedType(null, ComponentRole.class, String.class); Type mapType = new DefaultParameterizedType(null, Map.class, String.class, annotatedType); assertEquals(mapType, ReflectionUtils.unserializeType( "java.util.Map<java.lang.String, org.xwiki.component.descriptor.ComponentRole<java.lang.String>>", Thread.currentThread().getContextClassLoader())); }
@Test void testUnserializeMapInMapWithTypeWithGenerics() throws Exception { Type annotatedType = new DefaultParameterizedType(null, ComponentRole.class, String.class); Type mapType1 = new DefaultParameterizedType(null, Map.class, String.class, annotatedType); Type mapType2 = new DefaultParameterizedType(null, Map.class, String.class, mapType1); assertEquals(mapType2, ReflectionUtils.unserializeType( "java.util.Map<java.lang.String, java.util.Map<java.lang.String, " + "org.xwiki.component.descriptor.ComponentRole<java.lang.String>>>", Thread.currentThread().getContextClassLoader())); }