Skip to content

Commit d9b2200

Browse files
committed
[anno] [add auto-complete mapper config for UiRoute, but isn't graceful]
1 parent 941c44d commit d9b2200

File tree

12 files changed

+431
-58
lines changed

12 files changed

+431
-58
lines changed

componentlib/src/main/java/com/mrzhang/component/componentlib/router/RouterConstants.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,7 @@
1010

1111
public class RouterConstants {
1212
public static final String SDK_NAME = "Router";
13-
public static final String TAG = SDK_NAME + "::";
1413
public static final String SEPARATOR = "$$";
15-
public static final String SUFFIX_ROOT = "Root";
16-
public static final String SUFFIX_INTERCEPTORS = "Interceptors";
17-
public static final String SUFFIX_PROVIDERS = "Providers";
1814
public static final String SUFFIX_AUTOWIRED = SEPARATOR + SDK_NAME + SEPARATOR + "Autowired";
19-
public static final String DOT = ".";
2015

21-
// public static final String ROUTE_ROOT_PAKCAGE = "com.alibaba.android.arouter.routes";
22-
//
23-
// public static final String AROUTER_SP_CACHE_KEY = "SP_AROUTER_CACHE";
24-
// public static final String AROUTER_SP_KEY_MAP = "ROUTER_MAP";
25-
//
26-
// public static final String LAST_VERSION_NAME = "LAST_VERSION_NAME";
27-
// public static final String LAST_VERSION_CODE = "LAST_VERSION_CODE";
2816
}

router-anno-compiler/src/main/java/com/ljsw/router/compiler/processor/RouterDefProcessor.java

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
package com.ljsw.router.compiler.processor;
2+
3+
import com.google.auto.service.AutoService;
4+
import com.ljsw.router.compiler.utils.Logger;
5+
import com.ljsw.router.facade.annotation.RouteNode;
6+
import com.ljsw.router.facade.annotation.Router;
7+
import com.ljsw.router.facade.enums.NodeType;
8+
import com.ljsw.router.facade.model.Node;
9+
import com.squareup.javapoet.ClassName;
10+
import com.squareup.javapoet.JavaFile;
11+
import com.squareup.javapoet.MethodSpec;
12+
import com.squareup.javapoet.ParameterSpec;
13+
import com.squareup.javapoet.ParameterizedTypeName;
14+
import com.squareup.javapoet.TypeSpec;
15+
16+
import org.apache.commons.collections4.CollectionUtils;
17+
18+
import java.io.IOException;
19+
import java.util.ArrayList;
20+
import java.util.HashMap;
21+
import java.util.List;
22+
import java.util.Map;
23+
import java.util.Set;
24+
25+
import javax.annotation.processing.AbstractProcessor;
26+
import javax.annotation.processing.Filer;
27+
import javax.annotation.processing.ProcessingEnvironment;
28+
import javax.annotation.processing.Processor;
29+
import javax.annotation.processing.RoundEnvironment;
30+
import javax.annotation.processing.SupportedAnnotationTypes;
31+
import javax.annotation.processing.SupportedOptions;
32+
import javax.annotation.processing.SupportedSourceVersion;
33+
import javax.lang.model.SourceVersion;
34+
import javax.lang.model.element.Element;
35+
import javax.lang.model.element.TypeElement;
36+
import javax.lang.model.type.MirroredTypeException;
37+
import javax.lang.model.type.TypeMirror;
38+
import javax.lang.model.util.Elements;
39+
import javax.lang.model.util.Types;
40+
41+
import static com.ljsw.router.compiler.utils.Constants.ACTIVITY;
42+
import static com.ljsw.router.compiler.utils.Constants.ANNOTATION_TYPE_ROUTER;
43+
import static com.ljsw.router.compiler.utils.Constants.ANNOTATION_TYPE_ROUTE_NODE;
44+
import static com.ljsw.router.compiler.utils.Constants.KEY_MODULE_NAME;
45+
import static com.ljsw.router.compiler.utils.Constants.ROUTER_UTIL_METHOD_ADDTO;
46+
import static com.ljsw.router.compiler.utils.Constants.TYPE_UIROUTER_LOADER;
47+
import static javax.lang.model.element.Modifier.PUBLIC;
48+
49+
/**
50+
* <p><b>Package:</b> com.ljsw.router.compiler.processor </p>
51+
* <p><b>Project:</b> DDComponentForAndroid </p>
52+
* <p><b>Classname:</b> RouterProcessor </p>
53+
* <p><b>Description:</b> generate RouterLoader class for 'Router' annotated class,
54+
* parse 'RouteNode' annotated Activities to mapper
55+
* </p>
56+
* Created by leobert on 2017/9/18.
57+
*/
58+
@AutoService(Processor.class)
59+
@SupportedOptions(KEY_MODULE_NAME)
60+
@SupportedSourceVersion(SourceVersion.RELEASE_7)
61+
@SupportedAnnotationTypes({ANNOTATION_TYPE_ROUTE_NODE, ANNOTATION_TYPE_ROUTER})
62+
public class RouterProcessor extends AbstractProcessor {
63+
private Logger logger;
64+
65+
private Filer mFiler; // File util, write class file into disk.
66+
private Types types;
67+
private Elements elements;
68+
69+
private Map<String, TypeMirror> routers;
70+
private Map<String, List<Node>> routerNodes;
71+
72+
@Override
73+
public synchronized void init(ProcessingEnvironment processingEnv) {
74+
super.init(processingEnv);
75+
76+
routers = new HashMap<>();
77+
routerNodes = new HashMap<>();
78+
79+
mFiler = processingEnv.getFiler(); // Generate class.
80+
types = processingEnv.getTypeUtils(); // Get type utils.
81+
elements = processingEnv.getElementUtils(); // Get class meta.
82+
83+
logger = new Logger(processingEnv.getMessager()); // Package the log utils.
84+
85+
logger.info(">>> RouteProcessor init. <<<");
86+
}
87+
88+
@Override
89+
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
90+
if (CollectionUtils.isNotEmpty(set)) {
91+
Set<? extends Element> routeService =
92+
roundEnvironment.getElementsAnnotatedWith(Router.class);
93+
try {
94+
logger.info(">>> Found routeService <<<");
95+
this.foundRouters(routeService);
96+
} catch (Exception e) {
97+
logger.error(e);
98+
}
99+
100+
101+
Set<? extends Element> routeNodes = roundEnvironment.getElementsAnnotatedWith(RouteNode.class);
102+
try {
103+
logger.info(">>> Found routes, start... <<<");
104+
this.parseRouteNodes(routeNodes);
105+
} catch (Exception e) {
106+
logger.error(e);
107+
}
108+
109+
this.write2Util();
110+
111+
return true;
112+
}
113+
114+
return false;
115+
}
116+
117+
private void write2Util() {
118+
Set<String> groups = routers.keySet();
119+
120+
ClassName type_UiRouterLoader = ClassName.get(elements.getTypeElement(TYPE_UIROUTER_LOADER));
121+
for (String group : groups) {
122+
logger.info(">>> write for group:" + group);
123+
124+
TypeMirror value = routers.get(group);
125+
String path = value.toString();
126+
127+
String pkg = path.substring(0, path.lastIndexOf("."));
128+
String cn = path.substring(path.lastIndexOf(".") + 1) + "Loader";
129+
130+
ParameterizedTypeName inputMapTypeOfGroup = ParameterizedTypeName.get(
131+
ClassName.get(Map.class),
132+
ClassName.get(String.class),
133+
ClassName.get(Class.class)
134+
);
135+
136+
ParameterSpec groupParamSpec =
137+
ParameterSpec.builder(inputMapTypeOfGroup, "mapper").build();
138+
139+
140+
MethodSpec.Builder loadIntoMethodOfRootBuilder =
141+
MethodSpec.methodBuilder(ROUTER_UTIL_METHOD_ADDTO)
142+
.addParameter(groupParamSpec)
143+
.addAnnotation(Override.class)
144+
.addModifiers(PUBLIC);
145+
146+
List<Node> nodes = routerNodes.get(group);
147+
for (Node node : nodes) {
148+
loadIntoMethodOfRootBuilder.addStatement(
149+
"mapper.put($S,$T.class)",
150+
node.getPath(),
151+
ClassName.get((TypeElement) node.getRawType()));
152+
}
153+
154+
155+
try {
156+
JavaFile.builder(pkg, TypeSpec.classBuilder(cn)
157+
.addModifiers(PUBLIC)
158+
.addSuperinterface(type_UiRouterLoader)
159+
.addMethod(loadIntoMethodOfRootBuilder.build())
160+
.build()
161+
).build().writeTo(mFiler);
162+
} catch (IOException e) {
163+
e.printStackTrace();
164+
}
165+
}
166+
}
167+
168+
private void parseRouteNodes(Set<? extends Element> routeElements) {
169+
170+
TypeMirror type_Activity = elements.getTypeElement(ACTIVITY).asType();
171+
172+
for (Element element : routeElements) {
173+
TypeMirror tm = element.asType();
174+
RouteNode route = element.getAnnotation(RouteNode.class);
175+
176+
if (types.isSubtype(tm, type_Activity)) { // Activity
177+
logger.info(">>> Found activity route: " + tm.toString() + " <<<");
178+
179+
String group = route.group();
180+
181+
Node node = new Node();
182+
node.setPath(route.path());
183+
node.setPriority(route.priority());
184+
node.setNodeType(NodeType.ACTIVITY);
185+
node.setRawType(element);
186+
187+
if (routerNodes.containsKey(group)) {
188+
routerNodes.get(group).add(node);
189+
} else {
190+
ArrayList<Node> nodes = new ArrayList<>();
191+
nodes.add(node);
192+
routerNodes.put(group, nodes);
193+
}
194+
} else {
195+
throw new IllegalStateException("only activity can be annotated by RouteNode");
196+
}
197+
}
198+
}
199+
200+
private void foundRouters(Set<? extends Element> routers) {
201+
for (Element element : routers) {
202+
Router router = element.getAnnotation(Router.class);
203+
String group = router.group();
204+
if (this.routers.containsKey(group))
205+
throw new IllegalStateException("duplicated group at annotation," +
206+
"please check group:" + group);
207+
208+
//use exception to get path
209+
TypeMirror value = null;
210+
try {
211+
router.classPath();
212+
} catch (MirroredTypeException mte) {
213+
value = mte.getTypeMirror();
214+
}
215+
216+
this.routers.put(group, value);
217+
}
218+
}
219+
}

