1
- package com .apigcc ;
1
+ package com .apigcc . core ;
2
2
3
- import com .apigcc .parser .VisitorParser ;
4
- import com .apigcc .render .ProjectRender ;
5
- import com .apigcc .schema .Project ;
6
- import com .apigcc .spring .SpringParserStrategy ;
3
+ import com .apigcc .core .common .helper .StringHelper ;
4
+ import com .apigcc .core .parser .ParserStrategy ;
5
+ import com .apigcc .core .parser .VisitorParser ;
6
+ import com .apigcc .core .render .ProjectRender ;
7
+ import com .apigcc .core .resolver .TypeResolvers ;
8
+ import com .apigcc .core .schema .Project ;
7
9
import com .github .javaparser .ParseResult ;
8
10
import com .github .javaparser .ParserConfiguration ;
9
11
import com .github .javaparser .ast .CompilationUnit ;
13
15
import com .github .javaparser .symbolsolver .resolution .typesolvers .JavaParserTypeSolver ;
14
16
import com .github .javaparser .symbolsolver .resolution .typesolvers .ReflectionTypeSolver ;
15
17
import com .github .javaparser .utils .SourceRoot ;
18
+ import com .google .common .collect .Lists ;
19
+ import lombok .Getter ;
16
20
import lombok .extern .slf4j .Slf4j ;
17
21
18
22
import java .io .IOException ;
19
23
import java .nio .file .Path ;
24
+ import java .util .List ;
25
+ import java .util .Objects ;
26
+ import java .util .ServiceLoader ;
20
27
21
28
@ Slf4j
22
29
public class Apigcc {
@@ -27,12 +34,17 @@ public static Apigcc getInstance(){
27
34
return INSTANCE ;
28
35
}
29
36
37
+ @ Getter
30
38
private Context context ;
39
+ @ Getter
31
40
private Project project = new Project ();
32
41
33
42
private VisitorParser visitorParser = new VisitorParser ();
34
43
private ParserConfiguration parserConfiguration ;
35
44
45
+ @ Getter
46
+ private TypeResolvers typeResolvers = new TypeResolvers ();
47
+
36
48
private Apigcc (){
37
49
init (new Context ());
38
50
}
@@ -52,7 +64,6 @@ private void init(Context context){
52
64
project .setName (context .getName ());
53
65
project .setDescription (context .getDescription ());
54
66
project .setVersion (context .getVersion ());
55
- visitorParser .setParserStrategy (new SpringParserStrategy ());
56
67
57
68
CombinedTypeSolver typeSolver = new CombinedTypeSolver ();
58
69
for (Path dependency : context .getDependencies ()) {
@@ -65,10 +76,37 @@ private void init(Context context){
65
76
log .warn ("exception on {} {}" , jar , e .getMessage ());
66
77
}
67
78
}
68
- typeSolver .add (new ReflectionTypeSolver (false ));
79
+ typeSolver .add (new ReflectionTypeSolver ());
69
80
70
81
parserConfiguration = new ParserConfiguration ();
71
82
parserConfiguration .setSymbolResolver (new JavaSymbolSolver (typeSolver ));
83
+
84
+ ParserStrategy strategy = loadParserStrategy ();
85
+ strategy .onLoad ();
86
+ visitorParser .setParserStrategy (strategy );
87
+
88
+ }
89
+
90
+ /**
91
+ * 加载并设置解析框架
92
+ * null时,使用读取到的第一个框架解析器
93
+ * 找不到时,报错
94
+ */
95
+ private ParserStrategy loadParserStrategy (){
96
+ ServiceLoader <ParserStrategy > serviceLoader = ServiceLoader .load (ParserStrategy .class );
97
+ List <ParserStrategy > strategies = Lists .newArrayList (serviceLoader );
98
+ if (strategies .isEmpty ()){
99
+ throw new IllegalArgumentException ("no com.apigcc.core.parser.ParserStrategy implements found" );
100
+ }
101
+ if (StringHelper .isBlank (context .framework )){
102
+ return strategies .get (0 );
103
+ }
104
+ for (ParserStrategy strategy : strategies ) {
105
+ if (Objects .equals (context .framework ,strategy .name ())){
106
+ return strategy ;
107
+ }
108
+ }
109
+ throw new IllegalArgumentException ("no com.apigcc.core.parser.ParserStrategy implements found for " +context .framework );
72
110
}
73
111
74
112
/**
@@ -100,7 +138,4 @@ public void render(){
100
138
}
101
139
}
102
140
103
- public Context getContext () {
104
- return context ;
105
- }
106
141
}
0 commit comments