Skip to content

Commit

Permalink
Merge branch 'master' into master-java8
Browse files Browse the repository at this point in the history
  • Loading branch information
keepactive committed Jul 1, 2021
2 parents 47dec92 + 48e6267 commit bfa56e2
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ private ComponentConstants() {

// System interface

public static final String COMPONENT_CLASS_NAME = "com.xiaojinzi.component.Component";
public static final String UTILS_CLASS_NAME = "com.xiaojinzi.component.support.Utils";
public static final String COMPONENT_GENERATED_ANNO_CLASS_NAME = "com.xiaojinzi.component.anno.support.ComponentGeneratedAnno";
public static final String APPLICATION_LIFECYCLE_INTERFACE_CLASS_NAME = "com.xiaojinzi.component.application.IApplicationLifecycle";
public static final String INTERCEPTOR_INTERFACE_CLASS_NAME = "com.xiaojinzi.component.impl.RouterInterceptor";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import com.xiaojinzi.component.anno.ModuleAppAnno;
Expand Down Expand Up @@ -180,11 +181,33 @@ private MethodSpec generateOnCreateMethod() {
.addParameter(parameterSpec)
.addModifiers(Modifier.PUBLIC);
methodSpecBuilder.addStatement("super.onCreate(application)");
methodSpecBuilder.addStatement("$T.getInstance().register(getHost())", routerCenterTypeElement);

methodSpecBuilder.addStatement("$T.getInstance().register(getHost())", centerServiceTypeElement);
methodSpecBuilder.addStatement("$T.getInstance().register(getHost())", centerInterceptorTypeElement);
methodSpecBuilder.addStatement("$T.getInstance().register(getHost())", centerRouterDegradeTypeElement);
methodSpecBuilder.addStatement("$T.getInstance().register(getHost())", centerFragmentTypeElement);

MethodSpec.Builder runnableMethodBuilder = MethodSpec
.methodBuilder("run")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC);

runnableMethodBuilder.addStatement("$T.getInstance().register(getHost())", routerCenterTypeElement);
runnableMethodBuilder.addStatement("$T.getInstance().register(getHost())", centerInterceptorTypeElement);
runnableMethodBuilder.addStatement("$T.getInstance().register(getHost())", centerRouterDegradeTypeElement);
runnableMethodBuilder.addStatement("$T.getInstance().register(getHost())", centerFragmentTypeElement);

TypeSpec innerTypeSpec = TypeSpec.anonymousClassBuilder("")
.addSuperinterface(ParameterizedTypeName.get(Runnable.class))
.addMethod(runnableMethodBuilder.build())
.build();

methodSpecBuilder.addStatement("$T $N = $L", Runnable.class, "r", innerTypeSpec);

methodSpecBuilder.beginControlFlow("if($N.getConfig().isInitRouterAsync())", ComponentConstants.COMPONENT_CLASS_NAME);
methodSpecBuilder.addStatement("$N.postActionToWorkThread(r)", ComponentConstants.UTILS_CLASS_NAME);
methodSpecBuilder.endControlFlow();
methodSpecBuilder.beginControlFlow("else");
methodSpecBuilder.addStatement("r.run()");
methodSpecBuilder.endControlFlow();

return methodSpecBuilder.build();
}

