Java Code Examples for com.facebook.react.bridge.BaseJavaModule

The following are top voted examples for showing how to use com.facebook.react.bridge.BaseJavaModule. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: RNLearn_Project1   File: ReactIntegrationTestCase.java   View source code 6 votes vote down vote up
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);
  }
}
 
Example 2
Project: RNLearn_Project1   File: JavaModuleWrapper.java   View source code 6 votes vote down vote up
@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;
}
 
Example 3
Project: RNLearn_Project1   File: JavaModuleWrapper.java   View source code 6 votes vote down vote up
@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;
}
 
Example 4
Project: RNLearn_Project1   File: ReactIntegrationTestCase.java   View source code 6 votes vote down vote up
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);
  }
}
 
Example 5
Project: ReactNativeSignatureExample   File: ReactIntegrationTestCase.java   View source code 6 votes vote down vote up
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);
  }
}
 
Example 6
Project: react-native-ibeacon-android   File: ReactIntegrationTestCase.java   View source code 6 votes vote down vote up
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);
  }
}
 
Example 7
Project: react-native-box-loaders   File: ReactIntegrationTestCase.java   View source code 6 votes vote down vote up
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);
  }
}
 
Example 8
Project: Ironman   File: JavaModuleWrapper.java   View source code 6 votes vote down vote up
@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;
}
 
Example 9
Project: Ironman   File: NativeModuleRegistry.java   View source code 6 votes vote down vote up
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);
}
 
Example 10
Project: Ironman   File: ReactIntegrationTestCase.java   View source code 6 votes vote down vote up
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);
  }
}
 
Example 11
Project: RNLearn_Project1   File: JavaModuleWrapper.java   View source code 5 votes vote down vote up
@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);
}
 
Example 12
Project: RNLearn_Project1   File: JavaModuleWrapper.java   View source code 5 votes vote down vote up
@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;
}
 
Example 13
Project: RNLearn_Project1   File: NativeModuleRegistryBuilder.java   View source code 4 votes vote down vote up
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);
    }
  }
}
 
Example 14
Project: RNLearn_Project1   File: JavaModuleWrapper.java   View source code 4 votes vote down vote up
@DoNotStrip
public BaseJavaModule getModule() {
  return (BaseJavaModule) mModuleHolder.getModule();
}
 
Example 15
Project: RNLearn_Project1   File: NativeModuleRegistryBuilder.java   View source code 4 votes vote down vote up
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);
    }
  }
}
 
Example 16
Project: RNLearn_Project1   File: JavaModuleWrapper.java   View source code 4 votes vote down vote up
@DoNotStrip
public BaseJavaModule getModule() {
  return (BaseJavaModule) mModuleHolder.getModule();
}
 
Example 17
Project: Ironman   File: JavaModuleWrapper.java   View source code 4 votes vote down vote up
@DoNotStrip
public BaseJavaModule getModule() {
  return (BaseJavaModule) mModuleHolder.getModule();
}