Skip to content

Commit 19fa046

Browse files
author
mrzhang
committed
auto generate HostUIRoutertable.txt
1 parent bd9f4fb commit 19fa046

File tree

12 files changed

+197
-24
lines changed

12 files changed

+197
-24
lines changed

UIRouterTable/AppRouterTable.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
auto generated, do not change !!!!
2+
3+
HOST : app
4+
5+
首页
6+
/main
7+

UIRouterTable/ShareRouterTable.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
auto generated, do not change !!!!
2+
3+
HOST : share
4+
5+
分享杂志页面
6+
/shareMagazine
7+
author:com.luojilab.componentservice.share.bean.Author
8+
bookName:String
9+
10+
分享书籍页面
11+
/shareBook
12+
author:com.luojilab.componentservice.share.bean.Author
13+
bookName:String
14+

app/src/main/java/com/luojilab/componentdemo/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import com.luojilab.componentservice.readerbook.ReadBookService;
1414
import com.luojilab.router.facade.annotation.RouteNode;
1515

16-
@RouteNode(path = "/main")
16+
@RouteNode(path = "/main", desc = "首页")
1717
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
1818

1919
Fragment fragment;

componentlib/src/main/java/com/luojilab/component/componentlib/router/ui/BaseCompRouter.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,14 @@
1515
import java.util.List;
1616
import java.util.Map;
1717

