Skip to content

Commit 9263b61

Browse files
author
wangshuwen15222
committed
feat:apt扫描路由增加重复报错逻辑
1 parent a3b4852 commit 9263b61

File tree

7 files changed

+98
-14
lines changed

7 files changed

+98
-14
lines changed

app/src/main/java/cn/cheney/app/MainActivity.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@ public void notFound() {
5858
book.name = "Kotlin";
5959
List<Book> bookList = new ArrayList<>();
6060
bookList.add(book);
61-
Boolean setSuccess = XRouter.<Boolean>method("moduleA/setBookList?bookList="
62-
+ JSON.toJSONString(bookList) + "&strValue=strValue")
61+
Boolean setSuccess = XRouter.<Boolean>method("moduleA/setBookInfo?bookList="
62+
+ JSON.toJSONString(bookList))
6363
.call();
6464
AlertUtil.showAlert(MainActivity.this, "" + setSuccess);
6565
});
6666

67-
//路由执行异步方法
67+
//路由执行异步方法+测试uri encode逻辑
6868
findViewById(R.id.btn3).setOnClickListener(v -> {
6969
Book book = new Book();
7070
book.name = "Kotlin";
7171
//测试urlEncode参数
72-
XRouter.<Book>method("moduleA/setBookInfo?info=%7B%22key%22%3A%20%22%26info2%3D123%22%7D")
72+
XRouter.<Book>method("moduleA/asyncSetBookName?info=%7B%22key%22%3A%20%22%26info2%3D123%22%7D")
7373
.put("book", book).call(new RouteCallback() {
7474
@Override
7575
public void onResult(Map<String, Object> result) {

gradle.properties

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ android.useAndroidX=true
1818
# Automatically convert third-party libraries to use AndroidX
1919
android.enableJetifier=true
2020

21-
debug = false
21+
debug = true
2222
pluginVersion = 1.0.6
23-
annotationVersion = 1.0.6
24-
compilerVersion = 1.0.7
25-
coreVersion = 1.0.7
23+
annotationVersion = 1.0.8
24+
compilerVersion = 1.0.8
25+
coreVersion = 1.0.8

m_test/src/main/java/cn/cheney/mtest/activity/ActivityA.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import cn.cheney.xrouter.annotation.XRoute;
2020
import cn.cheney.xrouter.core.XRouter;
2121

22-
@XRoute(module = "moduleA", path = {"page/a","page/a/copy"})
22+
@XRoute(module = "moduleA", path = {"page/a"})
2323
public class ActivityA extends AppCompatActivity {
2424
@XParam()
2525
Book book;

m_test/src/main/java/cn/cheney/mtest/module/ModuleA.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ public class ModuleA {
2020

2121
private static final String TAG = ModuleA.class.getSimpleName();
2222

23-
@XMethod(name = "setBookInfo")
24-
public static void getAsyncBookName(@XParam(name = XParam.RequestId) String requestId,
23+
@XMethod(name = "asyncSetBookName")
24+
public static void asyncSetBookName(@XParam(name = XParam.RequestId) String requestId,
2525
@XParam(name = "info") Map<String, String> mapInfo) {
2626
Log.i(TAG, "setBookInfo requestId =" + requestId + " mapInfo=" + mapInfo);
2727
HashMap<String, Object> map = new HashMap<>();
2828
map.put("result", "success");
2929
XRouter.getInstance().invokeCallback(requestId, map);
3030
}
3131

32+
3233
@XMethod(name = "setBookInfo")
33-
public static Boolean setBookList(Context context, List<Book> bookList, String strValue) {
34+
public static Boolean setBookList(Context context, List<Book> bookList) {
3435
Log.i(TAG, "setBookList bookList=" + JSON.toJSONString(bookList));
35-
Log.i(TAG, "setBookList strValue=" + strValue);
3636
return true;
3737
}
3838

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cn.cheney.mtest.module;
2+
3+
import android.content.Context;
4+
import android.util.Log;
5+
6+
import com.alibaba.fastjson.JSON;
7+
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
import cn.cheney.mtest.entity.Book;
13+
import cn.cheney.xrouter.annotation.XMethod;
14+
import cn.cheney.xrouter.annotation.XParam;
15+
import cn.cheney.xrouter.annotation.XRoute;
16+
import cn.cheney.xrouter.core.XRouter;
17+
18+
@XRoute(module = "moduleB")
19+
public class ModuleB {
20+
21+
private static final String TAG = ModuleB.class.getSimpleName();
22+
23+
@XMethod(name = "asyncSetBookName")
24+
public static void asyncSetBookName(@XParam(name = XParam.RequestId) String requestId,
25+
@XParam(name = "info") Map<String, String> mapInfo) {
26+
Log.i(TAG, "setBookInfo requestId =" + requestId + " mapInfo=" + mapInfo);
27+
HashMap<String, Object> map = new HashMap<>();
28+
map.put("result", "success");
29+
XRouter.getInstance().invokeCallback(requestId, map);
30+
}
31+
32+
@XMethod(name = "setBookInfo")
33+
public static Boolean setBookList(Context context, List<Book> bookList, String strValue) {
34+
Log.i(TAG, "setBookList bookList=" + JSON.toJSONString(bookList));
35+
Log.i(TAG, "setBookList strValue=" + strValue);
36+
return true;
37+
}
38+
39+
40+
}
41+

xrouter_compiler/src/main/java/cn/cheney/xrouter/compiler/XRouterProcessor.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import javax.lang.model.SourceVersion;
2121
import javax.lang.model.element.Element;
2222
import javax.lang.model.element.ElementKind;
23+
import javax.lang.model.element.ExecutableElement;
2324
import javax.lang.model.element.TypeElement;
2425
import javax.lang.model.element.VariableElement;
2526
import javax.lang.model.util.Elements;
@@ -41,6 +42,7 @@ public class XRouterProcessor extends AbstractProcessor {
4142

4243
private Holder holder;
4344
private Map<String, ModuleClassGenerator> moduleMap = new HashMap<>();
45+
private Set<String> routeSet = new HashSet<>();
4446
private Map<TypeElement, List<Element>> paramsMap = new HashMap<>();
4547

4648
@Override
@@ -180,7 +182,12 @@ private void processRoute(RoundEnvironment roundEnvironment) {
180182
} else {
181183
groupClassGenerator = moduleMap.get(route.module());
182184
}
183-
groupClassGenerator.generateSeg(typeElement, route.path());
185+
String[] paths = route.path();
186+
if (checkUriRepeat(typeElement, module, paths)) {
187+
return;
188+
}
189+
groupClassGenerator.generateSeg(typeElement, paths);
190+
184191
}
185192
for (Map.Entry<String, ModuleClassGenerator> entry : moduleMap.entrySet()) {
186193
ModuleClassGenerator moduleClassGenerator = entry.getValue();
@@ -189,6 +196,40 @@ private void processRoute(RoundEnvironment roundEnvironment) {
189196
}
190197

191198

199+
private boolean checkUriRepeat(TypeElement classType, String module, String[] activityRoutePaths) {
200+
for (String path : activityRoutePaths) {
201+
String uri = module + "/" + path;
202+
if (routeSet.contains(uri)) {
203+
Logger.e("the page uri=" + uri + " is repeat !!! please check class="
204+
+ classType.getQualifiedName());
205+
return true;
206+
}
207+
routeSet.add(uri);
208+
}
209+
for (Element element : classType.getEnclosedElements()) {
210+
if (element instanceof ExecutableElement) {
211+
ExecutableElement executableElement = (ExecutableElement) element;
212+
XMethod xMethod = executableElement.getAnnotation(XMethod.class);
213+
if (null == xMethod) {
214+
continue;
215+
}
216+
String methodPath = xMethod.name();
217+
if (methodPath.isEmpty()) {
218+
continue;
219+
}
220+
String methodUri = module + "/" + methodPath;
221+
if (routeSet.contains(methodUri)) {
222+
Logger.e("the uri=" + methodUri + " is repeat !!! please check in method="
223+
+ executableElement.getSimpleName() + " class="
224+
+ classType.getQualifiedName());
225+
return true;
226+
}
227+
routeSet.add(methodUri);
228+
}
229+
}
230+
return false;
231+
}
232+
192233
private void processInterceptor(RoundEnvironment roundEnvironment) {
193234
Set<? extends Element> elementSet = roundEnvironment.getElementsAnnotatedWith(XInterceptor.class);
194235
if (null == elementSet || elementSet.isEmpty()) {

xrouter_compiler/src/main/java/cn/cheney/xrouter/compiler/generator/ModuleClassGenerator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,13 @@ private MethodSpec.Builder loadMethodBuilder() {
9393
public void generateSeg(TypeElement classType, String[] paths) {
9494
TypeMirror typeActivity = holder.elementUtils
9595
.getTypeElement(XTypeMirror.ACTIVITY).asType();
96+
//页面路由
9697
if (holder.types.isSubtype(classType.asType(), typeActivity)) {
9798
for (String path : paths) {
9899
addActivityInvoke(classType, path);
99100
}
100101
}
102+
//方法路由
101103
for (Element element : classType.getEnclosedElements()) {
102104
if (element instanceof ExecutableElement) {
103105
ExecutableElement executableElement = (ExecutableElement) element;

0 commit comments

Comments
 (0)