Skip to content

Commit dcc8040

Browse files
author
wangshuwen15222
committed
chore:fix compiler生成拦截器代码bug
1 parent 663f1c2 commit dcc8040

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

app/src/main/java/cn/cheney/app/TestInterceptorA.java renamed to m_test/src/main/java/cn/cheney/mtest/interceptor/TestInterceptorA.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package cn.cheney.app;
1+
package cn.cheney.mtest.interceptor;
22

33
import cn.cheney.xrouter.annotation.XInterceptor;
44
import cn.cheney.xrouter.core.interceptor.Chain;
55
import cn.cheney.xrouter.core.interceptor.RouterInterceptor;
66
import cn.cheney.xrouter.core.util.Logger;
77

8-
@XInterceptor(modules = {"moduleA"}, paths = {"moduleA/page/a"})
8+
@XInterceptor(modules = {"moduleA"}, priority = 101)
99
public class TestInterceptorA implements RouterInterceptor {
1010

1111
@Override
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.cheney.mtest.interceptor;
2+
3+
4+
import cn.cheney.xrouter.annotation.XInterceptor;
5+
import cn.cheney.xrouter.core.interceptor.Chain;
6+
import cn.cheney.xrouter.core.interceptor.RouterInterceptor;
7+
import cn.cheney.xrouter.core.util.Logger;
8+
9+
@XInterceptor(paths = {"moduleA/page/a"}, priority = 100)
10+
public class TestInterceptorB implements RouterInterceptor {
11+
12+
@Override
13+
public Object intercept(Chain chain) {
14+
Logger.d("TestInterceptorB url=" + chain.call().getUri().toString());
15+
return chain.proceed(chain.call());
16+
}
17+
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class InterceptorClassGenerator {
3131

3232
private final MethodSpec.Builder loadMethodBuilder;
3333

34+
private boolean hasBuildVar = false;
35+
3436
public InterceptorClassGenerator(XRouterProcessor.Holder holder) {
3537
this.generatorClassName = GenerateFileConstant.INTERCEPTOR_FILE_PREFIX +
3638
UUID.randomUUID().toString().replace("-", "");
@@ -50,10 +52,11 @@ public void generateSeg(XInterceptor xInterceptor, TypeElement typeElement) {
5052
}
5153
ParameterizedTypeName listInterceptorDescType = ParameterizedTypeName.
5254
get(ClassName.get(List.class), CLASSNAME_INTERCEPTOR_DESC);
55+
if (!hasBuildVar){
56+
loadMethodBuilder.addStatement("$T $L", listInterceptorDescType, "interceptorList");
57+
hasBuildVar= true;
58+
}
5359
for (String interceptorStr : allList) {
54-
if (allList.indexOf(interceptorStr) == 0){
55-
loadMethodBuilder.addStatement("$T $L", listInterceptorDescType, "interceptorList");
56-
}
5760
loadMethodBuilder.beginControlFlow("if (map.containsKey($S))", interceptorStr);
5861
loadMethodBuilder.addStatement("interceptorList = map.get($S)", interceptorStr);
5962
loadMethodBuilder.nextControlFlow("else");

xrouter_core/src/main/java/cn/cheney/xrouter/core/XRouter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static XRouter getInstance() {
7373
}
7474

7575
public static void init(Application context, String scheme) {
76-
if (sHasInit){
76+
if (sHasInit) {
7777
return;
7878
}
7979
sHasInit = true;

xrouter_core/src/main/java/cn/cheney/xrouter/core/interceptor/InterceptorManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public List<RouterInterceptor> getInterceptorList(String module, String path) {
5757
}
5858
}
5959
//排序
60-
Collections.sort(filterDescList, (o1, o2) -> o1.getPriority() - o2.getPriority());
60+
Collections.sort(filterDescList, (o1, o2) -> o2.getPriority() - o1.getPriority());
6161
List<RouterInterceptor> interceptorList = new ArrayList<>();
6262
synchronized (mLock) {
6363
for (InterceptorDesc interceptorDesc : filterDescList) {

0 commit comments

Comments
 (0)