router-anno-compiler/src/main/java/com/ljsw/router/compiler/utils/Constants.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public interface Constants {
1818
///////////////////////////////////////////////////////////////////////////
1919
String KEY_MODULE_NAME = "ModuleName";
2020

21-
String ANNOTATION_TYPE_ROUTE = ANNO_FACADE_PKG + ".annotation.Route";
21+
String ANNOTATION_TYPE_ROUTE_NODE = ANNO_FACADE_PKG + ".annotation.RouteNode";
22+
String ANNOTATION_TYPE_ROUTER = ANNO_FACADE_PKG + ".annotation.Router";
2223
String ANNOTATION_TYPE_AUTOWIRED = ANNO_FACADE_PKG + ".annotation.Autowired";
2324

2425
String PREFIX_OF_LOGGER = "[Router-Anno-Compiler]-- ";
@@ -42,13 +43,12 @@ public interface Constants {
4243
String BOOLEAN = LANG + ".Boolean";
4344
String STRING = LANG + ".String";
4445

45-
// String TEMPLATE_PACKAGE = ".template";
46-
47-
// /**
48-
// * see at {@link com.mrzhang.component.componentlib.router.facade.ISyringe}
49-
// */
5046
String ISYRINGE = "com.mrzhang.component.componentlib.router.facade.ISyringe";
5147

5248
String JSON_SERVICE = "com.mrzhang.componentservice.json.JsonService";
49+
50+
String ROUTER_UTIL_METHOD_ADDTO = "addTo";
51+
52+
String TYPE_UIROUTER_LOADER = "com.ljsw.router.facade.IUiRouterLoader";
5353

5454
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.ljsw.router.facade;
2+
3+
import java.util.Map;
4+
5+
/**
6+
* <p><b>Package:</b> com.ljsw.router.facade </p>
7+
* <p><b>Project:</b> DDComponentForAndroid </p>
8+
* <p><b>Classname:</b> IUiRouterUtil </p>
9+
* <p><b>Description:</b> used to add route mapper </p>
10+
* Created by leobert on 2017/9/19.
11+
*/
12+
13+
public interface IUiRouterLoader {
14+
void addTo(Map<String, Class> atlas);
15+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.ljsw.router.facade;
2+
3+
import java.util.Map;
4+
5+
/**
6+
* <p><b>Package:</b> com.ljsw.router.facade </p>
7+
* <p><b>Project:</b> DDComponentForAndroid </p>
8+
* <p><b>Classname:</b> UiRouterMapperUtils </p>
9+
* <p><b>Description:</b> used to fetch mapper from generated XXLoader class for XX </p>
10+
* Created by leobert on 2017/9/19.
11+
*/
12+
13+
public class UiRouterMapperUtils {
14+
public static void fetchRouteForMe(Class clz, Map<String, Class> mapper) {
15+
if (clz == null)
16+
throw new IllegalArgumentException("give me your component-uirouter class");
17+
if (mapper == null)
18+
throw new IllegalArgumentException("mapper cannot be null!");
19+
20+
String name = clz.getName() + "Loader";
21+
try {
22+
Class util = Class.forName(name);
23+
IUiRouterLoader instance = (IUiRouterLoader) util.newInstance();
24+
instance.addTo(mapper);
25+
} catch (ClassNotFoundException e) {
26+
e.printStackTrace();
27+
} catch (InstantiationException e) {
28+
e.printStackTrace();
29+
} catch (IllegalAccessException e) {
30+
e.printStackTrace();
31+
}
32+
33+
}
34+
}

router-annotation/src/main/java/com/ljsw/router/facade/annotation/RouteDef.java renamed to router-annotation/src/main/java/com/ljsw/router/facade/annotation/RouteNode.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
* <p><b>Package:</b> com.ljsw.router.facade.annotation </p>
1010
* <p><b>Project:</b> DDComponentForAndroid </p>
1111
* <p><b>Classname:</b> RouteDef </p>
12-
* <p><b>Description:</b> used to decline a route </p>
12+
* <p><b>Description:</b> used to decline a route node</p>
1313
* Created by leobert on 2017/9/18.
1414
*/
1515
@Target({ElementType.TYPE})
1616
@Retention(RetentionPolicy.CLASS)
17-
public @interface RouteDef {
17+
public @interface RouteNode {
1818
/**
1919
* path of one route
2020
*/
@@ -26,18 +26,15 @@
2626
* e.g.
2727
* '/user/login' and '/user/register' can be merger into group 'user'
2828
*
29+
* we have departed the whole system to different component,thus it is useless
30+
* to define group for merge router
31+
*
32+
* it will be removed when route develop complete.
33+
*
34+
* emmm.... 暂时留存该设计
2935
*/
30-
String group() default "";
31-
32-
/**
33-
* Name of route, used to generate javadoc.
34-
*/
35-
String name() default "undefined";
36+
String group() default "default";
3637

37-
/**
38-
* Extra data, can be set by user.
39-
*/
40-
int extras() default Integer.MIN_VALUE;
4138

4239
/**
4340
* The priority of route.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.ljsw.router.facade.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* <p><b>Package:</b> com.ljsw.router.facade.annotation </p>
10+
* <p><b>Project:</b> DDComponentForAndroid </p>
11+
* <p><b>Classname:</b> Routes </p>
12+
* <p><b>Description:</b> use to def the router which dispatches requests </p>
13+
* Created by leobert on 2017/9/19.
14+
*/
15+
@Target({ElementType.TYPE})
16+
@Retention(RetentionPolicy.CLASS)
17+
public @interface Router {
18+
Class classPath();
19+
String group() default "default";
20+
}

0 commit comments

Comments
 (0)