diff --git a/src/main/java/com/power/doc/extension/dict/DictionaryValuesResolver.java b/src/main/java/com/power/doc/extension/dict/DictionaryValuesResolver.java new file mode 100644 index 00000000..08eb4d29 --- /dev/null +++ b/src/main/java/com/power/doc/extension/dict/DictionaryValuesResolver.java @@ -0,0 +1,9 @@ +package com.power.doc.extension.dict; + +import com.power.common.model.EnumDictionary; + +import java.util.Collection; + +public interface DictionaryValuesResolver { + Collection resolve(); +} diff --git a/src/main/java/com/power/doc/extension/package-info.java b/src/main/java/com/power/doc/extension/package-info.java new file mode 100644 index 00000000..d534461e --- /dev/null +++ b/src/main/java/com/power/doc/extension/package-info.java @@ -0,0 +1,4 @@ +/** + * customized extension point + */ +package com.power.doc.extension; diff --git a/src/main/java/com/power/doc/model/ApiErrorCodeDictionary.java b/src/main/java/com/power/doc/model/ApiErrorCodeDictionary.java index ab292e59..61aa066c 100644 --- a/src/main/java/com/power/doc/model/ApiErrorCodeDictionary.java +++ b/src/main/java/com/power/doc/model/ApiErrorCodeDictionary.java @@ -45,6 +45,11 @@ public class ApiErrorCodeDictionary { * enum class name */ private String enumClassName; + + /** + * customized enum values resolver + */ + private String valuesResolverClass; /** * code field */ @@ -109,6 +114,15 @@ public ApiErrorCodeDictionary setEnumClassName(String enumClassName) { return this; } + public String getValuesResolverClass() { + return this.valuesResolverClass; + } + + public ApiErrorCodeDictionary setValuesResolverClass(String valuesResolverClass) { + this.valuesResolverClass = valuesResolverClass; + return this; + } + @Override public String toString() { final StringBuilder sb = new StringBuilder("{"); diff --git a/src/main/java/com/power/doc/utils/DocClassUtil.java b/src/main/java/com/power/doc/utils/DocClassUtil.java index 669da3fc..49cf7dee 100644 --- a/src/main/java/com/power/doc/utils/DocClassUtil.java +++ b/src/main/java/com/power/doc/utils/DocClassUtil.java @@ -26,6 +26,8 @@ import com.power.doc.filter.ReturnTypeProcessor; import com.power.doc.model.ApiReturn; +import javax.annotation.Nonnull; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; import java.util.Stack; @@ -292,4 +294,12 @@ private static boolean isClassName(String className) { } return stack.isEmpty(); } + + public static T newInstance(@Nonnull Class classWithNoArgsConstructor) { + try { + return classWithNoArgsConstructor.getConstructor().newInstance(); + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | InstantiationException e) { + throw new IllegalArgumentException("Class must have the NoArgsConstructor"); + } + } } diff --git a/src/main/java/com/power/doc/utils/DocUtil.java b/src/main/java/com/power/doc/utils/DocUtil.java index c8db13cc..84938c00 100644 --- a/src/main/java/com/power/doc/utils/DocUtil.java +++ b/src/main/java/com/power/doc/utils/DocUtil.java @@ -23,12 +23,35 @@ package com.power.doc.utils; import com.mifmif.common.regex.Generex; -import com.power.common.util.*; -import com.power.doc.constants.*; -import com.power.doc.model.*; +import com.power.common.util.CollectionUtil; +import com.power.common.util.DateTimeUtil; +import com.power.common.util.EnumUtil; +import com.power.common.util.IDCardUtil; +import com.power.common.util.RandomUtil; +import com.power.common.util.StringUtil; +import com.power.common.util.ValidateUtil; +import com.power.doc.constants.DocAnnotationConstants; +import com.power.doc.constants.DocGlobalConstants; +import com.power.doc.constants.DocTags; +import com.power.doc.constants.JAXRSAnnotations; +import com.power.doc.constants.JakartaJaxrsAnnotations; +import com.power.doc.extension.dict.DictionaryValuesResolver; +import com.power.doc.model.ApiConfig; +import com.power.doc.model.ApiDataDictionary; +import com.power.doc.model.ApiDocDict; +import com.power.doc.model.ApiErrorCode; +import com.power.doc.model.ApiErrorCodeDictionary; +import com.power.doc.model.ApiReqParam; +import com.power.doc.model.DataDict; +import com.power.doc.model.DocJavaField; +import com.power.doc.model.FormData; import com.power.doc.model.request.RequestMapping; import com.thoughtworks.qdox.JavaProjectBuilder; -import com.thoughtworks.qdox.model.*; +import com.thoughtworks.qdox.model.DocletTag; +import com.thoughtworks.qdox.model.JavaAnnotation; +import com.thoughtworks.qdox.model.JavaClass; +import com.thoughtworks.qdox.model.JavaField; +import com.thoughtworks.qdox.model.JavaMethod; import com.thoughtworks.qdox.model.expression.Add; import com.thoughtworks.qdox.model.expression.AnnotationValue; import com.thoughtworks.qdox.model.expression.Expression; @@ -39,7 +62,22 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.IdentityHashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.Stack; +import java.util.UUID; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -757,7 +795,16 @@ public static List errorCodeDictToList(ApiConfig config, JavaProje clzz = classLoader.loadClass(dictionary.getEnumClassName()); } - if (clzz.isInterface()) { + Class valuesResolverClass = null; + if (StringUtil.isNotEmpty(dictionary.getValuesResolverClass())) { + valuesResolverClass = classLoader.loadClass(dictionary.getValuesResolverClass()); + } + if (null != valuesResolverClass && DictionaryValuesResolver.class.isAssignableFrom(valuesResolverClass)) { + DictionaryValuesResolver resolver = (DictionaryValuesResolver) DocClassUtil.newInstance(valuesResolverClass); + Collection enumDictionaries = resolver.resolve(); + errorCodeList.addAll(enumDictionaries); + } + else if (clzz.isInterface()) { Set> enumImplementSet = dictionary.getEnumImplementSet(); if (CollectionUtil.isEmpty(enumImplementSet)) { continue;