18-
public class BaseCompRouter implements IComponentRouter {
18+
public abstract class BaseCompRouter implements IComponentRouter {
1919
protected Map<String, Class> routeMapper = new HashMap<String, Class>();
2020

2121
protected Map<Class, Map<String, Integer>> paramsMapper = new HashMap<>();
2222

23-
protected String HOST = "";
24-
25-
protected boolean hasInitHost = false;
2623
protected boolean hasInitMap = false;
2724

28-
protected void initHost() {
29-
hasInitHost = true;
30-
}
25+
protected abstract String getHost();
3126

3227
protected void initMap() {
3328
hasInitMap = true;
@@ -64,7 +59,7 @@ public boolean openUri(Context context, Uri uri, Bundle bundle, Integer requestC
6459
}
6560
String scheme = uri.getScheme();
6661
String host = uri.getHost();
67-
if (!HOST.equals(host)) {
62+
if (!getHost().equals(host)) {
6863
return false;
6964
}
7065
List<String> pathSegments = uri.getPathSegments();
@@ -91,11 +86,8 @@ public boolean openUri(Context context, Uri uri, Bundle bundle, Integer requestC
9186

9287
@Override
9388
public boolean verifyUri(Uri uri) {
94-
if (!hasInitHost) {
95-
initHost();
96-
}
9789
String host = uri.getHost();
98-
if (!HOST.equals(host)) {
90+
if (!getHost().equals(host)) {
9991
return false;
10092
}
10193
if (!hasInitMap) {

componentlib/src/main/java/com/luojilab/component/componentlib/utils/UriUtils.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ public static void setBundleValue(Bundle bundle, Integer typeDef, String key, St
119119
} else if (typeDef == Type.STRING.ordinal()) {
120120
bundle.putString(key, value);
121121
} else if (typeDef == Type.PARCELABLE.ordinal()) {
122-
// TODO : How to description parcelable value with string?
123-
// bundle.putParcelable(key, );
124122
} else if (typeDef == Type.OBJECT.ordinal()) {
125123
bundle.putString(key, value);
126124
} else {

router-anno-compiler/src/main/java/com/luojilab/router/compiler/processor/RouterProcessor.java

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.luojilab.router.compiler.processor;
22

33
import com.google.auto.service.AutoService;
4+
import com.luojilab.router.compiler.utils.FileUtils;
45
import com.luojilab.router.compiler.utils.Logger;
56
import com.luojilab.router.compiler.utils.TypeUtils;
67
import com.luojilab.router.facade.annotation.Autowired;
@@ -71,6 +72,8 @@ public class RouterProcessor extends AbstractProcessor {
7172
private Types types;
7273
private Elements elements;
7374

75+
private TypeMirror type_String;
76+
7477
private ArrayList<Node> routerNodes;
7578
private TypeUtils typeUtils;
7679
private String host = null;
@@ -86,13 +89,18 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
8689
elements = processingEnv.getElementUtils();
8790
typeUtils = new TypeUtils(types, elements);
8891

92+
type_String = elements.getTypeElement("java.lang.String").asType();
93+
8994
logger = new Logger(processingEnv.getMessager());
9095

9196
Map<String, String> options = processingEnv.getOptions();
9297
if (MapUtils.isNotEmpty(options)) {
9398
host = options.get(KEY_HOST_NAME);
9499
logger.info(">>> host is " + host + " <<<");
95100
}
101+
if (host == null || host.equals("")) {
102+
host = "default";
103+
}
96104
logger.info(">>> RouteProcessor init. <<<");
97105
}
98106

@@ -107,12 +115,42 @@ public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnv
107115
logger.error(e);
108116
}
109117
generateRouterImpl();
118+
generateRouterTable();
110119
return true;
111120
}
112121
return false;
113122
}
114123

124+
/**
125+
* generate HostRouterTable.txt
126+
*/
127+
private void generateRouterTable() {
128+
String fileName = RouteUtils.genRouterTable(host);
129+
if (FileUtils.createFile(fileName)) {
130+
131+
StringBuilder stringBuilder = new StringBuilder();
132+
stringBuilder.append("auto generated, do not change !!!! \n\n");
133+
stringBuilder.append("HOST : " + host + "\n\n");
134+
135+
for (Node node : routerNodes) {
136+
stringBuilder.append(node.getDesc() + "\n");
137+
stringBuilder.append(node.getPath() + "\n");
138+
Map<String, String> paramsType = node.getParamsDesc();
139+
if (MapUtils.isNotEmpty(paramsType)) {
140+
for (Map.Entry<String, String> types : paramsType.entrySet()) {
141+
stringBuilder.append(types.getKey() + ":" + types.getValue() + "\n");
142+
}
143+
}
144+
stringBuilder.append("\n");
145+
}
146+
FileUtils.writeStringToFile(fileName, stringBuilder.toString(), false);
147+
}
148+
}
149+
115150

151+
/**
152+
* generate HostUIRouter.java
153+
*/
116154
private void generateRouterImpl() {
117155

118156
String claName = RouteUtils.genHostUIRouterClass(host);
@@ -157,19 +195,24 @@ private void parseRouteNodes(Set<? extends Element> routeElements) {
157195
checkPath(path);
158196

159197
node.setPath(path);
198+
node.setDesc(route.desc());
160199
node.setPriority(route.priority());
161200
node.setNodeType(NodeType.ACTIVITY);
162201
node.setRawType(element);
163202

164203
Map<String, Integer> paramsType = new HashMap<>();
204+
Map<String, String> paramsDesc = new HashMap<>();
165205
for (Element field : element.getEnclosedElements()) {
166206
if (field.getKind().isField() && field.getAnnotation(Autowired.class) != null) {
167207
Autowired paramConfig = field.getAnnotation(Autowired.class);
168208
paramsType.put(StringUtils.isEmpty(paramConfig.name())
169209
? field.getSimpleName().toString() : paramConfig.name(), typeUtils.typeExchange(field));
210+
paramsDesc.put(StringUtils.isEmpty(paramConfig.name())
211+
? field.getSimpleName().toString() : paramConfig.name(), typeUtils.typeDesc(field));
170212
}
171213
}
172214
node.setParamsType(paramsType);
215+
node.setParamsDesc(paramsDesc);
173216

174217
if (!routerNodes.contains(node)) {
175218
routerNodes.add(node);
@@ -196,16 +239,14 @@ private void checkPath(String path) {
196239
* create init host method
197240
*/
198241
private MethodSpec generateInitHostMethod() {
199-
TypeName returnType = TypeName.VOID;
242+
TypeName returnType = TypeName.get(type_String);
200243

201-
MethodSpec.Builder openUriMethodSpecBuilder = MethodSpec.methodBuilder("initHost")
244+
MethodSpec.Builder openUriMethodSpecBuilder = MethodSpec.methodBuilder("getHost")
202245
.returns(returnType)
203246
.addAnnotation(Override.class)
204247
.addModifiers(Modifier.PUBLIC);
205248

206-
openUriMethodSpecBuilder.addStatement("super.initHost()");
207-
208-
openUriMethodSpecBuilder.addStatement("HOST = $S", host);
249+
openUriMethodSpecBuilder.addStatement("return $S", host);
209250

210251
return openUriMethodSpecBuilder.build();
211252
}
@@ -228,9 +269,7 @@ private MethodSpec generateInitMapMethod() {
228269
mRouteMapperFieldName + ".put($S,$T.class)",
229270
node.getPath(),
230271
ClassName.get((TypeElement) node.getRawType()));
231-
}
232272

233-
for (Node node : routerNodes) {
234273
// Make map body for paramsType
235274
StringBuilder mapBodyBuilder = new StringBuilder();
236275
Map<String, Integer> paramsType = node.getParamsType();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.luojilab.router.compiler.utils;
2+
3+
import java.io.BufferedWriter;
4+
import java.io.File;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.io.OutputStreamWriter;
8+
9+
/**
10+
* Created by mrzhang on 2017/12/20.
11+
*/
12+
13+
public class FileUtils {
14+
15+
16+
/**
17+
* @param fileName
18+
*/
19+
public static boolean createFile(String fileName) {
20+
try {
21+
File file = new File(fileName);
22+
if (!file.exists()) {
23+
File dir = new File(file.getParent());
24+
dir.mkdirs();
25+
file.createNewFile();
26+
}
27+
return true;
28+
} catch (Exception e) {
29+
e.printStackTrace();
30+
}
31+
return false;
32+
}
33+
34+
35+
/**
36+
* @param fileName
37+
* @param content
38+
*/
39+
public static void writeStringToFile(String fileName, String content, boolean append) {
40+
BufferedWriter out = null;
41+
try {
42+
out = new BufferedWriter(new OutputStreamWriter(
43+
new FileOutputStream(fileName, append)));
44+
out.write(content);
45+
} catch (Exception e) {
46+
e.printStackTrace();
47+
} finally {
48+
try {
49+
out.close();
50+
} catch (IOException e) {
51+
e.printStackTrace();
52+
}
53+
}
54+
}
55+
}

router-anno-compiler/src/main/java/com/luojilab/router/compiler/utils/TypeUtils.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,46 @@ public int typeExchange(Element element) {
6868
}
6969
}
7070
}
71+
72+
/**
73+
* DESC of type
74+
*
75+
* @param element Raw type
76+
* @return Type class of java
77+
*/
78+
public String typeDesc(Element element) {
79+
TypeMirror typeMirror = element.asType();
80+
81+
// Primitive
82+
if (typeMirror.getKind().isPrimitive()) {
83+
return element.asType().getKind().name();
84+
}
85+
86+
switch (typeMirror.toString()) {
87+
case Constants.BYTE:
88+
return "byte";
89+
case Constants.SHORT:
90+
return "short";
91+
case Constants.INTEGER:
92+
return "int";
93+
case Constants.LONG:
94+
return "long";
95+
case Constants.FLOAT:
96+
return "byte";
97+
case Constants.DOUBEL:
98+
return "double";
99+
case Constants.BOOLEAN:
100+
return "boolean";
101+
case Constants.STRING:
102+
return "String";
103+
default: // Other side, maybe the PARCELABLE or OBJECT.
104+
if (types.isSubtype(typeMirror, parcelableType)) { // PARCELABLE
105+
return "parcelable";
106+
} else { // For others
107+
return typeMirror.toString();
108+
}
109+
}
110+
}
111+
112+
71113
}

router-annotation/src/main/java/com/luojilab/router/facade/model/Node.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ public class Node {
1919
private Element rawType; // Raw type of route
2020
private Class<?> destination; // Destination
2121
private String path; // Path of route
22+
private String desc; // Desc of route
2223
private int priority = -1; // The smaller the number, the higher the priority
2324
private Map<String, Integer> paramsType;
25+
private Map<String, String> paramsDesc;
2426

2527
public NodeType getNodeType() {
2628
return nodeType;
@@ -69,4 +71,20 @@ public Map<String, Integer> getParamsType() {
6971
public void setParamsType(Map<String, Integer> paramsType) {
7072
this.paramsType = paramsType;
7173
}
74+
75+
public Map<String, String> getParamsDesc() {
76+
return paramsDesc;
77+
}
78+
79+
public void setParamsDesc(Map<String, String> paramsDesc) {
80+
this.paramsDesc = paramsDesc;
81+
}
82+
83+
public String getDesc() {
84+
return desc;
85+
}
86+
87+
public void setDesc(String desc) {
88+
this.desc = desc;
89+
}
7290
}

router-annotation/src/main/java/com/luojilab/router/facade/utils/RouteUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class RouteUtils {
1010
private static final String DOT = ".";
1111
private static final String UIROUTER = "UiRouter";
1212

13+
private static final String ROUTERTABLE = "RouterTable";
14+
1315
public static String fiestCharUpperCase(String str) {
1416
char[] ch = str.toCharArray();
1517
if (ch[0] >= 'a' && ch[0] <= 'z') {
@@ -24,4 +26,10 @@ public static String genHostUIRouterClass(String host) {
2426
}
2527

2628

29+
public static String genRouterTable(String host) {
30+
String claName = "./UIRouterTable/" + fiestCharUpperCase(host) + ROUTERTABLE + ".txt";
31+
return new String(claName);
32+
}
33+
34+
2735
}

0 commit comments

Comments
 (0)