fix out of index

This commit is contained in:
oppofind 2020-08-22 22:49:30 +08:00
parent ca15dd152b
commit b7a2ce6380
1 changed files with 263 additions and 253 deletions

View File

@ -1,253 +1,263 @@
/* /*
* smart-doc * smart-doc
* *
* Copyright (C) 2018-2020 smart-doc * Copyright (C) 2018-2020 smart-doc
* *
* Licensed to the Apache Software Foundation (ASF) under one * Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file * or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information * distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file * regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the * to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance * "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at * with the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an * software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package com.power.doc.utils; package com.power.doc.utils;
import com.power.common.util.StringUtil; import com.power.common.util.StringUtil;
import com.power.doc.filter.ReturnTypeProcessor; import com.power.doc.filter.ReturnTypeProcessor;
import com.power.doc.model.ApiReturn; import com.power.doc.model.ApiReturn;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* Description: * Description:
* Doc class handle util * Doc class handle util
* *
* @author yu 2018//14. * @author yu 2018//14.
*/ */
public class DocClassUtil { public class DocClassUtil {
/** /**
* get class names by generic class name * get class names by generic class name
* *
* @param returnType generic class name * @param returnType generic class name
* @return array of string * @return array of string
*/ */
public static String[] getSimpleGicName(String returnType) { public static String[] getSimpleGicName(String returnType) {
if (returnType.contains("<")) { if (returnType.contains("<")) {
String pre = returnType.substring(0, returnType.indexOf("<")); String pre = returnType.substring(0, returnType.indexOf("<"));
if (JavaClassValidateUtil.isMap(pre)) { if (JavaClassValidateUtil.isMap(pre)) {
return getMapKeyValueType(returnType); return getMapKeyValueType(returnType);
} }
String type = returnType.substring(returnType.indexOf("<") + 1, returnType.lastIndexOf(">")); String type = returnType.substring(returnType.indexOf("<") + 1, returnType.lastIndexOf(">"));
if (JavaClassValidateUtil.isCollection(pre)) { if (JavaClassValidateUtil.isCollection(pre)) {
return type.split(" "); return type.split(" ");
} }
String[] arr = type.split(","); String[] arr = type.split(",");
return classNameFix(arr); return classNameFix(arr);
} else { } else {
return returnType.split(" "); return returnType.split(" ");
} }
} }
/** /**
* Get a simple type name from a generic class name * Get a simple type name from a generic class name
* *
* @param gicName Generic class name * @param gicName Generic class name
* @return String * @return String
*/ */
public static String getSimpleName(String gicName) { public static String getSimpleName(String gicName) {
if (gicName.contains("<")) { if (gicName.contains("<")) {
return gicName.substring(0, gicName.indexOf("<")); return gicName.substring(0, gicName.indexOf("<"));
} else { } else {
return gicName; return gicName;
} }
} }
/** /**
* Automatic repair of generic split class names * Automatic repair of generic split class names
* *
* @param arr arr of class name * @param arr arr of class name
* @return array of String * @return array of String
*/ */
private static String[] classNameFix(String[] arr) { private static String[] classNameFix(String[] arr) {
List<String> classes = new ArrayList<>(); List<String> classes = new ArrayList<>();
List<Integer> indexList = new ArrayList<>(); List<Integer> indexList = new ArrayList<>();
int globIndex = 0; int globIndex = 0;
int length = arr.length; int length = arr.length;
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
if (classes.size() > 0) { if (classes.size() > 0) {
int index = classes.size() - 1; int index = classes.size() - 1;
if (!isClassName(classes.get(index))) { if (!isClassName(classes.get(index))) {
globIndex = globIndex + 1; globIndex = globIndex + 1;
if (globIndex < length) { if (globIndex < length) {
indexList.add(globIndex); indexList.add(globIndex);
String className = classes.get(index) + "," + arr[globIndex]; String className = classes.get(index) + "," + arr[globIndex];
classes.set(index, className); classes.set(index, className);
} }
} else { } else {
globIndex = globIndex + 1; globIndex = globIndex + 1;
if (globIndex < length) { if (globIndex < length) {
if (isClassName(arr[globIndex])) { if (isClassName(arr[globIndex])) {
indexList.add(globIndex); indexList.add(globIndex);
classes.add(arr[globIndex]); classes.add(arr[globIndex]);
} else { } else {
if (!indexList.contains(globIndex) && !indexList.contains(globIndex + 1)) { if (!indexList.contains(globIndex) && !indexList.contains(globIndex + 1)) {
indexList.add(globIndex); indexList.add(globIndex);
classes.add(arr[globIndex] + "," + arr[globIndex + 1]); classes.add(arr[globIndex] + "," + arr[globIndex + 1]);
globIndex = globIndex + 1; globIndex = globIndex + 1;
indexList.add(globIndex); indexList.add(globIndex);
} if (arr.length > globIndex + 1) {
} classes.add(arr[globIndex] + "," + arr[globIndex + 1]);
} globIndex = globIndex + 1;
} indexList.add(globIndex);
} else { }
if (isClassName(arr[i])) { }
indexList.add(i); }
classes.add(arr[i]); }
} else { }
if (!indexList.contains(i) && !indexList.contains(i + 1)) { } else {
globIndex = i + 1; if (isClassName(arr[i])) {
classes.add(arr[i] + "," + arr[globIndex]); indexList.add(i);
indexList.add(i); classes.add(arr[i]);
indexList.add(i + 1); } else {
} if (!indexList.contains(i) && !indexList.contains(i + 1)) {
} globIndex = i + 1;
} classes.add(arr[i] + "," + arr[globIndex]);
} indexList.add(i);
return classes.toArray(new String[classes.size()]); indexList.add(i + 1);
} if (arr.length > globIndex) {
classes.add(arr[i] + "," + arr[globIndex]);
/** indexList.add(i);
* get map key and value type name populate into array. indexList.add(i + 1);
* }
* @param gName generic class name }
* @return array of string }
*/ }
public static String[] getMapKeyValueType(String gName) { }
if (gName.contains("<")) { return classes.toArray(new String[classes.size()]);
String[] arr = new String[2]; }
String key = gName.substring(gName.indexOf("<") + 1, gName.indexOf(","));
String value = gName.substring(gName.indexOf(",") + 1, gName.lastIndexOf(">")); /**
arr[0] = key; * get map key and value type name populate into array.
arr[1] = value; *
return arr; * @param gName generic class name
} else { * @return array of string
return new String[0]; */
} public static String[] getMapKeyValueType(String gName) {
if (gName.contains("<")) {
} String[] arr = new String[2];
String key = gName.substring(gName.indexOf("<") + 1, gName.indexOf(","));
/** String value = gName.substring(gName.indexOf(",") + 1, gName.lastIndexOf(">"));
* Convert the parameter types exported to the api document arr[0] = key;
* arr[1] = value;
* @param javaTypeName java simple typeName return arr;
* @return String } else {
*/ return new String[0];
public static String processTypeNameForParams(String javaTypeName) { }
if (StringUtil.isEmpty(javaTypeName)) {
return "object"; }
}
if (javaTypeName.length() == 1) { /**
return "object"; * Convert the parameter types exported to the api document
} *
if (javaTypeName.contains("[]")) { * @param javaTypeName java simple typeName
return "array"; * @return String
} */
switch (javaTypeName) { public static String processTypeNameForParams(String javaTypeName) {
case "java.lang.String": if (StringUtil.isEmpty(javaTypeName)) {
case "string": return "object";
case "char": }
case "java.util.Byte": if (javaTypeName.length() == 1) {
case "byte": return "object";
case "date": }
case "localdatetime": if (javaTypeName.contains("[]")) {
case "localdate": return "array";
case "localtime": }
case "timestamp": switch (javaTypeName) {
return "string"; case "java.lang.String":
case "java.util.List": case "string":
case "list": case "char":
return "array"; case "java.util.Byte":
case "java.lang.Integer": case "byte":
case "integer": case "date":
case "int": case "localdatetime":
return "int32"; case "localdate":
case "short": case "localtime":
case "java.lang.Short": case "timestamp":
return "int16"; return "string";
case "double": case "java.util.List":
return "double"; case "list":
case "java.lang.Long": return "array";
case "long": case "java.lang.Integer":
return "int64"; case "integer":
case "java.lang.Float": case "int":
case "float": return "int32";
return "float"; case "short":
case "bigdecimal": case "java.lang.Short":
case "biginteger": return "int16";
return "number"; case "double":
case "java.lang.Boolean": return "double";
case "boolean": case "java.lang.Long":
return "boolean"; case "long":
case "map": return "int64";
return "map"; case "java.lang.Float":
case "multipartfile": case "float":
return "file"; return "float";
default: case "bigdecimal":
return "object"; case "biginteger":
} return "number";
case "java.lang.Boolean":
} case "boolean":
return "boolean";
/** case "map":
* process return type return "map";
* case "multipartfile":
* @param fullyName fully name return "file";
* @return ApiReturn default:
*/ return "object";
public static ApiReturn processReturnType(String fullyName) { }
ReturnTypeProcessor processor = new ReturnTypeProcessor();
processor.setTypeName(fullyName); }
return processor.process();
} /**
* process return type
/** *
* rewrite request param * @param fullyName fully name
* * @return ApiReturn
* @param typeName param type name */
* @return String public static ApiReturn processReturnType(String fullyName) {
*/ ReturnTypeProcessor processor = new ReturnTypeProcessor();
public static String rewriteRequestParam(String typeName) { processor.setTypeName(fullyName);
switch (typeName) { return processor.process();
case "org.springframework.data.domain.Pageable": }
return "org.springframework.data.domain.PageRequest";
default: /**
return typeName; * rewrite request param
} *
} * @param typeName param type name
* @return String
private static boolean isClassName(String className) { */
if (StringUtil.isEmpty(className)) { public static String rewriteRequestParam(String typeName) {
return false; switch (typeName) {
} case "org.springframework.data.domain.Pageable":
if (className.contains("<") && !className.contains(">")) { return "org.springframework.data.domain.PageRequest";
return false; default:
} else if (className.contains(">") && !className.contains("<")) { return typeName;
return false; }
} else { }
return true;
} private static boolean isClassName(String className) {
} if (StringUtil.isEmpty(className)) {
} return false;
}
if (className.contains("<") && !className.contains(">")) {
return false;
} else if (className.contains(">") && !className.contains("<")) {
return false;
} else {
return true;
}
}
}