optimized code

This commit is contained in:
oppofind 2020-12-26 16:25:18 +08:00
parent 94f1bf83fd
commit dd9659b813
9 changed files with 57 additions and 29 deletions

View File

@ -13,6 +13,7 @@
8. 修改类中使用集合字段未指定泛型可能出错的bug。 8. 修改类中使用集合字段未指定泛型可能出错的bug。
9. 优化set等集合类在文档中的类型显示。 9. 优化set等集合类在文档中的类型显示。
10. 添加对集合字段中枚举的处理。 10. 添加对集合字段中枚举的处理。
11. 枚举序列化支持优化。
#### 版本号2.0.1 #### 版本号2.0.1
- 更新日期: 2020-12-20 - 更新日期: 2020-12-20
- 更新内容: - 更新内容:

View File

@ -52,7 +52,7 @@ public class JsonBuildHelper {
public static String buildReturnJson(DocJavaMethod docJavaMethod, ProjectDocConfigBuilder builder) { public static String buildReturnJson(DocJavaMethod docJavaMethod, ProjectDocConfigBuilder builder) {
JavaMethod method = docJavaMethod.getJavaMethod(); JavaMethod method = docJavaMethod.getJavaMethod();
if (method.getReturns().isVoid()) { if (method.getReturns().isVoid()) {
return "void"; return "Doesn't return a value.";
} }
String returnTypeGenericCanonicalName = method.getReturnType().getGenericCanonicalName(); String returnTypeGenericCanonicalName = method.getReturnType().getGenericCanonicalName();
if (Objects.nonNull(builder.getApiConfig().getResponseBodyAdvice()) if (Objects.nonNull(builder.getApiConfig().getResponseBodyAdvice())

View File

@ -343,13 +343,14 @@ public class ParamsBuildHelper {
if (gName.length() == 1) { if (gName.length() == 1) {
// handle generic // handle generic
int len = globGicName.length; int len = globGicName.length;
if (len > 0) { if (len < 1) {
continue out;
}
String gicName = genericMap.get(gName) != null ? genericMap.get(gName) : globGicName[0]; String gicName = genericMap.get(gName) != null ? genericMap.get(gName) : globGicName[0];
if (!JavaClassValidateUtil.isPrimitive(gicName) && !simpleName.equals(gicName)) { if (!JavaClassValidateUtil.isPrimitive(gicName) && !simpleName.equals(gicName)) {
paramList.addAll(buildParams(gicName, preBuilder.toString(), nextLevel, isRequired, paramList.addAll(buildParams(gicName, preBuilder.toString(), nextLevel, isRequired,
responseFieldMap, isResp, registryClasses, projectBuilder, groupClasses, fieldPid)); responseFieldMap, isResp, registryClasses, projectBuilder, groupClasses, fieldPid));
} }
}
} else { } else {
paramList.addAll(buildParams(gName, preBuilder.toString(), nextLevel, isRequired, paramList.addAll(buildParams(gName, preBuilder.toString(), nextLevel, isRequired,
responseFieldMap, isResp, registryClasses, projectBuilder, groupClasses, fieldPid)); responseFieldMap, isResp, registryClasses, projectBuilder, groupClasses, fieldPid));
@ -412,7 +413,7 @@ public class ParamsBuildHelper {
} }
} }
} }
} }//end field
} }
return paramList; return paramList;
} }
@ -454,7 +455,7 @@ public class ParamsBuildHelper {
String enumComments = javaClass.getComment(); String enumComments = javaClass.getComment();
if (projectBuilder.getApiConfig().getInlineEnum()) { if (projectBuilder.getApiConfig().getInlineEnum()) {
ApiDataDictionary dataDictionary = projectBuilder.getApiConfig().getDataDictionary(javaClass.getSimpleName()); ApiDataDictionary dataDictionary = projectBuilder.getApiConfig().getDataDictionary(javaClass.getSimpleName());
if (dataDictionary == null) { if (Objects.isNull(dataDictionary)) {
comment = comment + "<br/>" + JavaClassUtil.getEnumParams(javaClass); comment = comment + "<br/>" + JavaClassUtil.getEnumParams(javaClass);
} else { } else {
comment = comment + "[enum:" + dictionaryListComment(dataDictionary) + "]"; comment = comment + "[enum:" + dictionaryListComment(dataDictionary) + "]";
@ -462,7 +463,7 @@ public class ParamsBuildHelper {
} else { } else {
enumComments = DocUtil.replaceNewLineToHtmlBr(enumComments); enumComments = DocUtil.replaceNewLineToHtmlBr(enumComments);
comment = comment + "<br/>" + JavaClassUtil.getEnumParams(javaClass) + "<br/>"; comment = comment + "<br/>" + JavaClassUtil.getEnumParams(javaClass) + "<br/>";
if (enumComments != null) { if (StringUtil.isNotEmpty(enumComments)) {
comment = comment + "(See: " + enumComments + ")"; comment = comment + "(See: " + enumComments + ")";
} }
comment = StringUtil.removeQuotes(comment); comment = StringUtil.removeQuotes(comment);

View File

@ -95,7 +95,7 @@ public interface IDocBuildTemplate<T> {
default List<ApiParam> buildReturnApiParams(DocJavaMethod docJavaMethod, ProjectDocConfigBuilder projectBuilder) { default List<ApiParam> buildReturnApiParams(DocJavaMethod docJavaMethod, ProjectDocConfigBuilder projectBuilder) {
JavaMethod method = docJavaMethod.getJavaMethod(); JavaMethod method = docJavaMethod.getJavaMethod();
if (method.getReturns().isVoid()) { if (method.getReturns().isVoid()) {
return null; return new ArrayList<>(0);
} }
String returnTypeGenericCanonicalName = method.getReturnType().getGenericCanonicalName(); String returnTypeGenericCanonicalName = method.getReturnType().getGenericCanonicalName();
if (Objects.nonNull(projectBuilder.getApiConfig().getResponseBodyAdvice()) if (Objects.nonNull(projectBuilder.getApiConfig().getResponseBodyAdvice())
@ -117,33 +117,33 @@ public interface IDocBuildTemplate<T> {
String typeName = apiReturn.getSimpleName(); String typeName = apiReturn.getSimpleName();
if (this.ignoreReturnObject(typeName, projectBuilder.getApiConfig().getIgnoreRequestParams())) { if (this.ignoreReturnObject(typeName, projectBuilder.getApiConfig().getIgnoreRequestParams())) {
return null; return new ArrayList<>(0);
} }
if (JavaClassValidateUtil.isPrimitive(typeName)) { if (JavaClassValidateUtil.isPrimitive(typeName)) {
docJavaMethod.setReturnSchema(OpenApiSchemaUtil.primaryTypeSchema(typeName)); docJavaMethod.setReturnSchema(OpenApiSchemaUtil.primaryTypeSchema(typeName));
return null; return new ArrayList<>(0);
} }
if (JavaClassValidateUtil.isCollection(typeName)) { if (JavaClassValidateUtil.isCollection(typeName)) {
if (returnType.contains("<")) { if (returnType.contains("<")) {
String gicName = returnType.substring(returnType.indexOf("<") + 1, returnType.lastIndexOf(">")); String gicName = returnType.substring(returnType.indexOf("<") + 1, returnType.lastIndexOf(">"));
if (JavaClassValidateUtil.isPrimitive(gicName)) { if (JavaClassValidateUtil.isPrimitive(gicName)) {
docJavaMethod.setReturnSchema(OpenApiSchemaUtil.arrayTypeSchema(gicName)); docJavaMethod.setReturnSchema(OpenApiSchemaUtil.arrayTypeSchema(gicName));
return null; return new ArrayList<>(0);
} }
return ParamsBuildHelper.buildParams(gicName, "", 0, null, projectBuilder.getCustomRespFieldMap(), return ParamsBuildHelper.buildParams(gicName, "", 0, null, projectBuilder.getCustomRespFieldMap(),
Boolean.TRUE, new HashMap<>(), projectBuilder, null, 0); Boolean.TRUE, new HashMap<>(), projectBuilder, null, 0);
} else { } else {
return null; return new ArrayList<>(0);
} }
} }
if (JavaClassValidateUtil.isMap(typeName)) { if (JavaClassValidateUtil.isMap(typeName)) {
String[] keyValue = DocClassUtil.getMapKeyValueType(returnType); String[] keyValue = DocClassUtil.getMapKeyValueType(returnType);
if (keyValue.length == 0) { if (keyValue.length == 0) {
return null; return new ArrayList<>(0);
} }
if (JavaClassValidateUtil.isPrimitive(keyValue[1])) { if (JavaClassValidateUtil.isPrimitive(keyValue[1])) {
docJavaMethod.setReturnSchema(OpenApiSchemaUtil.mapTypeSchema(keyValue[1])); docJavaMethod.setReturnSchema(OpenApiSchemaUtil.mapTypeSchema(keyValue[1]));
return null; return new ArrayList<>(0);
} }
return ParamsBuildHelper.buildParams(keyValue[1], "", 0, null, projectBuilder.getCustomRespFieldMap(), return ParamsBuildHelper.buildParams(keyValue[1], "", 0, null, projectBuilder.getCustomRespFieldMap(),
Boolean.TRUE, new HashMap<>(), projectBuilder, null, 0); Boolean.TRUE, new HashMap<>(), projectBuilder, null, 0);
@ -152,7 +152,7 @@ public interface IDocBuildTemplate<T> {
return ParamsBuildHelper.buildParams(returnType, "", 0, null, projectBuilder.getCustomRespFieldMap(), return ParamsBuildHelper.buildParams(returnType, "", 0, null, projectBuilder.getCustomRespFieldMap(),
Boolean.TRUE, new HashMap<>(), projectBuilder, null, 0); Boolean.TRUE, new HashMap<>(), projectBuilder, null, 0);
} }
return null; return new ArrayList<>(0);
} }
List<T> getApiData(ProjectDocConfigBuilder projectBuilder); List<T> getApiData(ProjectDocConfigBuilder projectBuilder);

View File

@ -35,12 +35,12 @@ public class ApiParamTreeUtil {
public static List<ApiParam> apiParamToTree(List<ApiParam> apiParamList) { public static List<ApiParam> apiParamToTree(List<ApiParam> apiParamList) {
if (Objects.isNull(apiParamList)) { if (Objects.isNull(apiParamList)) {
return null; return new ArrayList<>(0);
} }
List<ApiParam> params = new ArrayList<>(); List<ApiParam> params = new ArrayList<>();
// find root // find root
for (ApiParam apiParam : apiParamList) { for (ApiParam apiParam : apiParamList) {
//去除filed的前缀 // remove pre of field
apiParam.setField(apiParam.getField().replaceAll("└─", "").replaceAll("&nbsp;", "")); apiParam.setField(apiParam.getField().replaceAll("└─", "").replaceAll("&nbsp;", ""));
// pid == 0 // pid == 0
if (apiParam.getPid() == 0) { if (apiParam.getPid() == 0) {
@ -48,7 +48,7 @@ public class ApiParamTreeUtil {
} }
} }
for (ApiParam apiParam : params) { for (ApiParam apiParam : params) {
//去除filed的前缀 // remove pre of field
apiParam.setChildren(getChild(apiParam.getId(), apiParamList)); apiParam.setChildren(getChild(apiParam.getId(), apiParamList));
} }
return params; return params;
@ -72,7 +72,7 @@ public class ApiParamTreeUtil {
param.setChildren(getChild(param.getId(), apiParamList)); param.setChildren(getChild(param.getId(), apiParamList));
} }
if (childList.size() == 0) { if (childList.size() == 0) {
return null; return new ArrayList<>(0);
} }
return childList; return childList;
} }

View File

@ -32,6 +32,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
/** /**
* Beetl template handle util * Beetl template handle util
@ -75,7 +76,7 @@ public class BeetlTemplateUtil {
if (f.isFile()) { if (f.isFile()) {
String fileName = f.getName(); String fileName = f.getName();
Template tp = gt.getTemplate(fileName); Template tp = gt.getTemplate(fileName);
if (null != params) { if (Objects.nonNull(params)) {
tp.binding(params); tp.binding(params);
} }
templateMap.put(fileName, tp.render()); templateMap.put(fileName, tp.render());
@ -95,7 +96,7 @@ public class BeetlTemplateUtil {
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg); GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
return gt; return gt;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Can't get Beetl template."); throw new RuntimeException("Can't found Beetl template.");
} }
} }
} }

View File

@ -1,3 +1,25 @@
/*
* smart-doc
*
* Copyright (C) 2018-2021 smart-doc
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.power.doc.utils; package com.power.doc.utils;
import com.power.common.util.CollectionUtil; import com.power.common.util.CollectionUtil;

View File

@ -21,6 +21,9 @@
* under the License. * under the License.
*/ */
/**
* @author daiww 2020/08/26.
*/
package com.power.doc.utils; package com.power.doc.utils;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
@ -28,7 +31,7 @@ import java.util.function.BiConsumer;
public class Iterables { public class Iterables {
public static <E> void forEach( public static <E> void forEach(
Iterable<? extends E> elements, BiConsumer<Integer, ? super E> action) { Iterable<? extends E> elements, BiConsumer<Integer, ? super E> action) {
if(elements==null||action==null) return; if (elements == null || action == null) return;
int index = 0; int index = 0;
for (E element : elements) { for (E element : elements) {
action.accept(index++, element); action.accept(index++, element);

View File

@ -57,12 +57,12 @@ public class JavaClassUtil {
*/ */
public static List<DocJavaField> getFields(JavaClass cls1, int counter, Set<String> addedFields) { public static List<DocJavaField> getFields(JavaClass cls1, int counter, Set<String> addedFields) {
List<DocJavaField> fieldList = new ArrayList<>(); List<DocJavaField> fieldList = new ArrayList<>();
if (null == cls1) { if (Objects.isNull(cls1)) {
return fieldList; return fieldList;
} else if ("Object".equals(cls1.getSimpleName()) || "Timestamp".equals(cls1.getSimpleName()) || } else if ("Object".equals(cls1.getSimpleName()) || "Timestamp".equals(cls1.getSimpleName()) ||
"Date".equals(cls1.getSimpleName()) || "Locale".equals(cls1.getSimpleName()) "Date".equals(cls1.getSimpleName()) || "Locale".equals(cls1.getSimpleName())
|| "ClassLoader".equals(cls1.getSimpleName()) || JavaClassValidateUtil.isMap(cls1.getFullyQualifiedName()) || "ClassLoader".equals(cls1.getSimpleName()) || JavaClassValidateUtil.isMap(cls1.getFullyQualifiedName())
|| cls1.isEnum()) { || cls1.isEnum() || "Serializable".equals(cls1.getSimpleName())) {
return fieldList; return fieldList;
} else { } else {
String className = cls1.getFullyQualifiedName(); String className = cls1.getFullyQualifiedName();
@ -285,7 +285,7 @@ public class JavaClassUtil {
* @return JavaClass * @return JavaClass
*/ */
public static List<JavaType> getActualTypes(JavaType javaType) { public static List<JavaType> getActualTypes(JavaType javaType) {
if (null == javaType) { if (Objects.isNull(javaType)) {
return new ArrayList<>(0); return new ArrayList<>(0);
} }
String typeName = javaType.getGenericFullyQualifiedName(); String typeName = javaType.getGenericFullyQualifiedName();
@ -454,7 +454,7 @@ public class JavaClassUtil {
} }
public static void genericParamMap(Map<String, String> genericMap, JavaClass cls, String[] globGicName) { public static void genericParamMap(Map<String, String> genericMap, JavaClass cls, String[] globGicName) {
if (cls != null && null != cls.getTypeParameters()) { if (Objects.nonNull(cls) && Objects.nonNull(cls.getTypeParameters())) {
List<JavaTypeVariable<JavaGenericDeclaration>> variables = cls.getTypeParameters(); List<JavaTypeVariable<JavaGenericDeclaration>> variables = cls.getTypeParameters();
for (int i = 0; i < cls.getTypeParameters().size() && i < globGicName.length; i++) { for (int i = 0; i < cls.getTypeParameters().size() && i < globGicName.length; i++) {
genericMap.put(variables.get(i).getName(), globGicName[i]); genericMap.put(variables.get(i).getName(), globGicName[i]);