optimized code
This commit is contained in:
parent
552e52ccf5
commit
74948b4a7a
|
@ -10,6 +10,7 @@ import com.power.doc.constants.TemplateVariable;
|
|||
import com.power.doc.model.*;
|
||||
import com.power.doc.utils.BeetlTemplateUtil;
|
||||
import com.power.doc.utils.DocUtil;
|
||||
import com.power.doc.utils.EnumUtil;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.beetl.core.Template;
|
||||
|
||||
|
@ -203,32 +204,15 @@ public class DocBuilderTemplate {
|
|||
}
|
||||
clzz = Class.forName(apiDataDictionary.getEnumClassName());
|
||||
}
|
||||
List<DataDict> enumDictionaryList = EnumUtil.getEnumValues(clzz,apiDataDictionary.getCodeField(),
|
||||
apiDataDictionary.getDescField());
|
||||
if (!clzz.isEnum()) {
|
||||
throw new RuntimeException(clzz.getCanonicalName() + " is not an enum class.");
|
||||
}
|
||||
List<DataDict> dataDictList = new ArrayList<>();
|
||||
Object[] objects = clzz.getEnumConstants();
|
||||
String valueMethodName = "get" + StringUtil.firstToUpperCase(apiDataDictionary.getCodeField());
|
||||
String descMethodName = "get" + StringUtil.firstToUpperCase(apiDataDictionary.getDescField());
|
||||
Method valueMethod = clzz.getMethod(valueMethodName);
|
||||
Method descMethod = clzz.getMethod(descMethodName);
|
||||
for (Object object : objects) {
|
||||
Object val = valueMethod.invoke(object);
|
||||
Object desc = descMethod.invoke(object);
|
||||
DataDict dataDict = new DataDict();
|
||||
if (val instanceof String) {
|
||||
dataDict.setType("string");
|
||||
} else {
|
||||
dataDict.setType("int32");
|
||||
}
|
||||
dataDict.setDesc(String.valueOf(desc));
|
||||
dataDict.setValue(String.valueOf(val));
|
||||
dataDictList.add(dataDict);
|
||||
}
|
||||
apiDocDict.setDataDictList(dataDictList);
|
||||
apiDocDict.setDataDictList(enumDictionaryList);
|
||||
apiDocDictList.add(apiDocDict);
|
||||
}
|
||||
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | ClassNotFoundException e) {
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return apiDocDictList;
|
||||
|
@ -253,24 +237,11 @@ public class DocBuilderTemplate {
|
|||
}
|
||||
clzz = Class.forName(dictionary.getEnumClassName());
|
||||
}
|
||||
if (!clzz.isEnum()) {
|
||||
throw new RuntimeException(clzz.getCanonicalName() + " is not an enum class.");
|
||||
}
|
||||
Object[] objects = clzz.getEnumConstants();
|
||||
String valueMethodName = "get" + StringUtil.firstToUpperCase(dictionary.getCodeField());
|
||||
String descMethodName = "get" + StringUtil.firstToUpperCase(dictionary.getDescField());
|
||||
Method valueMethod = clzz.getMethod(valueMethodName);
|
||||
Method descMethod = clzz.getMethod(descMethodName);
|
||||
for (Object object : objects) {
|
||||
Object val = valueMethod.invoke(object);
|
||||
Object desc = descMethod.invoke(object);
|
||||
ApiErrorCode errorCode = new ApiErrorCode();
|
||||
errorCode.setDesc(String.valueOf(desc));
|
||||
errorCode.setValue(String.valueOf(val));
|
||||
errorCodeList.add(errorCode);
|
||||
}
|
||||
List<ApiErrorCode> enumDictionaryList = EnumUtil.getEnumValues(clzz,dictionary.getCodeField(),
|
||||
dictionary.getDescField());
|
||||
errorCodeList.addAll(enumDictionaryList);
|
||||
}
|
||||
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | ClassNotFoundException e) {
|
||||
} catch ( ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return errorCodeList;
|
||||
|
|
|
@ -543,8 +543,7 @@ public class SourceBuilder {
|
|||
comment = field.getComment();
|
||||
}
|
||||
if (StringUtil.isNotEmpty(comment)) {
|
||||
comment = comment.replaceAll("\r\n", "<br>");
|
||||
comment = comment.replaceAll("\n", "<br>");
|
||||
comment = DocUtil.replaceNewLineToHtmlBr(comment);
|
||||
}
|
||||
if (DocClassUtil.isPrimitive(subTypeName)) {
|
||||
ApiParam param = ApiParam.of().setField(pre + fieldName);
|
||||
|
@ -560,8 +559,7 @@ public class SourceBuilder {
|
|||
JavaClass javaClass = builder.getClassByName(subTypeName);
|
||||
String enumComments = javaClass.getComment();
|
||||
if (StringUtil.isNotEmpty(enumComments) && javaClass.isEnum()) {
|
||||
enumComments = enumComments.replaceAll("\r\n", "<br>");
|
||||
enumComments = enumComments.replaceAll("\n", "<br>");
|
||||
enumComments = DocUtil.replaceNewLineToHtmlBr(enumComments);
|
||||
comment = comment + "(See: " + enumComments + ")";
|
||||
}
|
||||
String processedType = DocClassUtil.processTypeNameForParams(typeSimpleName.toLowerCase());
|
||||
|
|
|
@ -6,48 +6,5 @@ package com.power.doc.model;
|
|||
*
|
||||
* @author yu 2018/06/25.
|
||||
*/
|
||||
public class ApiErrorCode {
|
||||
|
||||
/**
|
||||
* error code
|
||||
*/
|
||||
private String value;
|
||||
|
||||
/**
|
||||
* error code type
|
||||
*/
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* error description
|
||||
*/
|
||||
private String desc;
|
||||
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public ApiErrorCode setValue(String value) {
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public ApiErrorCode setType(String type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
public ApiErrorCode setDesc(String desc) {
|
||||
this.desc = desc;
|
||||
return this;
|
||||
}
|
||||
public class ApiErrorCode extends EnumDictionary {
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.power.doc.model;
|
||||
|
||||
/**
|
||||
* @since 1.7.9
|
||||
* @author yu 2019/12/7.
|
||||
*/
|
||||
public class ApiErrorCodeDictionary {
|
||||
|
|
|
@ -1,24 +1,8 @@
|
|||
package com.power.doc.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author yu 2019/10/31.
|
||||
*/
|
||||
@Data
|
||||
public class DataDict {
|
||||
public class DataDict extends EnumDictionary {
|
||||
|
||||
/**
|
||||
* dict value
|
||||
*/
|
||||
private String value;
|
||||
|
||||
/**
|
||||
* code type
|
||||
*/
|
||||
private String Type;
|
||||
/**
|
||||
* dict desc
|
||||
*/
|
||||
private String desc;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
package com.power.doc.model;
|
||||
|
||||
/**
|
||||
* @since 1.7.9
|
||||
* @author yu 2019/12/7.
|
||||
*/
|
||||
public class EnumDictionary {
|
||||
|
||||
/**
|
||||
* dict value
|
||||
*/
|
||||
private String value;
|
||||
|
||||
/**
|
||||
* code type
|
||||
*/
|
||||
private String type;
|
||||
/**
|
||||
* dict desc
|
||||
*/
|
||||
private String desc;
|
||||
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public EnumDictionary setValue(String value) {
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public EnumDictionary setType(String type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
public EnumDictionary setDesc(String desc) {
|
||||
this.desc = desc;
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -322,8 +322,7 @@ public class DocUtil {
|
|||
} else {
|
||||
value = entry.getKey() + entry.getValue();
|
||||
}
|
||||
value = value.replace("\r\n", "<br/>");
|
||||
value = value.replace("\r", "<br/>");
|
||||
value = replaceNewLineToHtmlBr(value);
|
||||
}
|
||||
}
|
||||
return value;
|
||||
|
@ -343,4 +342,11 @@ public class DocUtil {
|
|||
return valueId.substring(length - 32, length);
|
||||
}
|
||||
}
|
||||
|
||||
public static String replaceNewLineToHtmlBr(String content){
|
||||
if(StringUtil.isNotEmpty(content)){
|
||||
return content.replaceAll("(\r\n|\r|\n|\n\r)", "<br>");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
package com.power.doc.utils;
|
||||
|
||||
import com.power.common.util.StringUtil;
|
||||
import com.power.doc.model.EnumDictionary;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author yu 2019/12/7.
|
||||
*/
|
||||
public class EnumUtil {
|
||||
|
||||
/**
|
||||
* get enum values
|
||||
*
|
||||
* @param clzz class
|
||||
* @param codeField code field
|
||||
* @param descField desc field
|
||||
* @return
|
||||
*/
|
||||
public static <T extends EnumDictionary> List<T> getEnumValues(Class clzz, String codeField, String descField) {
|
||||
if (!clzz.isEnum()) {
|
||||
throw new RuntimeException(clzz.getCanonicalName() + " is not an enum class.");
|
||||
}
|
||||
Object[] objects = clzz.getEnumConstants();
|
||||
String valueMethodName = "get" + StringUtil.firstToUpperCase(codeField);
|
||||
String descMethodName = "get" + StringUtil.firstToUpperCase(descField);
|
||||
List<T> enumDictionaryList = new ArrayList<>();
|
||||
try {
|
||||
Method valueMethod = clzz.getMethod(valueMethodName);
|
||||
Method descMethod = clzz.getMethod(descMethodName);
|
||||
for (Object object : objects) {
|
||||
Object val = valueMethod.invoke(object);
|
||||
Object desc = descMethod.invoke(object);
|
||||
EnumDictionary dataDict = new EnumDictionary();
|
||||
if (val instanceof String) {
|
||||
dataDict.setType("string");
|
||||
} else {
|
||||
dataDict.setType("int32");
|
||||
}
|
||||
dataDict.setDesc(String.valueOf(desc));
|
||||
dataDict.setValue(String.valueOf(val));
|
||||
enumDictionaryList.add((T) dataDict);
|
||||
}
|
||||
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return enumDictionaryList;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue