JSONDocTypeBuilder { public static JSONDocType build(JSONDocType jsondocType, Class<?> clazz, Type type) { if(clazz.isAssignableFrom(JSONDocDefaultType.class)) { jsondocType.addItemToType(UNDEFINED); return jsondocType; } if (Map.class.isAssignableFrom(clazz)) { jsondocType.addItemToType(getCustomClassName(clazz)); if (type instanceof ParameterizedType) { Type mapKeyType = ((ParameterizedType) type).getActualTypeArguments()[0]; Type mapValueType = ((ParameterizedType) type).getActualTypeArguments()[1]; jsondocType.setMapKey(new JSONDocType()); jsondocType.setMapValue(new JSONDocType()); if (mapKeyType instanceof Class) { jsondocType.setMapKey(new JSONDocType(((Class<?>) mapKeyType).getSimpleName().toLowerCase())); } else if (mapKeyType instanceof WildcardType) { jsondocType.setMapKey(new JSONDocType(WILDCARD)); } else if(mapKeyType instanceof TypeVariable<?>){ jsondocType.setMapKey(new JSONDocType(((TypeVariable<?>) mapKeyType).getName())); } else { jsondocType.setMapKey(build(jsondocType.getMapKey(), (Class<?>) ((ParameterizedType) mapKeyType).getRawType(), mapKeyType)); } if (mapValueType instanceof Class) { jsondocType.setMapValue(new JSONDocType(((Class<?>) mapValueType).getSimpleName().toLowerCase())); } else if (mapValueType instanceof WildcardType) { jsondocType.setMapValue(new JSONDocType(WILDCARD)); } else if(mapValueType instanceof TypeVariable<?>){ jsondocType.setMapValue(new JSONDocType(((TypeVariable<?>) mapValueType).getName())); } else { jsondocType.setMapValue(build(jsondocType.getMapValue(), (Class<?>) ((ParameterizedType) mapValueType).getRawType(), mapValueType)); } } } else if (Collection.class.isAssignableFrom(clazz)) { if (type instanceof ParameterizedType) { Type parametrizedType = ((ParameterizedType) type).getActualTypeArguments()[0]; jsondocType.addItemToType(getCustomClassName(clazz)); if (parametrizedType instanceof Class) { jsondocType.addItemToType(getCustomClassName((Class<?>) parametrizedType)); } else if (parametrizedType instanceof WildcardType) { jsondocType.addItemToType(WILDCARD); } else if(parametrizedType instanceof TypeVariable<?>){ jsondocType.addItemToType(((TypeVariable<?>) parametrizedType).getName()); } else { return build(jsondocType, (Class<?>) ((ParameterizedType) parametrizedType).getRawType(), parametrizedType); } } else if (type instanceof GenericArrayType) { return build(jsondocType, clazz, ((GenericArrayType) type).getGenericComponentType()); } else { jsondocType.addItemToType(getCustomClassName(clazz)); } } else if (clazz.isArray()) { jsondocType.addItemToType(ARRAY); Class<?> componentType = clazz.getComponentType(); return build(jsondocType, componentType, type); } else { jsondocType.addItemToType(getCustomClassName(clazz)); if (type instanceof ParameterizedType) { Type parametrizedType = ((ParameterizedType) type).getActualTypeArguments()[0]; if (parametrizedType instanceof Class) { jsondocType.addItemToType(getCustomClassName((Class<?>) parametrizedType)); } else if (parametrizedType instanceof WildcardType) { jsondocType.addItemToType(WILDCARD); } else if(parametrizedType instanceof TypeVariable<?>){ jsondocType.addItemToType(((TypeVariable<?>) parametrizedType).getName()); } else { return build(jsondocType, (Class<?>) ((ParameterizedType) parametrizedType).getRawType(), parametrizedType); } } } return jsondocType; }  static JSONDocType build(JSONDocType jsondocType, Class<?> clazz, Type type);  }
@Test public void testReflex() throws NoSuchMethodException, SecurityException, ClassNotFoundException, JsonGenerationException, JsonMappingException, IOException { mapper.setSerializationInclusion(Include.NON_NULL); JSONDocType jsonDocType = new JSONDocType(); Method method = JSONDocTypeBuilderTest.class.getMethod("getString"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("string", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getInteger"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("integer", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getInt"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("int", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getLong"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("long", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getlong"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("long", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getListString"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("list of string", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getListSetString"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("list of set of string", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getStringArray"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("array of string", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getIntegerArray"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("array of integer", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getListOfStringArray"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("array of list of string", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getSetOfStringArray"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("array of set of string", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getList"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("list", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getListOfWildcard"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("list of wildcard", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getListOfWildcardArray"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("array of list of wildcard", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getListArray"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("array of list", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getSetArray"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("array of set", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getMap"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("map", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getHashMap"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("hashmap", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getMapStringInteger"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("map[string, integer]", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getMapListOfStringInteger"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("map[list of string, integer]", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getMapStringSetOfInteger"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("map[string, set of integer]", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getMapListOfStringSetOfInteger"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("map[list of string, set of integer]", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getMapListOfSetOfStringSetOfInteger"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("map[list of set of string, set of integer]", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getMapWildcardInteger"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("map[wildcard, integer]", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getMapWildcardWildcard"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("map[wildcard, wildcard]", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getMapListOfWildcardWildcard"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("map[list of wildcard, wildcard]", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getMapMapInteger"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("map[map, integer]", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getMapMapStringLongInteger"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("map[map[string, long], integer]", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getResponseEntityString"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("responseentity of string", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getResponseEntityListOfString"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("responseentity of list of string", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getParentPojoList"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("list of my_parent", jsonDocType.getOneLineText()); System.out.println("---------------------------"); jsonDocType = new JSONDocType(); method = JSONDocTypeBuilderTest.class.getMethod("getSpecializedWGenericsPojo"); JSONDocTypeBuilder.build(jsonDocType, method.getReturnType(), method.getGenericReturnType()); System.out.println(mapper.writeValueAsString(jsonDocType)); System.out.println(jsonDocType.getOneLineText()); Assert.assertEquals("fooPojo of T", jsonDocType.getOneLineText()); System.out.println("---------------------------"); }