protected static void initializeJavaModule(final BaseJavaModule javaModule) { final Semaphore semaphore = new Semaphore(0); UiThreadUtil.runOnUiThread( new Runnable() { @Override public void run() { javaModule.initialize(); if (javaModule instanceof LifecycleEventListener) { ((LifecycleEventListener) javaModule).onHostResume(); } semaphore.release(); } }); try { SoftAssertions.assertCondition( semaphore.tryAcquire(5000, TimeUnit.MILLISECONDS), "Timed out initializing timing module"); } catch (InterruptedException e) { throw new RuntimeException(e); } }
@DoNotStrip public List<MethodDescriptor> getMethodDescriptors() { ArrayList<MethodDescriptor> descs = new ArrayList<>(); for (Map.Entry<String, NativeModule.NativeMethod> entry : getModule().getMethods().entrySet()) { MethodDescriptor md = new MethodDescriptor(); md.name = entry.getKey(); md.type = entry.getValue().getType(); BaseJavaModule.JavaMethod method = (BaseJavaModule.JavaMethod) entry.getValue(); if (md.type == BaseJavaModule.METHOD_TYPE_SYNC) { md.signature = method.getSignature(); md.method = method.getMethod(); } mMethods.add(method); descs.add(md); } return descs; }
@DoNotStrip public NativeArray getConstants() { BaseJavaModule baseJavaModule = getModule(); ReactMarker.logMarker(GET_CONSTANTS_START, getName()); SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "Map constants") .arg("moduleName", getName()) .flush(); Map<String, Object> map = baseJavaModule.getConstants(); Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "WritableNativeMap constants") .arg("moduleName", getName()) .flush(); ReactMarker.logMarker(CONVERT_CONSTANTS_START, getName()); WritableNativeMap writableNativeMap; try { writableNativeMap = Arguments.makeNativeMap(map); } finally { Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); } WritableNativeArray array = new WritableNativeArray(); array.pushMap(writableNativeMap); ReactMarker.logMarker(CONVERT_CONSTANTS_END); ReactMarker.logMarker(GET_CONSTANTS_END); return array; }
@DoNotStrip public List<MethodDescriptor> getMethodDescriptors() { ArrayList<MethodDescriptor> descs = new ArrayList<>(); for (Map.Entry<String, BaseJavaModule.NativeMethod> entry : getModule().getMethods().entrySet()) { MethodDescriptor md = new MethodDescriptor(); md.name = entry.getKey(); md.type = entry.getValue().getType(); BaseJavaModule.JavaMethod method = (BaseJavaModule.JavaMethod) entry.getValue(); mMethods.add(method); descs.add(md); } return descs; }
ModuleRegistryHolder getModuleRegistryHolder( CatalystInstanceImpl catalystInstanceImpl) { ArrayList<JavaModuleWrapper> javaModules = new ArrayList<>(); ArrayList<CxxModuleWrapper> cxxModules = new ArrayList<>(); for (Map.Entry<Class<? extends NativeModule>, ModuleHolder> entry : mModules.entrySet()) { Class<?> type = entry.getKey(); ModuleHolder moduleHolder = entry.getValue(); if (BaseJavaModule.class.isAssignableFrom(type)) { javaModules.add(new JavaModuleWrapper(catalystInstanceImpl, moduleHolder)); } else if (CxxModuleWrapper.class.isAssignableFrom(type)) { cxxModules.add((CxxModuleWrapper) moduleHolder.getModule()); } else { throw new IllegalArgumentException("Unknown module type " + type); } } return new ModuleRegistryHolder(catalystInstanceImpl, javaModules, cxxModules); }
@DoNotStrip private void findMethods() { Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "findMethods"); Set<String> methodNames = new HashSet<>(); Method[] targetMethods = mModuleClass.getDeclaredMethods(); for (Method targetMethod : targetMethods) { ReactMethod annotation = targetMethod.getAnnotation(ReactMethod.class); if (annotation != null) { String methodName = targetMethod.getName(); if (methodNames.contains(methodName)) { // We do not support method overloading since js sees a function as an object regardless // of number of params. throw new IllegalArgumentException( "Java Module " + getName() + " method name already registered: " + methodName); } MethodDescriptor md = new MethodDescriptor(); JavaMethodWrapper method = new JavaMethodWrapper(this, targetMethod, annotation.isBlockingSynchronousMethod()); md.name = methodName; md.type = method.getType(); if (md.type == BaseJavaModule.METHOD_TYPE_SYNC) { md.signature = method.getSignature(); md.method = targetMethod; } mMethods.add(method); mDescs.add(md); } } Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); }
@DoNotStrip public @Nullable NativeArray getConstants() { if (!mModuleHolder.getHasConstants()) { return null; } BaseJavaModule baseJavaModule = getModule(); ReactMarker.logMarker(GET_CONSTANTS_START, getName()); SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "Map constants") .arg("moduleName", getName()) .flush(); Map<String, Object> map = baseJavaModule.getConstants(); Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "WritableNativeMap constants") .arg("moduleName", getName()) .flush(); ReactMarker.logMarker(CONVERT_CONSTANTS_START, getName()); WritableNativeMap writableNativeMap; try { writableNativeMap = Arguments.makeNativeMap(map); } finally { Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); } WritableNativeArray array = new WritableNativeArray(); array.pushMap(writableNativeMap); ReactMarker.logMarker(CONVERT_CONSTANTS_END); ReactMarker.logMarker(GET_CONSTANTS_END); return array; }
public void processPackage(ReactPackage reactPackage) { if (mLazyNativeModulesEnabled) { if (!(reactPackage instanceof LazyReactPackage)) { throw new IllegalStateException("Lazy native modules requires all ReactPackage to " + "inherit from LazyReactPackage"); } LazyReactPackage lazyReactPackage = (LazyReactPackage) reactPackage; List<ModuleSpec> moduleSpecs = lazyReactPackage.getNativeModules(mReactApplicationContext); Map<Class, ReactModuleInfo> reactModuleInfoMap = lazyReactPackage.getReactModuleInfoProvider() .getReactModuleInfos(); for (ModuleSpec moduleSpec : moduleSpecs) { Class<? extends NativeModule> type = moduleSpec.getType(); ReactModuleInfo reactModuleInfo = reactModuleInfoMap.get(type); ModuleHolder moduleHolder; if (reactModuleInfo == null) { if (BaseJavaModule.class.isAssignableFrom(type)) { throw new IllegalStateException("Native Java module " + type.getSimpleName() + " should be annotated with @ReactModule and added to a @ReactModuleList."); } ReactMarker.logMarker( ReactMarkerConstants.CREATE_MODULE_START, moduleSpec.getType().getName()); NativeModule module = moduleSpec.getProvider().get(); ReactMarker.logMarker(ReactMarkerConstants.CREATE_MODULE_END); moduleHolder = new ModuleHolder(module); } else { moduleHolder = new ModuleHolder( reactModuleInfo.name(), reactModuleInfo.canOverrideExistingModule(), reactModuleInfo.supportsWebWorkers(), reactModuleInfo.needsEagerInit(), reactModuleInfo.hasConstants(), moduleSpec.getProvider()); } String name = moduleHolder.getName(); if (namesToType.containsKey(name)) { Class<? extends NativeModule> existingNativeModule = namesToType.get(name); if (!moduleHolder.getCanOverrideExistingModule()) { throw new IllegalStateException("Native module " + type.getSimpleName() + " tried to override " + existingNativeModule.getSimpleName() + " for module name " + name + ". If this was your intention, set canOverrideExistingModule=true"); } mModules.remove(existingNativeModule); } namesToType.put(name, type); mModules.put(type, moduleHolder); } } else { FLog.d( ReactConstants.TAG, reactPackage.getClass().getSimpleName() + " is not a LazyReactPackage, falling back to old version."); List<NativeModule> nativeModules; if (reactPackage instanceof ReactInstancePackage) { ReactInstancePackage reactInstancePackage = (ReactInstancePackage) reactPackage; nativeModules = reactInstancePackage.createNativeModules( mReactApplicationContext, mReactInstanceManager); } else { nativeModules = reactPackage.createNativeModules(mReactApplicationContext); } for (NativeModule nativeModule : nativeModules) { addNativeModule(nativeModule); } } }
@DoNotStrip public BaseJavaModule getModule() { return (BaseJavaModule) mModuleHolder.getModule(); }
public void processPackage(ReactPackage reactPackage) { if (mLazyNativeModulesEnabled) { if (!(reactPackage instanceof LazyReactPackage)) { throw new IllegalStateException("Lazy native modules requires all ReactPackage to " + "inherit from LazyReactPackage"); } LazyReactPackage lazyReactPackage = (LazyReactPackage) reactPackage; List<ModuleSpec> moduleSpecs = lazyReactPackage.getNativeModules(mReactApplicationContext); Map<Class, ReactModuleInfo> reactModuleInfoMap = lazyReactPackage.getReactModuleInfoProvider() .getReactModuleInfos(); for (ModuleSpec moduleSpec : moduleSpecs) { Class<? extends NativeModule> type = moduleSpec.getType(); ReactModuleInfo reactModuleInfo = reactModuleInfoMap.get(type); ModuleHolder moduleHolder; if (reactModuleInfo == null) { if (BaseJavaModule.class.isAssignableFrom(type)) { throw new IllegalStateException("Native Java module " + type.getSimpleName() + " should be annotated with @ReactModule and added to a @ReactModuleList."); } ReactMarker.logMarker( ReactMarkerConstants.CREATE_MODULE_START, moduleSpec.getType().getName()); NativeModule module = moduleSpec.getProvider().get(); ReactMarker.logMarker(ReactMarkerConstants.CREATE_MODULE_END); moduleHolder = new ModuleHolder(module); } else { moduleHolder = new ModuleHolder( reactModuleInfo.name(), reactModuleInfo.canOverrideExistingModule(), reactModuleInfo.supportsWebWorkers(), reactModuleInfo.needsEagerInit(), moduleSpec.getProvider()); } String name = moduleHolder.getName(); if (namesToType.containsKey(name)) { Class<? extends NativeModule> existingNativeModule = namesToType.get(name); if (!moduleHolder.getCanOverrideExistingModule()) { throw new IllegalStateException("Native module " + type.getSimpleName() + " tried to override " + existingNativeModule.getSimpleName() + " for module name " + name + ". If this was your intention, set canOverrideExistingModule=true"); } mModules.remove(existingNativeModule); } namesToType.put(name, type); mModules.put(type, moduleHolder); } } else { FLog.d( ReactConstants.TAG, reactPackage.getClass().getSimpleName() + " is not a LazyReactPackage, falling back to old version."); List<NativeModule> nativeModules; if (reactPackage instanceof ReactInstancePackage) { ReactInstancePackage reactInstancePackage = (ReactInstancePackage) reactPackage; nativeModules = reactInstancePackage.createNativeModules( mReactApplicationContext, mReactInstanceManager); } else { nativeModules = reactPackage.createNativeModules(mReactApplicationContext); } for (NativeModule nativeModule : nativeModules) { addNativeModule(nativeModule); } } }