com.facebook.react.module.annotations.ReactModule Java Examples

The following examples show how to use com.facebook.react.module.annotations.ReactModule. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReactModuleSpecProcessor.java    From react-native-GPay with MIT License 4 votes vote down vote up
private CodeBlock getCodeBlockForReactModuleInfos(List<String> nativeModules)
  throws ReactModuleSpecException {
  CodeBlock.Builder builder = CodeBlock.builder();
  if (nativeModules == null || nativeModules.isEmpty()) {
    builder.addStatement("return $T.emptyMap()", COLLECTIONS_TYPE);
  } else {
    builder.addStatement("$T map = new $T()", MAP_TYPE, INSTANTIATED_MAP_TYPE);

    TypeMirror cxxModuleWrapperTypeMirror = mElements.getTypeElement(CxxModuleWrapper.class.getName()).asType();

    for (String nativeModule : nativeModules) {
      String keyString = nativeModule;

      TypeElement typeElement = mElements.getTypeElement(nativeModule);
      if (typeElement == null) {
        throw new ReactModuleSpecException(
          keyString + " not found by ReactModuleSpecProcessor. " +
          "Did you misspell the module?");
      }

      ReactModule reactModule = typeElement.getAnnotation(ReactModule.class);
      if (reactModule == null) {
        throw new ReactModuleSpecException(
          keyString + " not found by ReactModuleSpecProcessor. " +
          "Did you forget to add the @ReactModule annotation to the native module?");
      }

      List<? extends Element> elements = typeElement.getEnclosedElements();
      boolean hasConstants = false;
      if (elements != null) {
        hasConstants =
            elements
                .stream()
                .filter(element -> element.getKind() == ElementKind.METHOD)
                .map(Element::getSimpleName)
                .anyMatch(
                    name -> name.contentEquals("getConstants") || name.contentEquals("getTypedExportedConstants"));
      }

      boolean isCxxModule = mTypes.isAssignable(typeElement.asType(), cxxModuleWrapperTypeMirror);

      String valueString = new StringBuilder()
        .append("new ReactModuleInfo(")
        .append("\"").append(reactModule.name()).append("\"").append(", ")
        .append(reactModule.canOverrideExistingModule()).append(", ")
        .append(reactModule.needsEagerInit()).append(", ")
        .append(hasConstants).append(", ")
        .append(isCxxModule)
        .append(")")
        .toString();

      builder.addStatement("map.put(\"" + keyString + "\", " + valueString + ")");
    }
    builder.addStatement("return map");
  }
  return builder.build();
}