feat:gitee #I1ADFQ

This commit is contained in:
songhaozhi 2020-03-20 23:30:57 +08:00
parent ee8a79d837
commit a6b435e7f5
3 changed files with 291 additions and 256 deletions

View File

@ -99,6 +99,7 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate {
private List<ApiMethodDoc> buildControllerMethod(final JavaClass cls, ApiConfig apiConfig, ProjectDocConfigBuilder projectBuilder) {
String clazName = cls.getCanonicalName();
String classAuthor = JavaClassUtil.getClassTagsValue(cls, DocTags.AUTHOR);
List<JavaAnnotation> 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<String, String> 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<String> springMvcRequestAnnotations = SpringMvcRequestAnnotationsEnum.listSpringMvcRequestAnnotations();
List<FormData> 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;

View File

@ -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);
}

View File

@ -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<DocJavaField> getFields(JavaClass cls1, int i) {
List<DocJavaField> 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<JavaMethod> 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<DocJavaField> 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<JavaField> javaFields = javaClass.getEnumConstants();
List<JavaMethod> 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<JavaClass> getActualTypes(JavaClass javaClass) {
if (null == javaClass) {
return new ArrayList<>(0);
}
List<JavaClass> javaClassList = new ArrayList<>();
List<JavaType> 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<String> getParamGroupJavaClass(List<JavaAnnotation> annotations) {
if (CollectionUtil.isEmpty(annotations)) {
return new ArrayList<>(0);
}
List<String> javaClassList = new ArrayList<>();
List<String> validates = DocValidatorAnnotationEnum.listValidatorAnnotations();
for (JavaAnnotation javaAnnotation : annotations) {
List<AnnotationValue> 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<String> getParamGroupJavaClass(JavaAnnotation javaAnnotation) {
if (Objects.isNull(javaAnnotation)) {
return new ArrayList<>(0);
}
List<String> javaClassList = new ArrayList<>();
List<String> validates = DocValidatorAnnotationEnum.listValidatorAnnotations();
List<AnnotationValue> annotationValueList = getAnnotationValues(validates,javaAnnotation);
addGroupClass(annotationValueList, javaClassList);
return javaClassList;
}
private static void addGroupClass(List<AnnotationValue> annotationValueList, List<String> 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<AnnotationValue> getAnnotationValues(List<String> validates,JavaAnnotation javaAnnotation){
List<AnnotationValue> 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<DocJavaField> getFields(JavaClass cls1, int i) {
List<DocJavaField> 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<JavaMethod> 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<DocJavaField> 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<JavaField> javaFields = javaClass.getEnumConstants();
List<JavaMethod> 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<JavaClass> getActualTypes(JavaClass javaClass) {
if (null == javaClass) {
return new ArrayList<>(0);
}
List<JavaClass> javaClassList = new ArrayList<>();
List<JavaType> 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<String> getParamGroupJavaClass(List<JavaAnnotation> annotations) {
if (CollectionUtil.isEmpty(annotations)) {
return new ArrayList<>(0);
}
List<String> javaClassList = new ArrayList<>();
List<String> validates = DocValidatorAnnotationEnum.listValidatorAnnotations();
for (JavaAnnotation javaAnnotation : annotations) {
List<AnnotationValue> 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<String> getParamGroupJavaClass(JavaAnnotation javaAnnotation) {
if (Objects.isNull(javaAnnotation)) {
return new ArrayList<>(0);
}
List<String> javaClassList = new ArrayList<>();
List<String> validates = DocValidatorAnnotationEnum.listValidatorAnnotations();
List<AnnotationValue> 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<DocletTag> 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<AnnotationValue> annotationValueList, List<String> 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<AnnotationValue> getAnnotationValues(List<String> validates, JavaAnnotation javaAnnotation) {
List<AnnotationValue> 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;
}
}