From da8616ec69a12a90b973fea1d8345c7517408a73 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Thu, 14 Sep 2023 11:13:16 -0700 Subject: [PATCH] Add ReactPackage.getModule method (#39336) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/39336 We are introducing the new getModule method into the ReactPackage interface following the "Registration of Native Modules" design This method defaults to true with the goal to avoid compatibility breakage changelog: [Android][Changed] Introducing getModule method into ReactPackage interface, defaulting to null. This method will be used in the Stable API of React Native Reviewed By: cortinico Differential Revision: D48992720 fbshipit-source-id: 4ecc93bc84daa8903a4d9a1cf6ced60adb78f153 --- .../main/java/com/facebook/react/ReactPackage.java | 13 +++++++++++++ .../java/com/facebook/react/TurboReactPackage.java | 4 +++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackage.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackage.java index bf9469d8adc7aa..f09953e633ed4d 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackage.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackage.java @@ -14,6 +14,7 @@ import com.facebook.react.uimanager.UIManagerModule; import com.facebook.react.uimanager.ViewManager; import java.util.List; +import javax.annotation.Nullable; /** * Main interface for providing additional capabilities to the catalyst framework by couple of @@ -44,4 +45,16 @@ public interface ReactPackage { /** @return a list of view managers that should be registered with {@link UIManagerModule} */ @NonNull List createViewManagers(@NonNull ReactApplicationContext reactContext); + + /** + * Given a module name, it returns an instance of {@link NativeModule} for the name + * + * @param name name of the Native Module + * @param reactContext {@link ReactApplicationContext} context for this + */ + @Nullable + default NativeModule getModule( + @NonNull String name, @NonNull ReactApplicationContext reactContext) { + return null; + } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/TurboReactPackage.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/TurboReactPackage.java index 67d4b05b4b2ba1..398f7a5262ba30 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/TurboReactPackage.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/TurboReactPackage.java @@ -7,6 +7,7 @@ package com.facebook.react; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.facebook.react.bridge.ModuleHolder; import com.facebook.react.bridge.ModuleSpec; @@ -41,8 +42,9 @@ public List createNativeModules(ReactApplicationContext reactConte * @param name name of the Native Module * @param reactContext {@link ReactApplicationContext} context for this */ + @Override public abstract @Nullable NativeModule getModule( - String name, final ReactApplicationContext reactContext); + @NonNull String name, @NonNull ReactApplicationContext reactContext); /** * This is a temporary method till we implement TurboModules. Once we implement TurboModules, we