Skip to content

Commit

Permalink
fix #539
Browse files Browse the repository at this point in the history
  • Loading branch information
shalousun committed Jul 11, 2023
1 parent 3b48aad commit ec6bee5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private Set<Class<? extends Enum>> getEnumImplementsByInterface(Class<?> enumCla

public JavaClass getClassByName(String simpleName) {
JavaClass cls = javaProjectBuilder.getClassByName(simpleName);
List<DocJavaField> fieldList = JavaClassUtil.getFields(cls, 0, new LinkedHashMap<>());
List<DocJavaField> fieldList = JavaClassUtil.getFields(cls, 0, new LinkedHashMap<>(),null);
// handle inner class
if (Objects.isNull(cls.getFields()) || fieldList.isEmpty()) {
cls = classFilesMap.get(simpleName);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/power/doc/helper/FormDataBuildHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ public static List<FormData> getFormData(String className, Map<String, String> r
String simpleName = DocClassUtil.getSimpleName(className);
String[] globGicName = DocClassUtil.getSimpleGicName(className);
JavaClass cls = builder.getJavaProjectBuilder().getClassByName(simpleName);
List<DocJavaField> fields = JavaClassUtil.getFields(cls, 0, new LinkedHashMap<>());

List<DocJavaField> fields = JavaClassUtil.getFields(cls, 0, new LinkedHashMap<>(),builder.getApiConfig().getClassLoader());
if (JavaClassValidateUtil.isPrimitive(simpleName)) {
FormData formData = new FormData();
formData.setKey(pre);
Expand Down
40 changes: 20 additions & 20 deletions src/main/java/com/power/doc/helper/JsonBuildHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,17 @@ public static String buildReturnJson(DocJavaMethod docJavaMethod, ProjectDocConf
return StringUtil.removeQuotes(DocUtil.jsonValueByType(typeName));
}
if (DocGlobalConstants.JAVA_STRING_FULLY.equals(method.getReturnType().getGenericCanonicalName())
&& Objects.isNull(responseBodyAdvice)) {
&& Objects.isNull(responseBodyAdvice)) {
return "string";
}
String returnTypeGenericCanonicalName = method.getReturnType().getGenericCanonicalName();
if (Objects.nonNull(responseBodyAdvice)
&& Objects.isNull(method.getTagByName(IGNORE_RESPONSE_BODY_ADVICE))) {
&& Objects.isNull(method.getTagByName(IGNORE_RESPONSE_BODY_ADVICE))) {
if (!returnTypeGenericCanonicalName.startsWith(responseBodyAdvice)) {
StringBuilder sb = new StringBuilder();
sb.append(responseBodyAdvice)
.append("<")
.append(returnTypeGenericCanonicalName).append(">");
.append("<")
.append(returnTypeGenericCanonicalName).append(">");
returnTypeGenericCanonicalName = sb.toString();
}
}
Expand All @@ -121,7 +121,7 @@ public static String buildReturnJson(DocJavaMethod docJavaMethod, ProjectDocConf
}

return JsonUtil.toPrettyFormat(buildJson(typeName, returnType, Boolean.TRUE, 0,
new HashMap<>(), new HashSet<>(0), builder));
new HashMap<>(), new HashSet<>(0), builder));
}

/**
Expand All @@ -135,7 +135,7 @@ public static String buildReturnJson(DocJavaMethod docJavaMethod, ProjectDocConf
* @return String
*/
public static String buildJson(String typeName, String genericCanonicalName,
boolean isResp, int counter, Map<String, String> registryClasses, Set<String> groupClasses, ProjectDocConfigBuilder builder) {
boolean isResp, int counter, Map<String, String> registryClasses, Set<String> groupClasses, ProjectDocConfigBuilder builder) {

Map<String, String> genericMap = new HashMap<>(10);
JavaClass javaClass = builder.getJavaProjectBuilder().getClassByName(typeName);
Expand Down Expand Up @@ -214,8 +214,8 @@ public static String buildJson(String typeName, String genericCanonicalName,
String gicName = genericCanonicalName.substring(genericCanonicalName.indexOf(",") + 1, genericCanonicalName.lastIndexOf(">"));
if (DocGlobalConstants.JAVA_OBJECT_FULLY.equals(gicName)) {
data.append("{").append("\"mapKey\":")
.append("{\"waring\":\"You may use java.util.Object for Map value; smart-doc can't be handle.\"}")
.append("}");
.append("{\"waring\":\"You may use java.util.Object for Map value; smart-doc can't be handle.\"}")
.append("}");
} else if (JavaClassValidateUtil.isPrimitive(gicName)) {
data.append("{").append("\"mapKey1\":").append(DocUtil.jsonValueByType(gicName)).append(",");
data.append("\"mapKey2\":").append(DocUtil.jsonValueByType(gicName)).append("}");
Expand All @@ -225,7 +225,7 @@ public static String buildJson(String typeName, String genericCanonicalName,
data.append("{").append("\"mapKey\":").append(json).append("}");
} else {
data.append("{").append("\"mapKey\":")
.append(buildJson(gicName, genericCanonicalName, isResp, counter + 1, registryClasses, groupClasses, builder)).append("}");
.append(buildJson(gicName, genericCanonicalName, isResp, counter + 1, registryClasses, groupClasses, builder)).append("}");
}
return data.toString();
} else if (DocGlobalConstants.JAVA_OBJECT_FULLY.equals(typeName)) {
Expand All @@ -237,7 +237,7 @@ public static String buildJson(String typeName, String genericCanonicalName,
} else {
boolean requestFieldToUnderline = builder.getApiConfig().isRequestFieldToUnderline();
boolean responseFieldToUnderline = builder.getApiConfig().isResponseFieldToUnderline();
List<DocJavaField> fields = JavaClassUtil.getFields(cls, 0, new LinkedHashMap<>());
List<DocJavaField> fields = JavaClassUtil.getFields(cls, 0, new LinkedHashMap<>(), builder.getApiConfig().getClassLoader());
Map<String, String> ignoreFields = JavaClassUtil.getClassJsonIgnoreFields(cls);
out:
for (DocJavaField docField : fields) {
Expand Down Expand Up @@ -287,15 +287,15 @@ public static String buildJson(String typeName, String genericCanonicalName,
CustomField customRequestField = CustomField.nameEquals(key, builder.getCustomReqFieldMap());
if (customRequestField != null) {
if (JavaClassUtil.isTargetChildClass(typeName, customRequestField.getOwnerClassName()) && (customRequestField.isIgnore())
&& !isResp) {
&& !isResp) {
continue;
} else {
fieldName = StringUtil.isEmpty(customRequestField.getReplaceName()) ? fieldName : customRequestField.getReplaceName();
}
}
if (customResponseField != null) {
if (JavaClassUtil.isTargetChildClass(typeName, customResponseField.getOwnerClassName()) && (customResponseField.isIgnore())
&& isResp) {
&& isResp) {
continue;
} else {
fieldName = StringUtil.isEmpty(customResponseField.getReplaceName()) ? fieldName : customResponseField.getReplaceName();
Expand Down Expand Up @@ -346,9 +346,9 @@ public static String buildJson(String typeName, String genericCanonicalName,
} else {
if (!typeName.equals(gicName1)) {
data0.append("[").append(
buildJson(DocClassUtil.getSimpleName(gicName1), gicName1, isResp, nextLevel, registryClasses, groupClasses,
builder))
.append("]").append(",");
buildJson(DocClassUtil.getSimpleName(gicName1), gicName1, isResp, nextLevel, registryClasses, groupClasses,
builder))
.append("]").append(",");
} else {
data0.append("[{\"$ref\":\"..\"}]").append(",");
}
Expand All @@ -366,7 +366,7 @@ public static String buildJson(String typeName, String genericCanonicalName,
continue;
}
data0.append("[").append(buildJson(gicName, fieldGicName, isResp, nextLevel, registryClasses, groupClasses, builder))
.append("]").append(",");
.append("]").append(",");
} else {
data0.append("[{\"$ref\":\"..\"}]").append(",");
}
Expand Down Expand Up @@ -395,16 +395,16 @@ public static String buildJson(String typeName, String genericCanonicalName,
} else {
if (!typeName.equals(gicName1)) {
data0.append("{").append("\"mapKey\":")
.append(buildJson(DocClassUtil.getSimpleName(gicName1), gicName1, isResp, nextLevel, registryClasses,
groupClasses, builder)).append("},");
.append(buildJson(DocClassUtil.getSimpleName(gicName1), gicName1, isResp, nextLevel, registryClasses,
groupClasses, builder)).append("},");
} else {
data0.append("{\"mapKey\":{}},");
}
}
} else {
data0.append("{").append("\"mapKey\":")
.append(buildJson(gicName, fieldGicName, isResp, nextLevel, registryClasses, groupClasses, builder))
.append("},");
.append(buildJson(gicName, fieldGicName, isResp, nextLevel, registryClasses, groupClasses, builder))
.append("},");
}
} else if (fieldGicName.length() == 1) {
if (!typeName.equals(genericCanonicalName)) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/power/doc/helper/ParamsBuildHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static List<ApiParam> buildParams(String className, String pre, int level
fieldNameConvert = PropertyNameHelper.translate(clsAnnotation);
}
JavaClassUtil.genericParamMap(genericMap, cls, globGicName);
List<DocJavaField> fields = JavaClassUtil.getFields(cls, 0, new LinkedHashMap<>());
List<DocJavaField> fields = JavaClassUtil.getFields(cls, 0, new LinkedHashMap<>(),projectBuilder.getApiConfig().getClassLoader());
if (JavaClassValidateUtil.isPrimitive(simpleName)) {
String processedType = isShowJavaType ? simpleName : DocClassUtil.processTypeNameForParams(simpleName.toLowerCase());
paramList.addAll(primitiveReturnRespComment(processedType, atomicInteger, pid));
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/com/power/doc/utils/JavaClassUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public class JavaClassUtil {
* @param addedFields added fields,Field deduplication
* @return list of JavaField
*/
public static List<DocJavaField> getFields(JavaClass cls1, int counter, Map<String, DocJavaField> addedFields) {
public static List<DocJavaField> getFields(JavaClass cls1, int counter, Map<String, DocJavaField> addedFields, ClassLoader classLoader) {
Map<String, JavaType> actualJavaTypes = new HashMap<>(10);
List<DocJavaField> fields = getFields(cls1, counter, addedFields, actualJavaTypes);
List<DocJavaField> fields = getFields(cls1, counter, addedFields, actualJavaTypes, classLoader);
fields.stream().filter(f -> f.getGenericCanonicalName() != null)
.forEach(f -> actualJavaTypes.entrySet().stream()
.filter(e -> f.getGenericCanonicalName().equals(e.getKey()))
Expand All @@ -110,7 +110,7 @@ public static List<DocJavaField> getFields(JavaClass cls1, int counter, Map<Stri
* @return list of JavaField
*/
private static List<DocJavaField> getFields(JavaClass cls1, int counter, Map<String, DocJavaField> addedFields,
Map<String, JavaType> actualJavaTypes) {
Map<String, JavaType> actualJavaTypes, ClassLoader classLoader) {
List<DocJavaField> fieldList = new ArrayList<>();
if (Objects.isNull(cls1)) {
return fieldList;
Expand Down Expand Up @@ -168,13 +168,13 @@ private static List<DocJavaField> getFields(JavaClass cls1, int counter, Map<Str
if (!cls1.isEnum()) {
JavaClass parentClass = cls1.getSuperJavaClass();
if (Objects.nonNull(parentClass) && !"java.lang.Object".equals(parentClass.getName())) {
getFields(parentClass, counter, addedFields, actualJavaTypes);
getFields(parentClass, counter, addedFields, actualJavaTypes, classLoader);
}

List<JavaType> implClasses = cls1.getImplements();
for (JavaType type : implClasses) {
JavaClass javaClass = (JavaClass) type;
getFields(javaClass, counter, addedFields, actualJavaTypes);
getFields(javaClass, counter, addedFields, actualJavaTypes, classLoader);
}
}
actualJavaTypes.putAll(getActualTypesMap(cls1));
Expand Down Expand Up @@ -259,7 +259,7 @@ private static List<DocJavaField> getFields(JavaClass cls1, int counter, Map<Str
// Getting the Original Defined Type of Field
if (!docJavaField.isFile() || !docJavaField.isEnum() || !docJavaField.isPrimitive()
|| "java.lang.Object".equals(gicName)) {
String genericFieldTypeName = getFieldGenericType(javaField);
String genericFieldTypeName = getFieldGenericType(javaField,classLoader);
if (StringUtil.isNotEmpty(genericFieldTypeName)) {
gicName = genericFieldTypeName;
}
Expand Down Expand Up @@ -787,14 +787,19 @@ public static Map<String, String> getJsonIgnoresProp(JavaAnnotation annotation,
* @param javaField
* @return
*/
private static String getFieldGenericType(JavaField javaField) {
private static String getFieldGenericType(JavaField javaField, ClassLoader classLoader) {
if (JavaClassValidateUtil.isPrimitive(javaField.getType().getGenericCanonicalName())
|| (javaField.isFinal() && javaField.isPrivate())) {
return null;
}
String name = javaField.getName();
try {
Class c = Class.forName(javaField.getDeclaringClass().getCanonicalName());
Class c;
if (Objects.nonNull(classLoader)) {
c = classLoader.loadClass(javaField.getDeclaringClass().getCanonicalName());
} else {
c = Class.forName(javaField.getDeclaringClass().getCanonicalName());
}
Field f = c.getDeclaredField(name);
f.setAccessible(true);
Type t = f.getGenericType();
Expand Down

0 comments on commit ec6bee5

Please sign in to comment.