Expand All @@ -196,8 +219,8 @@ private MethodSpec generateOnDestroyMethod() {
.addModifiers(Modifier.PUBLIC);

methodSpecBuilder.addStatement("super.onDestroy()");
methodSpecBuilder.addStatement("$T.getInstance().unregister(getHost())", routerCenterTypeElement);
methodSpecBuilder.addStatement("$T.getInstance().unregister(getHost())", centerServiceTypeElement);
methodSpecBuilder.addStatement("$T.getInstance().unregister(getHost())", routerCenterTypeElement);
methodSpecBuilder.addStatement("$T.getInstance().unregister(getHost())", centerInterceptorTypeElement);
methodSpecBuilder.addStatement("$T.getInstance().unregister(getHost())", centerRouterDegradeTypeElement);
methodSpecBuilder.addStatement("$T.getInstance().unregister(getHost())", centerFragmentTypeElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,38 +713,38 @@ private void implementInterfaceMethod(TypeSpec.Builder typeSpecBuilder, Executab
}
} else if (isReturnCoroutines) {
if (navigateAnnotation.forResult()) {
routerStatement.insert(0, "$N.activityResultAwait(");
routerStatement.insert(0, "$N.activityResultAwait(\n");
args.add(0, ComponentConstants.KOTLIN_ROUTEREXTENDSKT);
routerStatement.append(", $N)");
routerStatement.append(",\n $N\n)");
args.add(continuationParameter.getSimpleName().toString());
} else if (navigateAnnotation.forIntent()) {
if (navigateAnnotation.resultCodeMatch() == Integer.MIN_VALUE) { // 表示用户没写
routerStatement.insert(0, "$N.intentAwait(");
routerStatement.insert(0, "$N.intentAwait(\n");
args.add(0, ComponentConstants.KOTLIN_ROUTEREXTENDSKT);
routerStatement.append(", $N)");
routerStatement.append(",\n $N\n)");
args.add(continuationParameter.getSimpleName().toString());
} else {
routerStatement.insert(0, "$N.intentResultCodeMatchAwait(");
routerStatement.insert(0, "$N.intentResultCodeMatchAwait(\n");
args.add(0, ComponentConstants.KOTLIN_ROUTEREXTENDSKT);
routerStatement.append(", $L, $N)");
routerStatement.append(",\n $L,\n $N\n)");
args.add(navigateAnnotation.resultCodeMatch());
args.add(continuationParameter.getSimpleName().toString());
}
} else if (navigateAnnotation.forResultCode()) {
routerStatement.insert(0, "$N.resultCodeAwait(");
routerStatement.insert(0, "$N.resultCodeAwait(\n");
args.add(0, ComponentConstants.KOTLIN_ROUTEREXTENDSKT);
routerStatement.append(", $N)");
routerStatement.append(",\n $N\n)");
args.add(continuationParameter.getSimpleName().toString());
} else {
if (navigateAnnotation.resultCodeMatch() == Integer.MIN_VALUE) {
routerStatement.insert(0, "$N.await(");
routerStatement.insert(0, "$N.await(\n");
args.add(0, ComponentConstants.KOTLIN_ROUTEREXTENDSKT);
routerStatement.append(", $N)");
routerStatement.append(",\n $N\n)");
args.add(continuationParameter.getSimpleName().toString());
} else {
routerStatement.insert(0, "$N.resultCodeMatchAwait(");
routerStatement.insert(0, "$N.resultCodeMatchAwait(\n");
args.add(0, ComponentConstants.KOTLIN_ROUTEREXTENDSKT);
routerStatement.append(", $L, $N)");
routerStatement.append(",\n $L,\n $N\n)");
args.add(navigateAnnotation.resultCodeMatch());
args.add(continuationParameter.getSimpleName().toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,35 @@ public void accept(Element element) {
List<String> interServiceClassNames = getInterServiceClassNames(anno);
boolean isUseOne = anno.name().length == 0;
for (int i = 0; i < interServiceClassNames.size(); i++) {
String name = isUseOne ? "" : anno.name()[i];
boolean isNameEmpty = (name == null || "".equals(name));
String interServiceClassName = interServiceClassNames.get(i);
ClassName implClassName = ClassName.get(mElements.getTypeElement(interServiceClassName));
if (anno.autoInit()) {
if (isNameEmpty) {
methodSpecBuilder.addStatement(
"$T.registerAutoInit($T.class)",
classNameServiceContainer, implClassName
);
} else {
methodSpecBuilder.addStatement(
"$T.registerAutoInit($T.class, $S)",
classNameServiceContainer, implClassName, name
);
}
}
if (isNameEmpty) {
methodSpecBuilder.addStatement(
"$T.register($T.class, $N)",
classNameServiceContainer, implClassName, implName
);
} else {
methodSpecBuilder.addStatement(
"$T.registerAutoInit($T.class)",
classNameServiceContainer, implClassName
"$T.register($T.class, $S, $N)",
classNameServiceContainer, implClassName, name, implName
);
}
String name = isUseOne ? "" : anno.name()[i];
methodSpecBuilder.addStatement(
"$T.register($T.class, $S, $N)",
classNameServiceContainer, implClassName, name, implName
);

}
}
});
Expand All @@ -299,16 +315,28 @@ public void accept(Element element) {
List<String> interServiceClassNames = getInterServiceClassNames(anno);
boolean isUseOne = anno.name().length == 0;
for (int i = 0; i < interServiceClassNames.size(); i++) {
String interServiceClassNameStr = interServiceClassNames.get(i);
String name = isUseOne ? "" : anno.name()[i];
boolean isNameEmpty = (name == null || "".equals(name));
String interServiceClassNameStr = interServiceClassNames.get(i);
ClassName interServiceClassName = ClassName.get(mElements.getTypeElement(interServiceClassNameStr));
if (anno.autoInit()) {
methodSpecBuilder.addStatement(
"$T.unregisterAutoInit($T.class)",
classNameServiceContainer, interServiceClassName
);
}
methodSpecBuilder.addStatement("$T.unregister($T.class, $S)", classNameServiceContainer, interServiceClassName, name);
if (isNameEmpty) {
methodSpecBuilder.addStatement(
"$T.unregister($T.class)",
classNameServiceContainer, interServiceClassName
);
} else {
methodSpecBuilder.addStatement(
"$T.unregister($T.class, $S)",
classNameServiceContainer, interServiceClassName, name
);
}

}
}
});
Expand Down
12 changes: 12 additions & 0 deletions ComponentImpl/src/main/java/com/xiaojinzi/component/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class Config {
@NonNull
private String defaultScheme;

private boolean isInitRouterAsync;
private boolean isOptimizeInit;
private boolean isAutoRegisterModule;
private boolean isTipWhenUseApplication;
Expand All @@ -29,6 +30,7 @@ public class Config {

private Config(@NonNull Builder builder) {
this.application = builder.application;
this.isInitRouterAsync = builder.isInitRouterAsync;
this.isOptimizeInit = builder.isOptimizeInit;
this.isAutoRegisterModule = builder.isAutoRegisterModule;
this.isTipWhenUseApplication = builder.isTipWhenUseApplication;
Expand All @@ -49,6 +51,10 @@ public String getDefaultScheme() {
return defaultScheme;
}

public boolean isInitRouterAsync() {
return isInitRouterAsync;
}

public boolean isOptimizeInit() {
return isOptimizeInit;
}
Expand Down Expand Up @@ -87,6 +93,7 @@ public static class Builder {

private Application application;
private String defaultScheme = "router";
private boolean isInitRouterAsync = false;
private boolean isOptimizeInit = false;
private boolean isAutoRegisterModule = false;
private boolean isTipWhenUseApplication = true;
Expand All @@ -109,6 +116,11 @@ public Builder defaultScheme(String defaultScheme) {
return this;
}

public Builder initRouterAsync(boolean isInitRouterAsync) {
this.isInitRouterAsync = isInitRouterAsync;
return this;
}

public Builder optimizeInit(boolean isOptimizeInit) {
this.isOptimizeInit = isOptimizeInit;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,29 @@ private ServiceManager() {
/**
* 需要自动初始化的 Service 的 class
*/
private static final Set<Class> autoInitSet = new HashSet<>();
private static final HashMap<Class, String> autoInitMap = new HashMap<Class, String>();

/**
* 注册自动注册的 Service Class
*/
public static <T> void registerAutoInit(@NonNull Class<T> tClass) {
registerAutoInit(tClass, null);
}

/**
* 注册自动注册的 Service Class
*/
public static <T> void registerAutoInit(@NonNull Class<T> tClass, @Nullable String name) {
Utils.checkNullPointer(tClass, "tClass");
autoInitSet.add(tClass);
autoInitMap.put(tClass, name);
}

/**
* 反注册自动注册的 Service Class
*/
public static <T> void unregisterAutoInit(@NonNull Class<T> tClass) {
Utils.checkNullPointer(tClass, "tClass");
autoInitSet.remove(tClass);
autoInitMap.remove(tClass);
}

/**
Expand All @@ -78,9 +85,14 @@ public static <T> void unregisterAutoInit(@NonNull Class<T> tClass) {
@WorkerThread
@NotAppUseAnno
public static void autoInitService() {
for (Class targetClass : autoInitSet) {
// 初始化实现类
ServiceManager.get(targetClass);
for (Map.Entry<Class, String> entry : autoInitMap.entrySet()) {
if (entry.getValue() == null) {
// 初始化实现类
ServiceManager.get(entry.getKey());
} else {
ServiceManager.get(entry.getKey(), entry.getValue());
}

}
}

Expand Down Expand Up @@ -197,7 +209,6 @@ public static <T> void unregister(@NonNull Class<T> tClass, @NonNull String name
*
* @param tClass 目标 Service class
* @param target 目标对象
* @param <target> 目标对象
* @return 返回一个增强的目标对象的装饰者
*/
@NotAppUseAnno
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,27 @@ public interface IComponentCenter<T> {
*
* @param t 注册的目标
*/
@UiThread
void register(@NonNull T t);

/**
* 通过host注册
*
* @param host 需要注册的 host
*/
@UiThread
void register(@NonNull String host);

/**
* 反注册
*
* @param t 反注册的目标
*/
@UiThread
void unregister(@NonNull T t);

/**
* 通过 host 反注册
*
* @param host 反注册的 host
*/
@UiThread
void unregister(@NonNull String host);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.xiaojinzi.component.support;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
Expand All @@ -15,11 +14,10 @@

import com.xiaojinzi.component.Component;
import com.xiaojinzi.component.error.RouterRuntimeException;
import com.xiaojinzi.component.error.RunTimeTimeoutException;

import java.util.concurrent.TimeoutException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

/**
* 一个工具类
Expand All @@ -35,6 +33,8 @@ public class Utils {
private static final String STR_CAN_NOT_BE_NULL = "' can't be null";
private static final String MAIN_THREAD_ERROR_MSG = "Component mainThreadCall method timeout, A deadlock was happened. see: https://github.com/xiaojinzi123/Component/issues/79";
private static final long MAIN_THREAD_TIME_OUT = 3000;
// 单线程的线程池
private static final ExecutorService workPool = Executors.newSingleThreadExecutor();

private Utils() {
}
Expand All @@ -54,13 +54,7 @@ public static void postDelayActionToMainThread(@NonNull @UiThread Runnable r, lo

@AnyThread
public static void postActionToWorkThread(@NonNull final Runnable r) {
new Thread(){
@Override
public void run() {
super.run();
r.run();
}
}.start();
workPool.submit(r);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.xiaojinzi.component.anno.ModuleAppAnno;
import com.xiaojinzi.component.application.IApplicationLifecycle;
import com.xiaojinzi.component.support.LogUtil;

@ModuleAppAnno()
public class Component1Application implements IApplicationLifecycle {
Expand All @@ -21,7 +22,13 @@ public static Application getApp() {
@Override
public void onCreate(@NonNull final Application app) {
mApp = app;
/*LogUtil.loge("模块1休眠一下");
// 你可以做一些当前业务模块的一些初始化
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// ignore
}*/
}

@Override
Expand Down
Loading

0 comments on commit bfa56e2

Please sign in to comment.