optimized code and upgrade common-util to 1.8.5

This commit is contained in:
oppofind 2019-09-19 21:02:32 +08:00
parent c0836d514f
commit f636457ef3
5 changed files with 63 additions and 73 deletions

View File

@ -58,7 +58,7 @@
<dependency> <dependency>
<groupId>com.github.shalousun</groupId> <groupId>com.github.shalousun</groupId>
<artifactId>common-util</artifactId> <artifactId>common-util</artifactId>
<version>1.8.4</version> <version>1.8.5</version>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -3,6 +3,7 @@ package com.power.doc.builder;
import com.power.common.util.CollectionUtil; import com.power.common.util.CollectionUtil;
import com.power.common.util.JsonFormatUtil; import com.power.common.util.JsonFormatUtil;
import com.power.common.util.StringUtil; import com.power.common.util.StringUtil;
import com.power.common.util.UrlUtil;
import com.power.doc.constants.AnnotationConstants; import com.power.doc.constants.AnnotationConstants;
import com.power.doc.constants.DocTags; import com.power.doc.constants.DocTags;
import com.power.doc.constants.GlobalConstants; import com.power.doc.constants.GlobalConstants;
@ -227,58 +228,28 @@ public class SourceBuilder {
for (JavaAnnotation annotation : annotations) { for (JavaAnnotation annotation : annotations) {
String annotationName = annotation.getType().getName(); String annotationName = annotation.getType().getName();
if (REQUEST_MAPPING.equals(annotationName) || GlobalConstants.REQUEST_MAPPING_FULLY.equals(annotationName)) { if (REQUEST_MAPPING.equals(annotationName) || GlobalConstants.REQUEST_MAPPING_FULLY.equals(annotationName)) {
if (null == annotation.getNamedParameter("value")) { url = DocUtil.handleMappingValue(annotation);
url = "/";
} else {
url = annotation.getNamedParameter("value").toString();
}
if (null != annotation.getNamedParameter("method")) { if (null != annotation.getNamedParameter("method")) {
methodType = annotation.getNamedParameter("method").toString(); methodType = annotation.getNamedParameter("method").toString();
if ("RequestMethod.POST".equals(methodType)) { methodType = DocUtil.handleHttpMethod(methodType);
methodType = "POST";
} else if ("RequestMethod.GET".equals(methodType)) {
methodType = "GET";
} else if ("RequestMethod.PUT".equals(methodType)) {
methodType = "PUT";
} else if ("RequestMethod.DELETE".equals(methodType)) {
methodType = "DELETE";
} else {
methodType = "GET";
}
} else { } else {
methodType = "GET"; methodType = "GET";
} }
methodCounter++; methodCounter++;
} else if (GET_MAPPING.equals(annotationName) || GlobalConstants.GET_MAPPING_FULLY.equals(annotationName)) { } else if (GET_MAPPING.equals(annotationName) || GlobalConstants.GET_MAPPING_FULLY.equals(annotationName)) {
if (null == annotation.getNamedParameter("value")) { url = DocUtil.handleMappingValue(annotation);
url = "/";
} else {
url = annotation.getNamedParameter("value").toString();
}
methodType = "GET"; methodType = "GET";
methodCounter++; methodCounter++;
} else if (POST_MAPPING.equals(annotationName) || GlobalConstants.POST_MAPPING_FULLY.equals(annotationName)) { } else if (POST_MAPPING.equals(annotationName) || GlobalConstants.POST_MAPPING_FULLY.equals(annotationName)) {
if (null == annotation.getNamedParameter("value")) { url = DocUtil.handleMappingValue(annotation);
url = "/";
} else {
url = annotation.getNamedParameter("value").toString();
}
methodType = "POST"; methodType = "POST";
methodCounter++; methodCounter++;
} else if (PUT_MAPPING.equals(annotationName) || GlobalConstants.PUT_MAPPING_FULLY.equals(annotationName)) { } else if (PUT_MAPPING.equals(annotationName) || GlobalConstants.PUT_MAPPING_FULLY.equals(annotationName)) {
if (null == annotation.getNamedParameter("value")) { url = DocUtil.handleMappingValue(annotation);
url = "/";
} else {
url = annotation.getNamedParameter("value").toString();
}
methodType = "PUT"; methodType = "PUT";
methodCounter++; methodCounter++;
} else if (DELETE_MAPPING.equals(annotationName) || GlobalConstants.DELETE_MAPPING_FULLY.equals(annotationName)) { } else if (DELETE_MAPPING.equals(annotationName) || GlobalConstants.DELETE_MAPPING_FULLY.equals(annotationName)) {
if (null == annotation.getNamedParameter("value")) { url = DocUtil.handleMappingValue(annotation);
url = "/";
} else {
url = annotation.getNamedParameter("value").toString();
}
methodType = "DELETE"; methodType = "DELETE";
methodCounter++; methodCounter++;
} }
@ -456,9 +427,9 @@ public class SourceBuilder {
an: an:
for (JavaAnnotation annotation : javaAnnotations) { for (JavaAnnotation annotation : javaAnnotations) {
String annotationName = annotation.getType().getSimpleName(); String annotationName = annotation.getType().getSimpleName();
if ("JsonIgnore".equals(annotationName) && isResp) { if (AnnotationConstants.SHORT_JSON_IGNORE.equals(annotationName) && isResp) {
continue out; continue out;
} else if ("JSONField".equals(annotationName) && isResp) { } else if (AnnotationConstants.SHORT_JSON_FIELD.equals(annotationName) && isResp) {
if (null != annotation.getProperty("serialize")) { if (null != annotation.getProperty("serialize")) {
if ("false".equals(annotation.getProperty("serialize").toString())) { if ("false".equals(annotation.getProperty("serialize").toString())) {
continue out; continue out;
@ -466,7 +437,7 @@ public class SourceBuilder {
} else if (null != annotation.getProperty("name")) { } else if (null != annotation.getProperty("name")) {
fieldName = annotation.getProperty("name").toString().replace("\"", ""); fieldName = annotation.getProperty("name").toString().replace("\"", "");
} }
} else if ("JsonProperty".equals(annotationName) && isResp) { } else if (AnnotationConstants.SHORT_JSON_PROPERTY.equals(annotationName) && isResp) {
if (null != annotation.getProperty("value")) { if (null != annotation.getProperty("value")) {
fieldName = annotation.getProperty("value").toString().replace("\"", ""); fieldName = annotation.getProperty("value").toString().replace("\"", "");
} }
@ -543,7 +514,7 @@ public class SourceBuilder {
preBuilder.append("└─"); preBuilder.append("└─");
if (DocClassUtil.isMap(subTypeName)) { if (DocClassUtil.isMap(subTypeName)) {
String gNameTemp = field.getType().getGenericCanonicalName(); String gNameTemp = field.getType().getGenericCanonicalName();
if ("java.util.Map".equals(gNameTemp)) { if (GlobalConstants.JAVA_MAP_FULLY.equals(gNameTemp)) {
params0.append(preBuilder + "any object|object|any object\n"); params0.append(preBuilder + "any object|object|any object\n");
continue; continue;
} }
@ -580,7 +551,7 @@ public class SourceBuilder {
} }
} }
} }
} else if (subTypeName.length() == 1 || "java.lang.Object".equals(subTypeName)) { } else if (subTypeName.length() == 1 || GlobalConstants.JAVA_OBJECT_FULLY.equals(subTypeName)) {
if (!simpleName.equals(className)) { if (!simpleName.equals(className)) {
if (n < globGicName.length) { if (n < globGicName.length) {
String gicName = globGicName[n]; String gicName = globGicName[n];
@ -711,7 +682,7 @@ public class SourceBuilder {
data.append("{\"mapKey\":{}}"); data.append("{\"mapKey\":{}}");
return data.toString(); return data.toString();
} }
if (!"java.lang.String".equals(getKeyValType[0])) { if (!GlobalConstants.JAVA_STRING_FULLY.equals(getKeyValType[0])) {
throw new RuntimeException("Map's key can only use String for json,but you use " + getKeyValType[0]); throw new RuntimeException("Map's key can only use String for json,but you use " + getKeyValType[0]);
} }
String gicName = gNameTemp.substring(gNameTemp.indexOf(",") + 1, gNameTemp.lastIndexOf(">")); String gicName = gNameTemp.substring(gNameTemp.indexOf(",") + 1, gNameTemp.lastIndexOf(">"));
@ -752,9 +723,9 @@ public class SourceBuilder {
List<JavaAnnotation> annotations = field.getAnnotations(); List<JavaAnnotation> annotations = field.getAnnotations();
for (JavaAnnotation annotation : annotations) { for (JavaAnnotation annotation : annotations) {
String annotationName = annotation.getType().getSimpleName(); String annotationName = annotation.getType().getSimpleName();
if ("JsonIgnore".equals(annotationName) && isResp) { if (AnnotationConstants.SHORT_JSON_IGNORE.equals(annotationName) && isResp) {
continue out; continue out;
} else if ("JSONField".equals(annotationName) && isResp) { } else if (AnnotationConstants.SHORT_JSON_FIELD.equals(annotationName) && isResp) {
if (null != annotation.getProperty("serialize")) { if (null != annotation.getProperty("serialize")) {
if ("false".equals(annotation.getProperty("serialize").toString())) { if ("false".equals(annotation.getProperty("serialize").toString())) {
continue out; continue out;
@ -762,7 +733,7 @@ public class SourceBuilder {
} else if (null != annotation.getProperty("name")) { } else if (null != annotation.getProperty("name")) {
fieldName = annotation.getProperty("name").toString().replace("\"", ""); fieldName = annotation.getProperty("name").toString().replace("\"", "");
} }
} else if ("JsonProperty".equals(annotationName) && isResp) { } else if (AnnotationConstants.SHORT_JSON_PROPERTY.equals(annotationName) && isResp) {
if (null != annotation.getProperty("value")) { if (null != annotation.getProperty("value")) {
fieldName = annotation.getProperty("value").toString().replace("\"", ""); fieldName = annotation.getProperty("value").toString().replace("\"", "");
} }
@ -797,9 +768,9 @@ public class SourceBuilder {
} }
String gicName = DocClassUtil.getSimpleGicName(fieldGicName)[0]; String gicName = DocClassUtil.getSimpleGicName(fieldGicName)[0];
if ("java.lang.String".equals(gicName)) { if (GlobalConstants.JAVA_STRING_FULLY.equals(gicName)) {
data0.append("[").append("\"").append(buildJson(gicName, fieldGicName, responseFieldMap, isResp)).append("\"]").append(","); data0.append("[").append("\"").append(buildJson(gicName, fieldGicName, responseFieldMap, isResp)).append("\"]").append(",");
} else if ("java.util.List".equals(gicName)) { } else if (GlobalConstants.JAVA_LIST_FULLY.equals(gicName)) {
data0.append("{\"object\":\"any object\"},"); data0.append("{\"object\":\"any object\"},");
} else if (gicName.length() == 1) { } else if (gicName.length() == 1) {
if (globGicName.length == 0) { if (globGicName.length == 0) {
@ -807,7 +778,7 @@ public class SourceBuilder {
continue out; continue out;
} }
String gicName1 = (i < globGicName.length) ? globGicName[i] : globGicName[globGicName.length - 1]; String gicName1 = (i < globGicName.length) ? globGicName[i] : globGicName[globGicName.length - 1];
if ("java.lang.String".equals(gicName1)) { if (GlobalConstants.JAVA_STRING_FULLY.equals(gicName1)) {
data0.append("[").append("\"").append(buildJson(gicName1, gicName1, responseFieldMap, isResp)).append("\"]").append(","); data0.append("[").append("\"").append(buildJson(gicName1, gicName1, responseFieldMap, isResp)).append("\"]").append(",");
} else { } else {
if (!typeName.equals(gicName1)) { if (!typeName.equals(gicName1)) {
@ -829,14 +800,14 @@ public class SourceBuilder {
} }
} }
} else if (DocClassUtil.isMap(subTypeName)) { } else if (DocClassUtil.isMap(subTypeName)) {
if ("java.util.Map".equals(subTypeName)) { if (GlobalConstants.JAVA_MAP_FULLY.equals(subTypeName)) {
data0.append("{").append("\"mapKey\":{}},"); data0.append("{").append("\"mapKey\":{}},");
continue out; continue out;
} }
String gicName = fieldGicName.substring(fieldGicName.indexOf(",") + 1, fieldGicName.indexOf(">")); String gicName = fieldGicName.substring(fieldGicName.indexOf(",") + 1, fieldGicName.indexOf(">"));
if (gicName.length() == 1) { if (gicName.length() == 1) {
String gicName1 = (i < globGicName.length) ? globGicName[i] : globGicName[globGicName.length - 1]; String gicName1 = (i < globGicName.length) ? globGicName[i] : globGicName[globGicName.length - 1];
if ("java.lang.String".equals(gicName1)) { if (GlobalConstants.JAVA_STRING_FULLY.equals(gicName1)) {
data0.append("{").append("\"mapKey\":\"").append(buildJson(gicName1, gicName1, responseFieldMap, isResp)).append("\"},"); data0.append("{").append("\"mapKey\":\"").append(buildJson(gicName1, gicName1, responseFieldMap, isResp)).append("\"},");
} else { } else {
if (!typeName.equals(gicName1)) { if (!typeName.equals(gicName1)) {
@ -945,9 +916,9 @@ public class SourceBuilder {
String url; String url;
if (containsBrace) { if (containsBrace) {
url = DocUtil.formatAndRemove(apiMethodDoc.getUrl(), paramsMap); url = DocUtil.formatAndRemove(apiMethodDoc.getUrl(), paramsMap);
url = DocUtil.urlJoin(url, paramsMap); url = UrlUtil.urlJoin(url, paramsMap);
} else { } else {
url = DocUtil.urlJoin(apiMethodDoc.getUrl(), paramsMap); url = UrlUtil.urlJoin(apiMethodDoc.getUrl(), paramsMap);
} }
return url; return url;
} }
@ -1033,7 +1004,7 @@ public class SourceBuilder {
reqParam.append(paramName).append("|") reqParam.append(paramName).append("|")
.append(DocClassUtil.processTypeNameForParams(simpleName)).append("|") .append(DocClassUtil.processTypeNameForParams(simpleName)).append("|")
.append(comment).append("|true\n"); .append(comment).append("|true\n");
} else if ("java.util.Map".equals(typeName)) { } else if (GlobalConstants.JAVA_MAP_FULLY.equals(typeName)) {
reqParam.append(paramName).append("|") reqParam.append(paramName).append("|")
.append("map").append("|") .append("map").append("|")
.append(comment).append("|true\n"); .append(comment).append("|true\n");
@ -1074,7 +1045,7 @@ public class SourceBuilder {
} }
} else if (DocClassUtil.isMap(fullTypeName)) { } else if (DocClassUtil.isMap(fullTypeName)) {
if ("java.util.Map".equals(typeName)) { if (GlobalConstants.JAVA_MAP_FULLY.equals(typeName)) {
reqParam.append(paramName).append("|") reqParam.append(paramName).append("|")
.append("map").append("|") .append("map").append("|")
.append(comment).append("|").append(required).append("\n"); .append(comment).append("|").append(required).append("\n");

View File

@ -10,7 +10,7 @@ public class AnnotationConstants {
public static final String SHORT_REST_CONTROLLER = "RestController"; public static final String SHORT_REST_CONTROLLER = "RestController";
public static final String SHORT_JSON_IGNORE= "Ignore"; public static final String SHORT_JSON_IGNORE= "JsonIgnore";
public static final String SHORT_JSON_PROPERTY = "JsonProperty"; public static final String SHORT_JSON_PROPERTY = "JsonProperty";

View File

@ -33,6 +33,12 @@ public class GlobalConstants {
*/ */
public static final String JAVA_OBJECT_FULLY = "java.lang.Object"; public static final String JAVA_OBJECT_FULLY = "java.lang.Object";
public static final String JAVA_STRING_FULLY = "java.lang.String";
public static final String JAVA_MAP_FULLY = "java.util.Map";
public static final String JAVA_LIST_FULLY = "java.util.List";
} }

View File

@ -5,6 +5,7 @@ import com.power.common.util.DateTimeUtil;
import com.power.common.util.IDCardUtil; import com.power.common.util.IDCardUtil;
import com.power.common.util.RandomUtil; import com.power.common.util.RandomUtil;
import com.power.common.util.StringUtil; import com.power.common.util.StringUtil;
import com.thoughtworks.qdox.model.JavaAnnotation;
import java.util.*; import java.util.*;
@ -217,24 +218,36 @@ public class DocUtil {
return builder.toString(); return builder.toString();
} }
public static String urlJoin(String url, Map<String, String> params) { /**
StringBuilder endUrl = new StringBuilder(url); * handle spring mvc method
if (null == params) { * @param method
return url; * @return
*/
public static String handleHttpMethod(String method) {
switch (method) {
case "RequestMethod.POST":
return "POST";
case "RequestMethod.GET":
return "GET";
case "RequestMethod.PUT":
return "PUT";
case "RequestMethod.DELETE":
return "DELETE";
default:
return "GET";
} }
boolean isFirst = true; }
Set<Map.Entry<String, String>> entrySet = params.entrySet();
for (Map.Entry<String, String> entry : entrySet) { /**
if (isFirst && !url.contains("?")) { * handle spring mvc mapping value
isFirst = false; * @param annotation
endUrl.append("?"); * @return
} else { */
endUrl.append("&"); public static String handleMappingValue(JavaAnnotation annotation){
} if (null == annotation.getNamedParameter("value")) {
endUrl.append(entry.getKey()); return "/";
endUrl.append("="); } else {
endUrl.append(entry.getValue()); return annotation.getNamedParameter("value").toString();
} }
return endUrl.toString();
} }
} }