Skip to content

Commit 0fb8b44

Browse files
authored
Merge pull request #7 from wyjsonGo/develop
1.@param自定义参数 2.修改openLog()到openDebug() 3.新的文档生成方式
2 parents c1f523a + 8bb384f commit 0fb8b44

File tree

38 files changed

+512
-534
lines changed

38 files changed

+512
-534
lines changed

GoRouter-Annotation/src/main/java/com/wyjson/router/annotation/Param.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
@Retention(CLASS)
1111
public @interface Param {
1212

13+
// Mark param name
14+
String name() default "";
15+
1316
// remark of the field
1417
String remark() default "";
1518

GoRouter-Compiler/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ dependencies {
1616
implementation 'org.apache.commons:commons-lang3:3.12.0'
1717
implementation 'org.apache.commons:commons-collections4:4.4'
1818

19-
// implementation project(path: ':GoRouter-Annotation')
20-
implementation 'com.github.wyjsonGo.GoRouter:GoRouter-Annotation:1.1.0'
19+
implementation 'com.google.code.gson:gson:2.10.1'
20+
21+
implementation project(path: ':GoRouter-Annotation')
22+
// implementation 'com.github.wyjsonGo.GoRouter:GoRouter-Annotation:1.1.0'
2123

2224
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.wyjson.router.compiler.doc;
2+
3+
import static com.wyjson.router.compiler.utils.Constants.DOCS_PACKAGE_NAME;
4+
5+
import com.google.gson.GsonBuilder;
6+
import com.wyjson.router.annotation.Interceptor;
7+
import com.wyjson.router.annotation.Route;
8+
import com.wyjson.router.annotation.Service;
9+
import com.wyjson.router.compiler.doc.model.DocumentModel;
10+
import com.wyjson.router.compiler.doc.model.InterceptorModel;
11+
import com.wyjson.router.compiler.doc.model.ParamModel;
12+
import com.wyjson.router.compiler.doc.model.RouteModel;
13+
import com.wyjson.router.compiler.doc.model.ServiceModel;
14+
import com.wyjson.router.compiler.utils.Logger;
15+
16+
import java.io.IOException;
17+
import java.io.Writer;
18+
import java.util.List;
19+
20+
import javax.annotation.processing.Filer;
21+
import javax.lang.model.element.Element;
22+
import javax.lang.model.element.TypeElement;
23+
import javax.tools.StandardLocation;
24+
25+
public class DocumentUtils {
26+
27+
private static Writer docWriter;
28+
private static DocumentModel documentModel;
29+
private static boolean isDocEnable;
30+
31+
public static void createDoc(Filer mFiler, String moduleName, Logger logger, boolean isEnable) {
32+
isDocEnable = isEnable;
33+
if (!isDocEnable)
34+
return;
35+
try {
36+
documentModel = new DocumentModel();
37+
docWriter = mFiler.createResource(
38+
StandardLocation.SOURCE_OUTPUT,
39+
DOCS_PACKAGE_NAME,
40+
moduleName + "-gorouter-doc.json"
41+
).openWriter();
42+
} catch (IOException e) {
43+
logger.error(moduleName + " Failed to create the document because " + e.getMessage());
44+
}
45+
}
46+
47+
public static void generate(String moduleName, Logger logger) {
48+
if (!isDocEnable)
49+
return;
50+
try {
51+
docWriter.append(new GsonBuilder().setPrettyPrinting().create().toJson(documentModel));
52+
docWriter.flush();
53+
docWriter.close();
54+
} catch (IOException e) {
55+
logger.error(moduleName + " Failed to generate the document because " + e.getMessage());
56+
}
57+
}
58+
59+
public static void addService(String moduleName, Logger logger, Element element, Service service) {
60+
if (!isDocEnable)
61+
return;
62+
try {
63+
String className = ((TypeElement) element).getInterfaces().get(0).toString();
64+
String serviceName = className.substring(className.lastIndexOf(".") + 1);
65+
documentModel.getServices().put(serviceName, new ServiceModel(className, element.toString(), service.remark()));
66+
} catch (Exception e) {
67+
logger.error(moduleName + " Failed to add service [" + element.toString() + "] document, " + e.getMessage());
68+
}
69+
}
70+
71+
public static void addInterceptor(String moduleName, Logger logger, Element element, Interceptor interceptor) {
72+
if (!isDocEnable)
73+
return;
74+
try {
75+
documentModel.getInterceptors().add(new InterceptorModel(interceptor.priority(), element.toString(), interceptor.remark()));
76+
} catch (Exception e) {
77+
logger.error(moduleName + " Failed to add interceptor [" + element.toString() + "] document, " + e.getMessage());
78+
}
79+
}
80+
81+
public static void addRoute(String moduleName, Logger logger, Element element, Route route, String typeDoc, List<ParamModel> paramModels) {
82+
if (!isDocEnable)
83+
return;
84+
try {
85+
RouteModel routeModel = new RouteModel();
86+
routeModel.setPath(route.path());
87+
routeModel.setType(typeDoc);
88+
routeModel.setPathClass(element.toString());
89+
routeModel.setRemark(route.remark());
90+
if (route.tag() != 0) {
91+
routeModel.setTag(route.tag());
92+
}
93+
if (!paramModels.isEmpty()) {
94+
routeModel.setParamsType(paramModels);
95+
}
96+
documentModel.getRoutes().add(routeModel);
97+
} catch (Exception e) {
98+
logger.error(moduleName + " Failed to add route [" + element.toString() + "] document, " + e.getMessage());
99+
}
100+
}
101+
102+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.wyjson.router.compiler.doc.model;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class DocumentModel {
9+
10+
public Map<String, ServiceModel> services;
11+
public List<InterceptorModel> interceptors;
12+
public List<RouteModel> routes;
13+
14+
public DocumentModel() {
15+
}
16+
17+
public Map<String, ServiceModel> getServices() {
18+
if (services == null) {
19+
services = new HashMap<>();
20+
}
21+
return services;
22+
}
23+
24+
public List<InterceptorModel> getInterceptors() {
25+
if (interceptors == null) {
26+
interceptors = new ArrayList<>();
27+
}
28+
return interceptors;
29+
}
30+
31+
public List<RouteModel> getRoutes() {
32+
if (routes == null) {
33+
routes = new ArrayList<>();
34+
}
35+
return routes;
36+
}
37+
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.wyjson.router.compiler.doc.model;
2+
3+
public class InterceptorModel {
4+
5+
public int priority;
6+
public String remark;
7+
public String className;
8+
9+
public InterceptorModel() {
10+
}
11+
12+
public InterceptorModel(int priority, String className, String remark) {
13+
this.priority = priority;
14+
this.className = className;
15+
this.remark = remark;
16+
}
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.wyjson.router.compiler.doc.model;
2+
3+
public class ParamModel {
4+
5+
private String name;
6+
private String remark;
7+
private String type;
8+
private boolean required;
9+
10+
public ParamModel() {
11+
}
12+
13+
public ParamModel(String name, String type, boolean required, String remark) {
14+
this.name = name;
15+
this.type = type;
16+
this.required = required;
17+
this.remark = remark;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public void setType(String type) {
25+
this.type = type;
26+
}
27+
28+
public void setRequired(boolean required) {
29+
this.required = required;
30+
}
31+
32+
public void setRemark(String remark) {
33+
this.remark = remark;
34+
}
35+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.wyjson.router.compiler.doc.model;
2+
3+
import java.util.List;
4+
5+
public class RouteModel {
6+
7+
private String path;
8+
private String remark;
9+
private String type;
10+
private String pathClass;
11+
private Integer tag;
12+
private List<ParamModel> paramsType;
13+
14+
public RouteModel() {
15+
}
16+
17+
public RouteModel(String path, String type, String pathClass, Integer tag, List<ParamModel> paramsType, String remark) {
18+
this.path = path;
19+
this.type = type;
20+
this.pathClass = pathClass;
21+
this.tag = tag;
22+
this.paramsType = paramsType;
23+
this.remark = remark;
24+
}
25+
26+
public void setPath(String path) {
27+
this.path = path;
28+
}
29+
30+
public void setType(String type) {
31+
this.type = type;
32+
}
33+
34+
public void setPathClass(String pathClass) {
35+
this.pathClass = pathClass;
36+
}
37+
38+
public void setTag(Integer tag) {
39+
this.tag = tag;
40+
}
41+
42+
public void setParamsType(List<ParamModel> paramsType) {
43+
this.paramsType = paramsType;
44+
}
45+
46+
public void setRemark(String remark) {
47+
this.remark = remark;
48+
}
49+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.wyjson.router.compiler.doc.model;
2+
3+
public class ServiceModel {
4+
5+
public String remark;
6+
public String prototype;
7+
public String className;
8+
9+
public ServiceModel() {
10+
}
11+
12+
public ServiceModel(String prototype, String className, String remark) {
13+
this.prototype = prototype;
14+
this.className = className;
15+
this.remark = remark;
16+
}
17+
18+
}

0 commit comments

Comments
 (0)