WXModuleManager { static boolean callModuleMethod(String instanceId, String moduleStr, String methodStr, JSONArray args) { ModuleFactory factory = sModuleFactoryMap.get(moduleStr); if(factory == null){ WXLogUtils.e("[WXModuleManager] module factory not found."); return false; } final WXModule wxModule = findModule(instanceId, moduleStr,factory); if (wxModule == null) { return false; } wxModule.mWXSDKInstance = WXSDKManager.getInstance().getSDKInstance(instanceId); Map<String, Invoker> methodsMap = factory.getMethodMap(); if (methodsMap == null) { WXLogUtils.e("[WXModuleManager] callModuleMethod methodsMap is null."); return false; } final Invoker invoker = methodsMap.get(methodStr); try { Type[] paramClazzs = invoker.getParameterTypes(); final Object[] params = new Object[paramClazzs.length]; Object value; Type paramClazz; for (int i = 0; i < paramClazzs.length; i++) { paramClazz = paramClazzs[i]; if(i>=args.size()){ if(!paramClazz.getClass().isPrimitive()) { params[i] = null; continue; }else { WXLogUtils.e("[WXModuleManager] module method argument list not match."); return false; } } value = args.get(i); if (paramClazz == JSONObject.class) { params[i] = value; } else if(JSCallback.class == paramClazz){ if(value instanceof String){ params[i] = new SimpleJSCallback(instanceId,(String)value); }else{ throw new Exception("Parameter type not match."); } } else { params[i] = WXReflectionUtils.parseArgument(paramClazz,value); } } if (invoker.isRunInUIThread()) { WXSDKManager.getInstance().postOnUiThread(new Runnable() { @Override public void run() { try { invoker.invoke(wxModule, params); } catch (Exception e) { WXLogUtils.e("callModuleMethod >>> invoke module:", e); } } }, 0); } else { invoker.invoke(wxModule, params); } } catch (Exception e) { WXLogUtils.e("callModuleMethod >>> invoke module:" + moduleStr + ", method:" + methodStr + " failed. ", e); return false; } finally { if (wxModule instanceof WXDomModule || wxModule instanceof WXTimerModule) { wxModule.mWXSDKInstance = null; } } return true; }  static boolean registerModule(final String moduleName, final ModuleFactory factory, final boolean global); static void destroyInstanceModules(String instanceId); static void reload();  }
@Test public void testCallModuleMethod() throws Exception { WXModuleManager.callModuleMethod(instance.getInstanceId(),"test1","testMethod",null); WXModuleManager.callModuleMethod(instance.getInstanceId(),"test2","testMethod",null); }
@Test public void testCallModuleMethod2() throws Exception { JSONArray args = new JSONArray(); args.add("testarg"); WXModuleManager.callModuleMethod(instance.getInstanceId(),"test1","testMethod",args); }
@Test public void testCallModuleMethod3() throws Exception { JSONArray args = new JSONArray(); args.add("testarg"); args.add(null); WXModuleManager.callModuleMethod(instance.getInstanceId(),"test1","testCallbackMethod",args); }
@Test public void testCallModuleMethod4() throws Exception { JSONArray args = new JSONArray(); args.add("testarg"); args.add("testcallbackId"); WXModuleManager.callModuleMethod(instance.getInstanceId(),"test1","testCallbackMethod",args); }