Skip to content

Commit

Permalink
🐳 #353 对接文档支持框架内置错误码的生成
Browse files Browse the repository at this point in the history
  • Loading branch information
iohao committed Aug 14, 2024
1 parent 0cc8181 commit 4ac2fdb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.iohao.game.action.skeleton.core.doc;

import com.iohao.game.action.skeleton.core.exception.ActionErrorEnum;
import com.iohao.game.action.skeleton.core.exception.MsgExceptionInfo;
import com.iohao.game.common.kit.CollKit;
import com.thoughtworks.qdox.JavaProjectBuilder;
Expand All @@ -28,6 +29,8 @@

import java.io.File;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
Expand All @@ -50,25 +53,56 @@ List<ActionDocument> analyseActionDocument(IoGameDocument ioGameDocument, TypeMa
}

List<ErrorCodeDocument> analyseErrorCodeDocument(Class<? extends MsgExceptionInfo> clazz) {
JavaClass javaClass = analyseJavaClass(clazz);
return analyseErrorCodeDocument(javaClass);

var analyseJavaClassRecord = analyseJavaClass(clazz);
if (!analyseJavaClassRecord.exists) {
// 框架内置错误码的特殊处理。因为编译后已经没有源码了,所以无法获取框架的 ActionErrorEnum 相关源码。
return analyseActionErrorEnumDocument(clazz);
}

return analyseErrorCodeDocument(analyseJavaClassRecord.javaClass());
}

JavaClass analyseJavaClass(Class<?> clazz) {
JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder();
private List<ErrorCodeDocument> analyseActionErrorEnumDocument(Class<? extends MsgExceptionInfo> clazz) {

if (!ActionErrorEnum.class.equals(clazz)) {
return Collections.emptyList();
}

return Arrays.stream(ActionErrorEnum.values()).map(code -> {
ErrorCodeDocument errorCodeDocument = new ErrorCodeDocument();
errorCodeDocument.setName(code.name());
errorCodeDocument.setValue(code.getCode());
errorCodeDocument.setDescription(code.getMsg());
return errorCodeDocument;
}).toList();
}

AnalyseJavaClassRecord analyseJavaClass(Class<?> clazz) {

URL resource = clazz.getResource(clazz.getSimpleName() + ".class");
String srcPath = sourceFilePathFun.apply(resource).replace("class", "java");

JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder();

File file = new File(srcPath);
// 源码在此包才做处理
if (file.exists()) {
boolean exists = file.exists();
if (exists) {
javaProjectBuilder.addSourceTree(file);
} else {
}

if (!exists && !ActionErrorEnum.class.equals(clazz)) {
log.warn("无法获取 {} 相关源码", clazz);
}

return javaProjectBuilder.getClassByName(clazz.getName());
JavaClass javaClass = javaProjectBuilder.getClassByName(clazz.getName());

return new AnalyseJavaClassRecord(exists, javaClass);
}

record AnalyseJavaClassRecord(boolean exists, JavaClass javaClass) {

}

private final Function<URL, String> sourceFilePathFun = resourceUrl -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private IoGameDocument analyse() {
.forEach(broadcastDocument -> {
// 广播业务数据解析,使用类信息的文档注释
Class<?> dataClass = broadcastDocument.getDataClass();
JavaClass javaClass = DocumentAnalyseKit.analyseJavaClass(dataClass);
JavaClass javaClass = DocumentAnalyseKit.analyseJavaClass(dataClass).javaClass();
String classComment = javaClass.getComment();

broadcastDocument.setDataDescription(classComment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public final class HeadMetadata implements Serializable {
ExecutorSelectEnum executorSelect;
/** traceId */
String traceId;
/** 框架自用字段。将来变化可能较大,开发者请不要使用。 */
byte[] userProcessorExecutorSelectorBytes;
/** 临时变量 */
transient Object other;
Expand Down

0 comments on commit 4ac2fdb

Please sign in to comment.