Skip to content

Commit 7465d20

Browse files
committed
[anno] [change output path of the generated Router to com.ljsw.gen.router.{group}.{interfaceName}Impl]
1 parent 6624553 commit 7465d20

File tree

6 files changed

+49
-34
lines changed

6 files changed

+49
-34
lines changed

componentlib/src/main/java/com/mrzhang/component/componentlib/router/ui/UIRouter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.support.annotation.NonNull;
77
import android.text.TextUtils;
88

9+
import com.ljsw.router.facade.Constants;
910
import com.ljsw.router.facade.annotation.Router;
1011

1112
import java.lang.annotation.Annotation;
@@ -135,11 +136,12 @@ public static IComponentRouter fetch(@NonNull Class<? extends IComponentRouter>
135136
if (!clz.isInterface())
136137
throw new IllegalArgumentException("need a interface, but this isn't a interface:" + clz.getName());
137138

138-
Annotation router = clz.getAnnotation(Router.class);
139+
Router router = clz.getAnnotation(Router.class);
139140
if (router == null)
140141
throw new IllegalArgumentException("not annotated with Router:" + clz.getName());
141142

142-
String path = clz.getName() + "Impl";
143+
String path = Constants.ROUTERIMPL_OUTPUT_PKG +
144+
Constants.DOT + router.group() + Constants.DOT + clz.getSimpleName() + "Impl";
143145
if (routerInstanceCache.containsKey(path))
144146
return routerInstanceCache.get(path);
145147

router-anno-compiler/src/main/java/com/ljsw/router/compiler/model/HostInfo.java renamed to router-anno-compiler/src/main/java/com/ljsw/router/compiler/model/GroupInfo.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,26 @@
1010
* Created by leobert on 2017/9/25.
1111
*/
1212

13-
public class HostInfo {
13+
public class GroupInfo {
1414
private String host;
15-
private TypeMirror interfaceTypeMirror;
15+
private String outPutPath;
16+
private String interfacePath;
1617

17-
public HostInfo(String host, TypeMirror interfaceTypeMirror) {
18+
public GroupInfo(String host, String outPutPath, String interfacePath) {
1819
this.host = host;
19-
this.interfaceTypeMirror = interfaceTypeMirror;
20+
this.outPutPath = outPutPath;
21+
this.interfacePath = interfacePath;
2022
}
2123

2224
public String getHost() {
2325
return host;
2426
}
2527

28+
public String getOutPutPath() {
29+
return outPutPath;
30+
}
2631

27-
public TypeMirror getInterfaceTypeMirror() {
28-
return interfaceTypeMirror;
32+
public String getInterfacePath() {
33+
return interfacePath;
2934
}
3035
}

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

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.ljsw.router.compiler.processor;
22

33
import com.google.auto.service.AutoService;
4-
import com.ljsw.router.compiler.model.HostInfo;
4+
import com.ljsw.router.compiler.model.GroupInfo;
55
import com.ljsw.router.compiler.utils.AnnoUtils;
66
import com.ljsw.router.compiler.utils.Logger;
7+
import com.ljsw.router.facade.Constants;
78
import com.ljsw.router.facade.annotation.RouteNode;
89
import com.ljsw.router.facade.annotation.Router;
910
import com.ljsw.router.facade.enums.NodeType;
@@ -39,7 +40,6 @@
3940
import javax.lang.model.element.Element;
4041
import javax.lang.model.element.Modifier;
4142
import javax.lang.model.element.TypeElement;
42-
import javax.lang.model.type.MirroredTypeException;
4343
import javax.lang.model.type.TypeMirror;
4444
import javax.lang.model.util.Elements;
4545
import javax.lang.model.util.Types;
@@ -74,7 +74,7 @@ public class RouterProcessor extends AbstractProcessor {
7474
/**
7575
* (group,hostInfo)
7676
*/
77-
private Map<String, HostInfo> routers;
77+
private Map<String, GroupInfo> routers;
7878
/**
7979
* (group,List(Node))
8080
*/
@@ -152,22 +152,21 @@ private void generateRouterImpl() {
152152

153153
for (String group : groups) {
154154
logger.info(">>> write for group:" + group);
155-
HostInfo hostInfo = routers.get(group);
156-
String path = hostInfo.getInterfaceTypeMirror().toString();
155+
GroupInfo groupInfo = routers.get(group);
157156

158-
String claName = path + "Impl";
157+
String claName = groupInfo.getOutPutPath() ;
159158
//pkg
160159
String pkg = claName.substring(0, claName.lastIndexOf("."));
161160
//simpleName
162161
String cn = claName.substring(claName.lastIndexOf(".") + 1);
163162
// superInterface ClassName
164-
ClassName superInterface = ClassName.get(elements.getTypeElement(path));
165-
163+
ClassName superInterface = ClassName.get(elements.getTypeElement(groupInfo.getInterfacePath()));
166164

165+
logger.info(">>> :tag:");
167166
// private static Map<String,Class> routeMapper = new HashMap<String.Class>();
168167
FieldSpec routeMapperField = generateRouteMapperFieldSpec();
169168
//private static final String HOST = "xxx"
170-
FieldSpec hostField = generateHostFieldSpec(hostInfo.getHost());
169+
FieldSpec hostField = generateHostFieldSpec(groupInfo.getHost());
171170

172171

173172
/*
@@ -180,7 +179,7 @@ private void generateRouterImpl() {
180179

181180

182181
MethodSpec openUrl = generateOpenUri1();
183-
logger.info(">>> :tag:");
182+
184183
MethodSpec openUri = generateOpenUri2();
185184
MethodSpec verify = generateVerify();
186185

@@ -277,15 +276,11 @@ private void foundRouters(Set<? extends Element> routers) {
277276
throw new IllegalStateException("duplicated group at annotation," +
278277
"please check group:" + group);
279278

280-
//use exception to get path
281-
TypeMirror value = null;
282-
try {
283-
router.classPath();
284-
} catch (MirroredTypeException mte) {
285-
value = mte.getTypeMirror();
286-
}
279+
String outPutPath = Constants.ROUTERIMPL_OUTPUT_PKG +
280+
Constants.DOT + group + Constants.DOT + element.getSimpleName() + "Impl";
287281

288-
this.routers.put(group, new HostInfo(host, value));
282+
String interfacePath = ((TypeElement) element).getQualifiedName().toString();
283+
this.routers.put(group, new GroupInfo(host, outPutPath,interfacePath));
289284
}
290285
}
291286

@@ -315,7 +310,6 @@ private FieldSpec generateHostFieldSpec(String host) {
315310
.build();
316311
}
317312

318-
319313
private CodeBlock generateInitCodeBlock(String group) {
320314
CodeBlock.Builder initBlockBuilder = CodeBlock.builder();
321315

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.ljsw.router.facade;
2+
3+
/**
4+
* <p><b>Package:</b> com.ljsw.router.facade </p>
5+
* <p><b>Project:</b> DDComponentForAndroid </p>
6+
* <p><b>Classname:</b> Constants </p>
7+
* <p><b>Description:</b> Constants </p>
8+
* Created by leobert on 2017/9/25.
9+
*/
10+
11+
public class Constants {
12+
public static final String ROUTERIMPL_OUTPUT_PKG = "com.ljsw.gen.router";
13+
public static final String DOT = ".";
14+
}

router-annotation/src/main/java/com/ljsw/router/facade/annotation/Router.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
@Target({ElementType.TYPE})
1616
@Retention(RetentionPolicy.CLASS)
1717
public @interface Router {
18-
/**
19-
* will removed, it is too heavy and confusing
20-
* @return the interface class annotated with Router
21-
*/
22-
@Deprecated
23-
Class classPath();
18+
// /**
19+
// * will removed, it is too heavy and confusing
20+
// * @return the interface class annotated with Router
21+
// */
22+
// @Deprecated
23+
// Class classPath();
2424
String host();
2525
String group() default "default";
2626
}

sharecomponent/src/main/java/com/mrzhang/share/compouirouter/ShareUiRouter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
* <p><b>Description:</b> TODO </p>
1111
* Created by leobert on 2017/9/25.
1212
*/
13-
@Router(classPath = ShareUiRouter.class,host = "shareComponent",group = "share")
13+
@Router(host = "shareComponent",group = "share")
1414
public interface ShareUiRouter extends IComponentRouter {
1515
}

0 commit comments

Comments
 (0)