@Test void unserializeType() throws Exception { Type simpleType = ComponentRole.class; assertEquals(simpleType, ReflectionUtils.unserializeType("org.xwiki.component.descriptor.ComponentRole", Thread.currentThread().getContextClassLoader())); }
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; }
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; } }
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(); }
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); }
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);  }