Skip to content

Commit 905fa24

Browse files
mdvaccameta-codesync[bot]
authored andcommitted
Fix KotlinNullableVariableCastToNonnullType in DefaultReactHost.kt (#55406)
Summary: Pull Request resolved: #55406 Refactored getDefaultReactHost to prove that reactHost is never null at return time, eliminating the need for checkNotNull. The original code used `checkNotNull(reactHost)` which was a runtime assertion. The refactored code uses a pattern that proves null-safety at compile time: 1. Early return with `?.let` if reactHost is already initialized 2. Create the new ReactHostImpl in a local `val newReactHost` (which compiler knows is non-null) 3. Assign to the class field and return the local variable directly This approach eliminates the runtime check while maintaining the same singleton behavior. changelog: [internal] internal Reviewed By: javache Differential Revision: D91820164 fbshipit-source-id: 4e550569e8473b549ba0debb701991a260a8dd04
1 parent 9bd72e7 commit 905fa24

1 file changed

Lines changed: 36 additions & 34 deletions

File tree

  • packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultReactHost.kt

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -71,43 +71,45 @@ public object DefaultReactHost {
7171
exceptionHandler: (Exception) -> Unit = { throw it },
7272
bindingsInstaller: BindingsInstaller? = null,
7373
): ReactHost {
74-
if (reactHost == null) {
74+
reactHost?.let {
75+
return it
76+
}
7577

76-
val bundleLoader =
77-
if (jsBundleFilePath != null) {
78-
if (jsBundleFilePath.startsWith("assets://")) {
79-
JSBundleLoader.createAssetLoader(context, jsBundleFilePath, true)
80-
} else {
81-
JSBundleLoader.createFileLoader(jsBundleFilePath)
82-
}
78+
val bundleLoader =
79+
if (jsBundleFilePath != null) {
80+
if (jsBundleFilePath.startsWith("assets://")) {
81+
JSBundleLoader.createAssetLoader(context, jsBundleFilePath, true)
8382
} else {
84-
JSBundleLoader.createAssetLoader(context, "assets://$jsBundleAssetPath", true)
83+
JSBundleLoader.createFileLoader(jsBundleFilePath)
8584
}
86-
val defaultTmmDelegateBuilder = DefaultTurboModuleManagerDelegate.Builder()
87-
cxxReactPackageProviders.forEach { defaultTmmDelegateBuilder.addCxxReactPackage(it) }
88-
val defaultReactHostDelegate =
89-
DefaultReactHostDelegate(
90-
jsMainModulePath = jsMainModulePath,
91-
jsBundleLoader = bundleLoader,
92-
reactPackages = packageList,
93-
jsRuntimeFactory = jsRuntimeFactory ?: HermesInstance(),
94-
bindingsInstaller = bindingsInstaller,
95-
turboModuleManagerDelegateBuilder = defaultTmmDelegateBuilder,
96-
exceptionHandler = exceptionHandler,
97-
)
98-
val componentFactory = ComponentFactory()
99-
DefaultComponentsRegistry.register(componentFactory)
100-
// TODO: T164788699 find alternative of accessing ReactHostImpl for initialising reactHost
101-
reactHost =
102-
ReactHostImpl(
103-
context,
104-
defaultReactHostDelegate,
105-
componentFactory,
106-
true /* allowPackagerServerAccess */,
107-
useDevSupport,
108-
)
109-
}
110-
return reactHost as ReactHost
85+
} else {
86+
JSBundleLoader.createAssetLoader(context, "assets://$jsBundleAssetPath", true)
87+
}
88+
val defaultTmmDelegateBuilder = DefaultTurboModuleManagerDelegate.Builder()
89+
cxxReactPackageProviders.forEach { defaultTmmDelegateBuilder.addCxxReactPackage(it) }
90+
val defaultReactHostDelegate =
91+
DefaultReactHostDelegate(
92+
jsMainModulePath = jsMainModulePath,
93+
jsBundleLoader = bundleLoader,
94+
reactPackages = packageList,
95+
jsRuntimeFactory = jsRuntimeFactory ?: HermesInstance(),
96+
bindingsInstaller = bindingsInstaller,
97+
turboModuleManagerDelegateBuilder = defaultTmmDelegateBuilder,
98+
exceptionHandler = exceptionHandler,
99+
)
100+
val componentFactory = ComponentFactory()
101+
DefaultComponentsRegistry.register(componentFactory)
102+
// TODO: T164788699 find alternative of accessing ReactHostImpl for initialising reactHost
103+
val newReactHost =
104+
ReactHostImpl(
105+
context,
106+
defaultReactHostDelegate,
107+
componentFactory,
108+
true /* allowPackagerServerAccess */,
109+
useDevSupport,
110+
)
111+
reactHost = newReactHost
112+
return newReactHost
111113
}
112114

113115
/**

0 commit comments

Comments
 (0)