optimized enum handle

This commit is contained in:
oppofind 2019-12-31 12:30:50 +08:00
parent 0633ab5f99
commit dde85485ca
2 changed files with 18 additions and 6 deletions

View File

@ -92,7 +92,7 @@ public class FormDataBuildHelper {
formData.setDesc(comment);
formDataList.add(formData);
} else if (javaClass.isEnum()) {
Object value = JavaClassUtil.getEnumValue(javaClass, Boolean.FALSE);
Object value = JavaClassUtil.getEnumValue(javaClass, Boolean.TRUE);
FormData formData = new FormData();
formData.setKey(pre + fieldName);
formData.setType("text");

View File

@ -2,12 +2,14 @@ package com.power.doc.utils;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaField;
import com.thoughtworks.qdox.model.JavaMethod;
import java.util.ArrayList;
import java.util.List;
/**
* Handle JavaClass
*
* @author yu 2019/12/21.
*/
public class JavaClassUtil {
@ -37,24 +39,33 @@ public class JavaClassUtil {
/**
* get enum value
* @param javaClass enum class
* @param returnEnum is return method
*
* @param javaClass enum class
* @param formDataEnum is return method
* @return Object
*/
public static Object getEnumValue(JavaClass javaClass, boolean returnEnum) {
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 (returnEnum) {
if (formDataEnum) {
value = valueBuilder.toString();
return value;
}
if (!JavaClassValidateUtil.isPrimitive(simpleName) && index < 1) {
if (null != javaField.getEnumConstantArguments()) {
if (null != javaField.getEnumConstantArguments() && annotation > 0) {
value = javaField.getEnumConstantArguments().get(0);
} else {
value = valueBuilder.toString();
@ -78,6 +89,7 @@ public class JavaClassUtil {
/**
* Get className
*
* @param className className
* @return String
*/