Skip to content

Commit

Permalink
feat: 防止路由重复初始化
Browse files Browse the repository at this point in the history
  • Loading branch information
wzbos committed Jul 2, 2020
1 parent 61a5c22 commit 48f60a3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 11 deletions.
4 changes: 2 additions & 2 deletions rudolph-compiler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ dependencies {
annotationProcessor 'com.google.auto.service:auto-service:1.0-rc7'
implementation 'org.apache.commons:commons-lang3:3.5'
implementation 'org.apache.commons:commons-collections4:4.1'
implementation project(':rudolph-annotations')
// implementation "${publishedGroupId}:rudolph-annotations:${rudolph_version}"
// implementation project(':rudolph-annotations')
implementation "${publishedGroupId}:rudolph-annotations:${rudolph_version}"
}

apply from: '../install.gradle'
Expand Down
17 changes: 14 additions & 3 deletions rudolph/src/main/java/cn/wzbos/android/rudolph/Rudolph.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Rudolph {
private static List<RouteInfo> routes = new ArrayList<>();
private static Application app;
private static String scheme;
private static Boolean initialized = false;

public static Application getApplication() {
return app;
Expand All @@ -40,6 +41,9 @@ public static String getScheme() {
*/
public static void init(Application application) {
Log.v(TAG, "init");
if (initialized)
return;

app = application;
AssetManager assetManager = application.getResources().getAssets();
try {
Expand All @@ -53,16 +57,23 @@ public static void init(Application application) {
iGroupInstance.init(application);
}
} catch (ClassNotFoundException e) {
Log.e(TAG, "初始化\"" + className + "\"组件失败,请检查包名是否正确!");
e.printStackTrace();
Log.e(TAG, "初始化\"" + className + "\"组件失败,请检查包名是否正确!", e);
}
}
}
initialized = true;
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "路由初始化异常!", e);
}
}

/**
* 获取是否已初始化路由表
*/
public static Boolean isInitialized() {
return initialized;
}

public static void setScheme(String schemeStr) {
scheme = schemeStr;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cn.wzbos.android.rudolph.router;

import android.util.Log;

import cn.wzbos.android.rudolph.exception.RudolphException;

public class FragmentRouter<R> extends Router<R> {
Expand Down Expand Up @@ -34,7 +36,7 @@ public R open() {

return (R) instance;
} catch (Exception e) {
e.printStackTrace();
Log.e("rudolph", "open fragment error!", e);
if (callback != null)
callback.onFailed(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Application;
import android.content.Context;
import android.util.Log;

import cn.wzbos.android.rudolph.Rudolph;
import cn.wzbos.android.rudolph.Consts;
Expand Down Expand Up @@ -79,7 +80,7 @@ public Object open(Context context) {
}

} catch (Exception e) {
e.printStackTrace();
Log.e("rudolph", "方法调用异常!", e);
if (null != callback)
callback.onFailed(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ private List<String> getSegments() {
segments.add(URLDecoder.decode(val, "utf-8"));
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e("rudolph", "getSegments failed!", e);
}
}
return segments;
Expand Down Expand Up @@ -385,7 +385,7 @@ Map<String, String> getUriAllParams() {
String value = URLDecoder.decode(kv[1], "utf-8");
params.put(name, value);
} catch (Exception e) {
e.printStackTrace();
Log.e("rudolph", "getUriAllParams failed!", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cn.wzbos.android.rudolph.router;


import android.util.Log;

import java.lang.reflect.Constructor;

import cn.wzbos.android.rudolph.IRouteService;

public class ServiceRouter<R extends IRouteService> extends Router<R> {
Expand All @@ -13,13 +17,19 @@ protected ServiceRouter(Builder builder) {
super(builder);
}


@Override
public R open() {
if (super.intercept(null))
return null;

try {
Object instance = target.getConstructor().newInstance();
Constructor<?> constructor = target.getConstructor();
if (!constructor.isAccessible()) {
Log.w("rudolph", target.getName() + " constructor method is private!");
constructor.setAccessible(true);
}
Object instance = constructor.newInstance();
if (instance instanceof IRouteService) {
IRouteService component = ((IRouteService) instance);
component.init(this.bundle);
Expand All @@ -28,7 +38,7 @@ public R open() {
return (R) component;
}
} catch (Exception e) {
e.printStackTrace();
Log.e("rudolph", "open service failed!");
if (callback != null)
callback.onFailed(e);
}
Expand Down

0 comments on commit 48f60a3

Please sign in to comment.