From a6b435e7f54e4e21453f06064fc382dfa4c5f95b Mon Sep 17 00:00:00 2001 From: songhaozhi <1150837340@qq.com> Date: Fri, 20 Mar 2020 23:30:57 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9Agitee=20#I1ADFQ?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../template/SpringBootDocBuildTemplate.java | 18 +- .../java/com/power/doc/utils/DocUtil.java | 4 +- .../com/power/doc/utils/JavaClassUtil.java | 525 +++++++++--------- 3 files changed, 291 insertions(+), 256 deletions(-) diff --git a/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java b/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java index a65f1ad..69f28be 100644 --- a/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java +++ b/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java @@ -99,6 +99,7 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate { private List buildControllerMethod(final JavaClass cls, ApiConfig apiConfig, ProjectDocConfigBuilder projectBuilder) { String clazName = cls.getCanonicalName(); + String classAuthor = JavaClassUtil.getClassTagsValue(cls, DocTags.AUTHOR); List classAnnotations = cls.getAnnotations(); String baseUrl = ""; for (JavaAnnotation annotation : classAnnotations) { @@ -130,10 +131,14 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate { if (StringUtil.isEmpty(apiNoteValue)) { apiNoteValue = method.getComment(); } - String authorValue = DocUtil.getNormalTagComments(method, DocTags.AUTHOR, cls.getName()); + Map authorMap = DocUtil.getParamsComments(method, DocTags.AUTHOR, cls.getName()); + String authorValue = String.join(", ", new ArrayList<>(authorMap.keySet())); if (apiConfig.isShowAuthor() && StringUtil.isNotEmpty(authorValue)) { apiMethodDoc.setAuthor(authorValue); } + if (apiConfig.isShowAuthor() && StringUtil.isEmpty(authorValue)) { + apiMethodDoc.setAuthor(classAuthor); + } apiMethodDoc.setDetail(apiNoteValue); //handle request mapping RequestMapping requestMapping = new SpringMVCRequestMappingHandler() @@ -192,7 +197,8 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate { List springMvcRequestAnnotations = SpringMvcRequestAnnotationsEnum.listSpringMvcRequestAnnotations(); List formDataList = new ArrayList<>(); ApiRequestExample requestExample = ApiRequestExample.builder(); - out:for (JavaParameter parameter : parameterList) { + out: + for (JavaParameter parameter : parameterList) { JavaType javaType = parameter.getType(); String typeName = javaType.getFullyQualifiedName(); if (JavaClassValidateUtil.isMvcIgnoreParams(typeName)) { @@ -436,14 +442,14 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate { } if (JavaClassValidateUtil.isPrimitive(gicName)) { String shortSimple = DocClassUtil.processTypeNameForParams(gicName); - ApiParam param = ApiParam.of().setField(paramName).setDesc(comment+",[array of "+shortSimple+"]" ) + ApiParam param = ApiParam.of().setField(paramName).setDesc(comment + ",[array of " + shortSimple + "]") .setRequired(required) .setType("array"); paramList.add(param); } else { if (requestBodyCounter > 0) { //for json - paramList.addAll(ParamsBuildHelper.buildParams(gicNameArr[0], DocGlobalConstants.ENMPTY, 0, "true", responseFieldMap, Boolean.FALSE, new HashMap<>(), builder,groupClasses)); + paramList.addAll(ParamsBuildHelper.buildParams(gicNameArr[0], DocGlobalConstants.ENMPTY, 0, "true", responseFieldMap, Boolean.FALSE, new HashMap<>(), builder, groupClasses)); } else { throw new RuntimeException("Spring MVC can't support binding Collection on method " + javaMethod.getName() + "Check it in " + javaMethod.getDeclaringClass().getCanonicalName()); @@ -462,13 +468,13 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate { continue out; } String[] gicNameArr = DocClassUtil.getSimpleGicName(typeName); - paramList.addAll(ParamsBuildHelper.buildParams(gicNameArr[1], DocGlobalConstants.ENMPTY, 0, "true", responseFieldMap, Boolean.FALSE, new HashMap<>(), builder,groupClasses)); + paramList.addAll(ParamsBuildHelper.buildParams(gicNameArr[1], DocGlobalConstants.ENMPTY, 0, "true", responseFieldMap, Boolean.FALSE, new HashMap<>(), builder, groupClasses)); } else if (javaClass.isEnum()) { ApiParam param = ApiParam.of().setField(paramName) .setType("string").setDesc(comment).setRequired(required).setVersion(DocGlobalConstants.DEFAULT_VERSION); paramList.add(param); } else { - paramList.addAll(ParamsBuildHelper.buildParams(fullTypeName, DocGlobalConstants.ENMPTY, 0, "true", responseFieldMap, Boolean.FALSE, new HashMap<>(), builder,groupClasses)); + paramList.addAll(ParamsBuildHelper.buildParams(fullTypeName, DocGlobalConstants.ENMPTY, 0, "true", responseFieldMap, Boolean.FALSE, new HashMap<>(), builder, groupClasses)); } } return paramList; diff --git a/src/main/java/com/power/doc/utils/DocUtil.java b/src/main/java/com/power/doc/utils/DocUtil.java index a8643f6..e147aeb 100644 --- a/src/main/java/com/power/doc/utils/DocUtil.java +++ b/src/main/java/com/power/doc/utils/DocUtil.java @@ -327,8 +327,8 @@ public class DocUtil { pName = value.substring(0, idx); pValue = value.substring(idx + 1); } else { - pName = (value.indexOf(" ") > -1) ? value.substring(0, value.indexOf(" ")) : value; - pValue = value.indexOf(" ") > -1 ? value.substring(value.indexOf(' ') + 1) : DocGlobalConstants.NO_COMMENTS_FOUND; + pName = (value.contains(" ")) ? value.substring(0, value.indexOf(" ")) : value; + pValue = value.contains(" ") ? value.substring(value.indexOf(' ') + 1) : DocGlobalConstants.NO_COMMENTS_FOUND; } paramTagMap.put(pName, pValue); } diff --git a/src/main/java/com/power/doc/utils/JavaClassUtil.java b/src/main/java/com/power/doc/utils/JavaClassUtil.java index 91edcbf..7d07f6a 100644 --- a/src/main/java/com/power/doc/utils/JavaClassUtil.java +++ b/src/main/java/com/power/doc/utils/JavaClassUtil.java @@ -1,248 +1,277 @@ -/* - * smart-doc - * - * Copyright (C) 2019-2020 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; - -import com.power.common.util.CollectionUtil; -import com.power.common.util.StringUtil; -import com.power.doc.constants.DocAnnotationConstants; -import com.power.doc.constants.DocValidatorAnnotationEnum; -import com.power.doc.constants.ValidatorAnnotations; -import com.power.doc.model.DocJavaField; -import com.thoughtworks.qdox.model.*; -import com.thoughtworks.qdox.model.expression.AnnotationValue; -import com.thoughtworks.qdox.model.expression.AnnotationValueList; -import com.thoughtworks.qdox.model.expression.TypeRef; -import com.thoughtworks.qdox.model.impl.DefaultJavaField; -import com.thoughtworks.qdox.model.impl.DefaultJavaParameterizedType; - -import javax.print.Doc; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -/** - * Handle JavaClass - * - * @author yu 2019/12/21. - */ -public class JavaClassUtil { - - /** - * Get fields - * - * @param cls1 The JavaClass object - * @param i Recursive counter - * @return list of JavaField - */ - public static List getFields(JavaClass cls1, int i) { - List fieldList = new ArrayList<>(); - if (null == cls1) { - return fieldList; - } else if ("Object".equals(cls1.getSimpleName()) || "Timestamp".equals(cls1.getSimpleName()) || - "Date".equals(cls1.getSimpleName()) || "Locale".equals(cls1.getSimpleName())) { - return fieldList; - } else { - String className = cls1.getFullyQualifiedName(); - if (cls1.isInterface() && - !JavaClassValidateUtil.isCollection(className) && - !JavaClassValidateUtil.isMap(className)) { - List methods = cls1.getMethods(); - for (JavaMethod javaMethod : methods) { - String methodName = javaMethod.getName(); - if (!methodName.startsWith("get")) { - continue; - } - methodName = StringUtil.firstToLowerCase(methodName.substring(3, methodName.length())); - String comment = javaMethod.getComment(); - JavaField javaField = new DefaultJavaField(javaMethod.getReturns(), methodName); - DocJavaField docJavaField = DocJavaField.builder().setJavaField(javaField).setComment(comment); - fieldList.add(docJavaField); - } - } - JavaClass pcls = cls1.getSuperJavaClass(); - fieldList.addAll(getFields(pcls, i)); - List docJavaFields = new ArrayList<>(); - for(JavaField javaField:cls1.getFields()){ - docJavaFields.add(DocJavaField.builder().setComment(javaField.getComment()).setJavaField(javaField)); - } - fieldList.addAll(docJavaFields); - } - return fieldList; - } - - - /** - * get enum value - * - * @param javaClass enum class - * @param formDataEnum is return method - * @return Object - */ - public static Object getEnumValue(JavaClass javaClass, boolean formDataEnum) { - List javaFields = javaClass.getEnumConstants(); - List methodList = javaClass.getMethods(); - int annotation = 0; - for (JavaMethod method : methodList) { - if (method.getAnnotations().size() > 0) { - annotation = 1; - break; - } - } - Object value = null; - int index = 0; - for (JavaField javaField : javaFields) { - String simpleName = javaField.getType().getSimpleName(); - StringBuilder valueBuilder = new StringBuilder(); - valueBuilder.append("\"").append(javaField.getName()).append("\"").toString(); - if (formDataEnum) { - value = valueBuilder.toString(); - return value; - } - if (!JavaClassValidateUtil.isPrimitive(simpleName) && index < 1) { - if (null != javaField.getEnumConstantArguments() && annotation > 0) { - value = javaField.getEnumConstantArguments().get(0); - } else { - value = valueBuilder.toString(); - } - } - index++; - } - return value; - } - - - /** - * Get annotation simpleName - * - * @param annotationName annotationName - * @return String - */ - public static String getAnnotationSimpleName(String annotationName) { - return getClassSimpleName(annotationName); - } - - /** - * Get className - * - * @param className className - * @return String - */ - public static String getClassSimpleName(String className) { - if (className.contains(".")) { - int index = className.lastIndexOf("."); - className = className.substring(index + 1, className.length()); - } - return className; - } - - /** - * get Actual type - * - * @param javaClass JavaClass - * @return JavaClass - */ - public static JavaClass getActualType(JavaClass javaClass) { - return getActualTypes(javaClass).get(0); - } - - /** - * get Actual type list - * - * @param javaClass JavaClass - * @return JavaClass - */ - public static List getActualTypes(JavaClass javaClass) { - if (null == javaClass) { - return new ArrayList<>(0); - } - List javaClassList = new ArrayList<>(); - List actualTypes = ((DefaultJavaParameterizedType) javaClass).getActualTypeArguments(); - actualTypes.forEach(javaType -> { - JavaClass actualClass = (JavaClass) javaType; - javaClassList.add(actualClass); - }); - return javaClassList; - } - - /** - * Obtain Validate Group classes - * - * @param annotations the annotations of controller method param - * @return the group annotation value - */ - public static List getParamGroupJavaClass(List annotations) { - if (CollectionUtil.isEmpty(annotations)) { - return new ArrayList<>(0); - } - List javaClassList = new ArrayList<>(); - List validates = DocValidatorAnnotationEnum.listValidatorAnnotations(); - for (JavaAnnotation javaAnnotation : annotations) { - List annotationValueList = getAnnotationValues(validates,javaAnnotation); - addGroupClass(annotationValueList, javaClassList); - } - return javaClassList; - } - - /** - * Obtain Validate Group classes - * @param javaAnnotation the annotation of controller method param - * @return the group annotation value - */ - public static List getParamGroupJavaClass(JavaAnnotation javaAnnotation) { - if (Objects.isNull(javaAnnotation)) { - return new ArrayList<>(0); - } - List javaClassList = new ArrayList<>(); - List validates = DocValidatorAnnotationEnum.listValidatorAnnotations(); - List annotationValueList = getAnnotationValues(validates,javaAnnotation); - addGroupClass(annotationValueList, javaClassList); - return javaClassList; - } - - - private static void addGroupClass(List annotationValueList, List javaClassList) { - if (CollectionUtil.isEmpty(annotationValueList)) { - return; - } - for (int i = 0; i < annotationValueList.size(); i++) { - TypeRef annotationValue = (TypeRef) annotationValueList.get(i); - DefaultJavaParameterizedType annotationValueType = (DefaultJavaParameterizedType) annotationValue.getType(); - javaClassList.add(annotationValueType.getGenericCanonicalName()); - } - } - - private static List getAnnotationValues(List validates,JavaAnnotation javaAnnotation){ - List annotationValueList = null; - String simpleName = javaAnnotation.getType().getSimpleName(); - if (simpleName.equalsIgnoreCase(ValidatorAnnotations.VALIDATED)) { - if (Objects.nonNull(javaAnnotation.getProperty(DocAnnotationConstants.VALUE_PROP))) { - annotationValueList = ((AnnotationValueList) javaAnnotation.getProperty(DocAnnotationConstants.VALUE_PROP)).getValueList(); - } - } else if (validates.contains(simpleName)) { - if (Objects.nonNull(javaAnnotation.getProperty(DocAnnotationConstants.GROUP_PROP))) { - annotationValueList = ((AnnotationValueList) javaAnnotation.getProperty(DocAnnotationConstants.GROUP_PROP)).getValueList(); - } - } - return annotationValueList; - } -} +/* + * smart-doc + * + * Copyright (C) 2019-2020 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; + +import com.power.common.util.CollectionUtil; +import com.power.common.util.StringUtil; +import com.power.doc.constants.DocAnnotationConstants; +import com.power.doc.constants.DocValidatorAnnotationEnum; +import com.power.doc.constants.ValidatorAnnotations; +import com.power.doc.model.DocJavaField; +import com.thoughtworks.qdox.model.*; +import com.thoughtworks.qdox.model.expression.AnnotationValue; +import com.thoughtworks.qdox.model.expression.AnnotationValueList; +import com.thoughtworks.qdox.model.expression.TypeRef; +import com.thoughtworks.qdox.model.impl.DefaultJavaField; +import com.thoughtworks.qdox.model.impl.DefaultJavaParameterizedType; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * Handle JavaClass + * + * @author yu 2019/12/21. + */ +public class JavaClassUtil { + + /** + * Get fields + * + * @param cls1 The JavaClass object + * @param i Recursive counter + * @return list of JavaField + */ + public static List getFields(JavaClass cls1, int i) { + List fieldList = new ArrayList<>(); + if (null == cls1) { + return fieldList; + } else if ("Object".equals(cls1.getSimpleName()) || "Timestamp".equals(cls1.getSimpleName()) || + "Date".equals(cls1.getSimpleName()) || "Locale".equals(cls1.getSimpleName())) { + return fieldList; + } else { + String className = cls1.getFullyQualifiedName(); + if (cls1.isInterface() && + !JavaClassValidateUtil.isCollection(className) && + !JavaClassValidateUtil.isMap(className)) { + List methods = cls1.getMethods(); + for (JavaMethod javaMethod : methods) { + String methodName = javaMethod.getName(); + if (!methodName.startsWith("get")) { + continue; + } + methodName = StringUtil.firstToLowerCase(methodName.substring(3, methodName.length())); + String comment = javaMethod.getComment(); + JavaField javaField = new DefaultJavaField(javaMethod.getReturns(), methodName); + DocJavaField docJavaField = DocJavaField.builder().setJavaField(javaField).setComment(comment); + fieldList.add(docJavaField); + } + } + JavaClass pcls = cls1.getSuperJavaClass(); + fieldList.addAll(getFields(pcls, i)); + List docJavaFields = new ArrayList<>(); + for (JavaField javaField : cls1.getFields()) { + docJavaFields.add(DocJavaField.builder().setComment(javaField.getComment()).setJavaField(javaField)); + } + fieldList.addAll(docJavaFields); + } + return fieldList; + } + + + /** + * get enum value + * + * @param javaClass enum class + * @param formDataEnum is return method + * @return Object + */ + public static Object getEnumValue(JavaClass javaClass, boolean formDataEnum) { + List javaFields = javaClass.getEnumConstants(); + List methodList = javaClass.getMethods(); + int annotation = 0; + for (JavaMethod method : methodList) { + if (method.getAnnotations().size() > 0) { + annotation = 1; + break; + } + } + Object value = null; + int index = 0; + for (JavaField javaField : javaFields) { + String simpleName = javaField.getType().getSimpleName(); + StringBuilder valueBuilder = new StringBuilder(); + valueBuilder.append("\"").append(javaField.getName()).append("\"").toString(); + if (formDataEnum) { + value = valueBuilder.toString(); + return value; + } + if (!JavaClassValidateUtil.isPrimitive(simpleName) && index < 1) { + if (null != javaField.getEnumConstantArguments() && annotation > 0) { + value = javaField.getEnumConstantArguments().get(0); + } else { + value = valueBuilder.toString(); + } + } + index++; + } + return value; + } + + + /** + * Get annotation simpleName + * + * @param annotationName annotationName + * @return String + */ + public static String getAnnotationSimpleName(String annotationName) { + return getClassSimpleName(annotationName); + } + + /** + * Get className + * + * @param className className + * @return String + */ + public static String getClassSimpleName(String className) { + if (className.contains(".")) { + int index = className.lastIndexOf("."); + className = className.substring(index + 1, className.length()); + } + return className; + } + + /** + * get Actual type + * + * @param javaClass JavaClass + * @return JavaClass + */ + public static JavaClass getActualType(JavaClass javaClass) { + return getActualTypes(javaClass).get(0); + } + + /** + * get Actual type list + * + * @param javaClass JavaClass + * @return JavaClass + */ + public static List getActualTypes(JavaClass javaClass) { + if (null == javaClass) { + return new ArrayList<>(0); + } + List javaClassList = new ArrayList<>(); + List actualTypes = ((DefaultJavaParameterizedType) javaClass).getActualTypeArguments(); + actualTypes.forEach(javaType -> { + JavaClass actualClass = (JavaClass) javaType; + javaClassList.add(actualClass); + }); + return javaClassList; + } + + /** + * Obtain Validate Group classes + * + * @param annotations the annotations of controller method param + * @return the group annotation value + */ + public static List getParamGroupJavaClass(List annotations) { + if (CollectionUtil.isEmpty(annotations)) { + return new ArrayList<>(0); + } + List javaClassList = new ArrayList<>(); + List validates = DocValidatorAnnotationEnum.listValidatorAnnotations(); + for (JavaAnnotation javaAnnotation : annotations) { + List annotationValueList = getAnnotationValues(validates, javaAnnotation); + addGroupClass(annotationValueList, javaClassList); + } + return javaClassList; + } + + /** + * Obtain Validate Group classes + * + * @param javaAnnotation the annotation of controller method param + * @return the group annotation value + */ + public static List getParamGroupJavaClass(JavaAnnotation javaAnnotation) { + if (Objects.isNull(javaAnnotation)) { + return new ArrayList<>(0); + } + List javaClassList = new ArrayList<>(); + List validates = DocValidatorAnnotationEnum.listValidatorAnnotations(); + List annotationValueList = getAnnotationValues(validates, javaAnnotation); + addGroupClass(annotationValueList, javaClassList); + return javaClassList; + } + + /** + * 通过name获取类标签的value + * + * @param cls 类 + * @param tagName 需要获取的标签name + * @return 类标签的value + * @author songhaozhi + */ + public static String getClassTagsValue(final JavaClass cls, final String tagName) { + if (StringUtil.isNotEmpty(tagName)) { + StringBuilder result = new StringBuilder(); + List tags = cls.getTags(); + for (int i = 0; i < tags.size(); i++) { + String value = tags.get(i).getValue(); + if (StringUtil.isEmpty(value)) { + throw new RuntimeException("ERROR: #" + cls.getName() + + "() - bad @" + tagName + " javadoc from " + cls.getName() + ", must be add comment if you use it."); + } + if (tagName.equals(tags.get(i).getName())) { + if (i != 0) { + result.append(","); + } + result.append(value); + } + } + return result.toString(); + } + return null; + } + + private static void addGroupClass(List annotationValueList, List javaClassList) { + if (CollectionUtil.isEmpty(annotationValueList)) { + return; + } + for (int i = 0; i < annotationValueList.size(); i++) { + TypeRef annotationValue = (TypeRef) annotationValueList.get(i); + DefaultJavaParameterizedType annotationValueType = (DefaultJavaParameterizedType) annotationValue.getType(); + javaClassList.add(annotationValueType.getGenericCanonicalName()); + } + } + + private static List getAnnotationValues(List validates, JavaAnnotation javaAnnotation) { + List annotationValueList = null; + String simpleName = javaAnnotation.getType().getSimpleName(); + if (simpleName.equalsIgnoreCase(ValidatorAnnotations.VALIDATED)) { + if (Objects.nonNull(javaAnnotation.getProperty(DocAnnotationConstants.VALUE_PROP))) { + annotationValueList = ((AnnotationValueList) javaAnnotation.getProperty(DocAnnotationConstants.VALUE_PROP)).getValueList(); + } + } else if (validates.contains(simpleName)) { + if (Objects.nonNull(javaAnnotation.getProperty(DocAnnotationConstants.GROUP_PROP))) { + annotationValueList = ((AnnotationValueList) javaAnnotation.getProperty(DocAnnotationConstants.GROUP_PROP)).getValueList(); + } + } + return annotationValueList; + } +}