ARouter
ARouter copied to clipboard
关于PathReplaceService实现的一个bug(烦请大佬帮忙确认)
发现PathReplaceService实现有一个逻辑错误。
假设一个特殊case。我们需要 path2 跳转到 path1,同时 path1 跳转到 path2。
使用PathReplaceService来实现的话,用 build(Uri) 方式是正常的,用 build(String) 方式就会错误。
比如在 Arouter 官方 Demo 项目增加一个 HelloPathReplaceService.java
package com.alibaba.android.arouter.demo.testservice;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.facade.service.PathReplaceService;
@Route(path = "/yourservicegroupname/pathreplace")
public class HelloPathReplaceService implements PathReplaceService {
Context mContext;
@Override
public void init(Context context) {
mContext = context;
}
@Override
public String forString(String path) {
Log.e("/test", "forString " + path);
if ("/test/activity1".equals(path)) {
path = "/test/activity2";
} else if ("/test/activity2".equals(path)) {
path = "/test/activity1";
}
return path;
}
@Override
public Uri forUri(Uri uri) {
Log.e("/test", "forUri " + uri);
if ("arouter://m.aliyun.com/test/activity1".equals(uri.toString())) {
uri = Uri.parse("arouter://m.aliyun.com/test/activity2");
} else if ("arouter://m.aliyun.com/test/activity2".equals(uri.toString())) {
uri = Uri.parse("arouter://m.aliyun.com/test/activity1");
}
return uri;
}
}
就可以出现这个问题了。找了一下源码,发现这段代码特别可疑,会导致 forString 被执行 2 次(因为build(path)会调用build(path,group)导致)。而 forUri 只会执行1次。按理说,执行1次才是对的。
/**
* Build postcard by path and default group
*/
protected Postcard build(String path) {
if (TextUtils.isEmpty(path)) {
throw new HandlerException(Consts.TAG + "Parameter is invalid!");
} else {
PathReplaceService pService = ARouter.getInstance().navigation(PathReplaceService.class);
if (null != pService) {
path = pService.forString(path);
}
return build(path, extractGroup(path));
}
}
/**
* Build postcard by uri
*/
protected Postcard build(Uri uri) {
if (null == uri || TextUtils.isEmpty(uri.toString())) {
throw new HandlerException(Consts.TAG + "Parameter invalid!");
} else {
PathReplaceService pService = ARouter.getInstance().navigation(PathReplaceService.class);
if (null != pService) {
uri = pService.forUri(uri);
}
return new Postcard(uri.getPath(), extractGroup(uri.getPath()), uri, null);
}
}
/**
* Build postcard by path and group
*/
protected Postcard build(String path, String group) {
if (TextUtils.isEmpty(path) || TextUtils.isEmpty(group)) {
throw new HandlerException(Consts.TAG + "Parameter is invalid!");
} else {
PathReplaceService pService = ARouter.getInstance().navigation(PathReplaceService.class);
if (null != pService) {
path = pService.forString(path);
}
return new Postcard(path, group);
}
}
虽然实际需求里面这种特殊case比较少,我只想到2类case会出问题
- path1=>path2=>path1
- path1=>path2=>path3
但是执行2次,难免会引入其他未知异常,还是希望能够被解答一下。谢谢。
是有这个问题,build 里面 replace 重复了
fixed 6909860a10ee3caa907d57f17759caf5eb06485b