Skip to content

Commit e3c3a0c

Browse files
javachefacebook-github-bot
authored andcommitted
Migrate NativeModuleRegistryBuilder to Kotlin (#47508)
Summary: Pull Request resolved: #47508 Changelog: [Internal] Reviewed By: tdn120 Differential Revision: D60037204 fbshipit-source-id: 2b405e492520e075b83a075009d25fb7b7fa8925
1 parent 2ec547a commit e3c3a0c

File tree

4 files changed

+66
-73
lines changed

4 files changed

+66
-73
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ public class com/facebook/react/MemoryPressureRouter : android/content/Component
6868
public fun removeMemoryPressureListener (Lcom/facebook/react/bridge/MemoryPressureListener;)V
6969
}
7070

71-
public class com/facebook/react/NativeModuleRegistryBuilder {
71+
public final class com/facebook/react/NativeModuleRegistryBuilder {
72+
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;)V
7273
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;Lcom/facebook/react/ReactInstanceManager;)V
73-
public fun build ()Lcom/facebook/react/bridge/NativeModuleRegistry;
74-
public fun processPackage (Lcom/facebook/react/ReactPackage;)V
74+
public final fun build ()Lcom/facebook/react/bridge/NativeModuleRegistry;
75+
public final fun processPackage (Lcom/facebook/react/ReactPackage;)V
7576
}
7677

7778
public abstract class com/facebook/react/ReactActivity : androidx/appcompat/app/AppCompatActivity, com/facebook/react/modules/core/DefaultHardwareBackBtnHandler, com/facebook/react/modules/core/PermissionAwareActivity {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/NativeModuleRegistryBuilder.java

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react
9+
10+
import com.facebook.react.bridge.ModuleHolder
11+
import com.facebook.react.bridge.NativeModuleRegistry
12+
import com.facebook.react.bridge.ReactApplicationContext
13+
14+
/** Helper class to build NativeModuleRegistry. */
15+
public class NativeModuleRegistryBuilder(
16+
private val reactApplicationContext: ReactApplicationContext,
17+
) {
18+
19+
private val modules = HashMap<String, ModuleHolder>()
20+
21+
@Deprecated(
22+
"ReactInstanceManager is not used",
23+
ReplaceWith("NativeModuleRegistryBuilder(reactApplicationContext)"))
24+
public constructor(
25+
reactApplicationContext: ReactApplicationContext,
26+
@Suppress("UNUSED_PARAMETER") reactInstanceManager: ReactInstanceManager
27+
) : this(reactApplicationContext) {}
28+
29+
public fun processPackage(reactPackage: ReactPackage) {
30+
// We use an iterable instead of an iterator here to ensure thread safety, and that this list
31+
// cannot be modified
32+
val moduleHolders =
33+
@Suppress("DEPRECATION")
34+
if (reactPackage is LazyReactPackage) {
35+
reactPackage.getNativeModuleIterator(reactApplicationContext)
36+
} else if (reactPackage is BaseReactPackage) {
37+
reactPackage.getNativeModuleIterator(reactApplicationContext)
38+
} else {
39+
ReactPackageHelper.getNativeModuleIterator(reactPackage, reactApplicationContext)
40+
}
41+
for (moduleHolder in moduleHolders) {
42+
val name = moduleHolder.name
43+
val existingNativeModule = modules[name]
44+
if (existingNativeModule != null) {
45+
check(moduleHolder.canOverrideExistingModule) {
46+
"""
47+
Native module $name tried to override ${existingNativeModule.className}.
48+
49+
Check the getPackages() method in MainApplication.java, it might be that module is being created twice.
50+
If this was your intention, set canOverrideExistingModule=true. This error may also be present if the
51+
package is present only once in getPackages() but is also automatically added later during build time
52+
by autolinking. Try removing the existing entry and rebuild.
53+
"""
54+
}
55+
}
56+
modules[name] = moduleHolder
57+
}
58+
}
59+
60+
public fun build(): NativeModuleRegistry = NativeModuleRegistry(reactApplicationContext, modules)
61+
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ private ReactApplicationContext createReactContext(
15361536
private NativeModuleRegistry processPackages(
15371537
ReactApplicationContext reactContext, List<ReactPackage> packages) {
15381538
NativeModuleRegistryBuilder nativeModuleRegistryBuilder =
1539-
new NativeModuleRegistryBuilder(reactContext, this);
1539+
new NativeModuleRegistryBuilder(reactContext);
15401540

15411541
ReactMarker.logMarker(PROCESS_PACKAGES_START);
15421542

0 commit comments

Comments
 (0)