ReflectionUtils { @Unstable public static String serializeType(Type type) { if (type == null) { return null; } StringBuilder sb = new StringBuilder(); if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; String rawTypeName = getTypeName(parameterizedType.getRawType()); if (parameterizedType.getOwnerType() != null) { if (parameterizedType.getOwnerType() instanceof Class) { sb.append(((Class<?>) parameterizedType.getOwnerType()).getName()); } else { sb.append(parameterizedType.getOwnerType().toString()); } sb.append('.'); if (parameterizedType.getOwnerType() instanceof ParameterizedType) { sb.append(rawTypeName.replace( ((Class<?>) ((ParameterizedType) parameterizedType.getOwnerType()).getRawType()) .getName() + '$', "")); } else { sb.append(rawTypeName); } } else { sb.append(rawTypeName); } if (parameterizedType.getActualTypeArguments() != null && parameterizedType.getActualTypeArguments().length > 0) { sb.append(OPEN_GENERIC); boolean first = true; for (Type typeArgument : parameterizedType.getActualTypeArguments()) { if (!first) { sb.append(", "); } sb.append(getTypeName(typeArgument)); first = false; } sb.append(CLOSE_GENERIC); } } else { sb.append(getTypeName(type)); } return sb.toString(); } 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 serializeType() { Type type = new DefaultParameterizedType(null, Void.class); assertEquals("java.lang.Void", ReflectionUtils.serializeType(type)); type = new DefaultParameterizedType(Collections.class, List.class); assertEquals("java.util.Collections.java.util.List", ReflectionUtils.serializeType(type)); type = new DefaultParameterizedType(Collections.class, List.class, String.class); assertEquals("java.util.Collections.java.util.List<java.lang.String>", ReflectionUtils.serializeType(type)); type = new DefaultParameterizedType(null, Map.class, Integer.class, String.class); assertEquals("java.util.Map<java.lang.Integer, java.lang.String>", ReflectionUtils.serializeType(type)); type = new DefaultParameterizedType(null, Map.class, Integer.class, new DefaultParameterizedType(null, List.class, String.class)); assertEquals("java.util.Map<java.lang.Integer, java.util.List<java.lang.String>>", ReflectionUtils.serializeType(type)); type = String.class; assertEquals("java.lang.String", ReflectionUtils.serializeType(type)); Map<String, List<Integer>> stringListMap = new HashMap<>(); assertEquals("java.util.HashMap", ReflectionUtils.serializeType(stringListMap.getClass())); assertNull(ReflectionUtils.serializeType(null)); }