From 920aab10356e0c03cc64a094d0f121466a8dacbc Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Fri, 19 Jul 2019 13:46:57 +0800 Subject: [PATCH] bridge extension and plugin map --- .../pengfeizhou/doric/DoricContext.java | 17 ++++- .../doric/engine/DoricJSEngine.java | 8 +-- .../doric/extension/DoricBridgeExtension.java | 43 +++++++++++-- .../doric/extension/DoricPluginInfo.java | 64 +++++++++++++++++++ .../doric/extension/ModuleClassInfo.java | 44 ------------- .../pengfeizhou/doric/utils/DoricLog.java | 48 ++++++++------ 6 files changed, 152 insertions(+), 72 deletions(-) create mode 100644 Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricPluginInfo.java delete mode 100644 Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/ModuleClassInfo.java diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricContext.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricContext.java index 519e56e6..128e01d9 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricContext.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricContext.java @@ -1,9 +1,12 @@ package com.github.pengfeizhou.doric; +import com.github.pengfeizhou.doric.bridge.DoricNativePlugin; +import com.github.pengfeizhou.doric.extension.DoricPluginInfo; import com.github.pengfeizhou.doric.utils.DoricSettableFuture; import com.github.pengfeizhou.jscore.JSDecoder; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.HashMap; +import java.util.Map; /** * @Description: Doric @@ -11,8 +14,8 @@ import java.util.concurrent.atomic.AtomicInteger; * @CreateDate: 2019-07-18 */ public class DoricContext { - private static AtomicInteger sCounter = new AtomicInteger(); private final String mContextId; + private final Map mPluginMap = new HashMap<>(); DoricContext(String contextId) { this.mContextId = contextId; @@ -33,5 +36,15 @@ public class DoricContext { public void teardown() { DoricDriver.getInstance().destroyContext(mContextId); + mPluginMap.clear(); + } + + public DoricNativePlugin obtainPlugin(DoricPluginInfo doricPluginInfo) { + DoricNativePlugin plugin = mPluginMap.get(doricPluginInfo.getName()); + if (plugin == null) { + plugin = doricPluginInfo.createPlugin(this); + mPluginMap.put(doricPluginInfo.getName(), plugin); + } + return plugin; } } diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/engine/DoricJSEngine.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/engine/DoricJSEngine.java index b9b791f3..1f50d41d 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/engine/DoricJSEngine.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/engine/DoricJSEngine.java @@ -57,13 +57,13 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time String message = args[1].string(); switch (type) { case "w": - DoricLog.w(message, "_js"); + DoricLog.w("_js", message); break; case "e": - DoricLog.e(message, "_js"); + DoricLog.e("_js", message); break; default: - DoricLog.d(message, "_js"); + DoricLog.d("_js", message); break; } } catch (Exception e) { @@ -79,7 +79,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time String name = args[0].string(); String content = Doric.getJSBundle(name); if (TextUtils.isEmpty(content)) { - DoricLog.e("error"); + DoricLog.e("require js bundle:%s is empty", name); return new JavaValue(false); } mDoricJSE.loadJS(packageModuleScript(name, content), "Module://" + name); diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricBridgeExtension.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricBridgeExtension.java index 73bff8a1..c25cb2bf 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricBridgeExtension.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricBridgeExtension.java @@ -1,24 +1,59 @@ package com.github.pengfeizhou.doric.extension; +import android.text.TextUtils; + import com.github.pengfeizhou.doric.DoricContext; import com.github.pengfeizhou.doric.DoricDriver; +import com.github.pengfeizhou.doric.bridge.DoricNativePlugin; +import com.github.pengfeizhou.doric.bridge.ModalPlugin; +import com.github.pengfeizhou.doric.utils.DoricLog; import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JavaValue; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + /** * @Description: Doric * @Author: pengfei.zhou * @CreateDate: 2019-07-18 */ public class DoricBridgeExtension { - public DoricBridgeExtension() { + private Map pluginInfoMap = new HashMap<>(); + public DoricBridgeExtension() { + registerExtension(ModalPlugin.class); } - public JavaValue callNative(String contextId, String module, String method, String callbackId, JSDecoder jsDecoder) { - DoricContext doricContext = DoricDriver.getInstance().getContext(contextId); + public void registerExtension(Class pluginClass) { + DoricPluginInfo doricPluginInfo = new DoricPluginInfo(pluginClass); + if (!TextUtils.isEmpty(doricPluginInfo.getName())) { + pluginInfoMap.put(doricPluginInfo.getName(), doricPluginInfo); + } + } + + public JavaValue callNative(String contextId, String module, String methodName, String callbackId, JSDecoder jsDecoder) { + DoricContext context = DoricDriver.getInstance().getContext(contextId); + DoricPluginInfo pluginInfo = pluginInfoMap.get(module); + if (pluginInfo == null) { + DoricLog.e("Cannot find plugin class:%s", module); + return new JavaValue(false); + } + DoricNativePlugin doricNativePlugin = context.obtainPlugin(pluginInfo); + if (doricNativePlugin == null) { + DoricLog.e("Cannot obtain plugin instance:%s,method:%", module); + return new JavaValue(false); + } + Method method = pluginInfo.getMethod(methodName); + if (method == null) { + DoricLog.e("Cannot find plugin method in class:%s,method:%s", module, methodName); + return new JavaValue(false); + } + + Class[] classes = method.getParameterTypes(); - return null; + return new JavaValue(true); } } diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricPluginInfo.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricPluginInfo.java new file mode 100644 index 00000000..6f0cf6c8 --- /dev/null +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricPluginInfo.java @@ -0,0 +1,64 @@ +package com.github.pengfeizhou.doric.extension; + +import android.text.TextUtils; + +import com.github.pengfeizhou.doric.DoricContext; +import com.github.pengfeizhou.doric.bridge.DoricNativePlugin; +import com.github.pengfeizhou.doric.utils.DoricLog; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * @Description: Doric + * @Author: pengfei.zhou + * @CreateDate: 2019-07-18 + */ +public class DoricPluginInfo { + + private Constructor pluginConstructor; + + private Map methodMap = new ConcurrentHashMap<>(); + public boolean stringify = false; + private String name; + + public DoricPluginInfo(Class pluginClass) { + try { + this.pluginConstructor = pluginClass.getConstructor(DoricContext.class); + DoricComponent doricComponent = pluginClass.getAnnotation(DoricComponent.class); + this.name = doricComponent.name(); + Method[] methods = pluginClass.getMethods(); + for (Method method : methods) { + DoricMethod doricMethod = method.getAnnotation(DoricMethod.class); + if (doricMethod != null) { + if (TextUtils.isEmpty(doricMethod.name())) { + methodMap.put(method.getName(), method); + } else { + methodMap.put(doricMethod.name(), method); + } + } + } + } catch (Exception e) { + DoricLog.e("Error to create doric for " + e.getLocalizedMessage()); + } + } + + public String getName() { + return name; + } + + public DoricNativePlugin createPlugin(DoricContext doricContext) { + try { + return pluginConstructor.newInstance(doricContext); + } catch (Exception e) { + DoricLog.e("Error to create doric plugin for " + e.getLocalizedMessage()); + return null; + } + } + + public Method getMethod(String name) { + return methodMap.get(name); + } +} diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/ModuleClassInfo.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/ModuleClassInfo.java deleted file mode 100644 index 8a70df26..00000000 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/ModuleClassInfo.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.github.pengfeizhou.doric.extension; - -import android.text.TextUtils; - -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.Map; - -/** - * @Description: Doric - * @Author: pengfei.zhou - * @CreateDate: 2019-07-18 - */ -public class ModuleClassInfo { - - private Class clz; - - private Object instance; - - private Map methodMap = new HashMap<>(); - public boolean stringify = false; - - public ModuleClassInfo(Class clz) { - this.clz = clz; - try { - this.instance = clz.newInstance(); - } catch (Exception e) { - e.printStackTrace(); - } - Method[] methods = clz.getMethods(); - if (methods != null) { - for (Method method : methods) { - DoricMethod doricMethod = method.getAnnotation(DoricMethod.class); - if (doricMethod != null) { - if (TextUtils.isEmpty(doricMethod.name())) { - methodMap.put(method.getName(), method); - } else { - methodMap.put(doricMethod.name(), method); - } - } - } - } - } -} diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/utils/DoricLog.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/utils/DoricLog.java index 4998ab24..ddfd9d35 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/utils/DoricLog.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/utils/DoricLog.java @@ -1,5 +1,6 @@ package com.github.pengfeizhou.doric.utils; +import android.text.TextUtils; import android.util.Log; import com.github.pengfeizhou.doric.Doric; @@ -12,27 +13,38 @@ import com.github.pengfeizhou.doric.Doric; public class DoricLog { private static String TAG = Doric.class.getSimpleName(); - public static void d(String message, String... suffix) { - StringBuilder stringBuilder = new StringBuilder(TAG); - for (String s : suffix) { - stringBuilder.append(s); - } - Log.d(stringBuilder.toString(), message); + + public static void d(String message, Object... args) { + Log.d(suffixTag(null), format(message, args)); } - public static void w(String message, String... suffix) { - StringBuilder stringBuilder = new StringBuilder(TAG); - for (String s : suffix) { - stringBuilder.append(s); - } - Log.w(stringBuilder.toString(), message); + public static void w(String message, Object... args) { + Log.w(suffixTag(null), format(message, args)); + } - public static void e(String message, String... suffix) { - StringBuilder stringBuilder = new StringBuilder(TAG); - for (String s : suffix) { - stringBuilder.append(s); - } - Log.e(stringBuilder.toString(), message); + public static void e(String message, Object... args) { + Log.e(suffixTag(null), format(message, args)); + } + + public static void d(String suffix, String message, Object... args) { + Log.d(suffixTag(suffix), format(message, args)); + } + + public static void w(String suffix, String message, Object... args) { + Log.w(suffixTag(suffix), format(message, args)); + + } + + public static void e(String suffix, String message, Object... args) { + Log.e(suffixTag(suffix), format(message, args)); + } + + private static String suffixTag(String suffix) { + return TextUtils.isEmpty(suffix) ? TAG : suffix + TAG; + } + + private static String format(String message, Object... args) { + return String.format(message, args); } }