From f87ae249c98445f5adf04be19a6be21101ffe37f Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Tue, 8 Apr 2025 01:25:16 -0700 Subject: [PATCH] Migrate to Kotlin - DebugCorePackage (#50521) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/50521 This diff migrates the following file to Kotlin - DebugCorePackage as part of our ongoing effort of migrating the codebase to Kotlin Changelog: [Internal] [Changed] - Migrate to Kotlin - DebugCorePackage Reviewed By: javache Differential Revision: D72556867 fbshipit-source-id: dc37538da7089f532dc3a617a8e7dbccc6e187e3 --- .../ReactAndroid/api/ReactAndroid.api | 2 +- .../com/facebook/react/DebugCorePackage.java | 86 ------------------- .../com/facebook/react/DebugCorePackage.kt | 56 ++++++++++++ 3 files changed, 57 insertions(+), 87 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/DebugCorePackage.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/DebugCorePackage.kt diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 4d4316d7262..0bf50ab0beb 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -12,7 +12,7 @@ public class com/facebook/react/CoreModulesPackage$$ReactModuleInfoProvider : co public fun getReactModuleInfos ()Ljava/util/Map; } -public class com/facebook/react/DebugCorePackage : com/facebook/react/BaseReactPackage, com/facebook/react/ViewManagerOnDemandReactPackage { +public final class com/facebook/react/DebugCorePackage : com/facebook/react/BaseReactPackage, com/facebook/react/ViewManagerOnDemandReactPackage { public fun ()V public fun createViewManager (Lcom/facebook/react/bridge/ReactApplicationContext;Ljava/lang/String;)Lcom/facebook/react/uimanager/ViewManager; public fun getModule (Ljava/lang/String;Lcom/facebook/react/bridge/ReactApplicationContext;)Lcom/facebook/react/bridge/NativeModule; diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/DebugCorePackage.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/DebugCorePackage.java deleted file mode 100644 index 41d92671908..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/DebugCorePackage.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.react; - -import androidx.annotation.Nullable; -import com.facebook.infer.annotation.Nullsafe; -import com.facebook.react.bridge.ModuleSpec; -import com.facebook.react.bridge.NativeModule; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.UIManager; -import com.facebook.react.module.annotations.ReactModuleList; -import com.facebook.react.module.model.ReactModuleInfo; -import com.facebook.react.module.model.ReactModuleInfoProvider; -import com.facebook.react.uimanager.ViewManager; -import com.facebook.react.views.debuggingoverlay.DebuggingOverlayManager; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Package defining core framework modules (e.g. UIManager). It should be used for modules that - * require special integration with other framework parts (e.g. with the list of packages to load - * view managers from). - */ -@Nullsafe(Nullsafe.Mode.LOCAL) -@ReactModuleList(nativeModules = {}) -/* package */ -public class DebugCorePackage extends BaseReactPackage implements ViewManagerOnDemandReactPackage { - private @Nullable Map mViewManagers; - - public DebugCorePackage() {} - - @Override - public @Nullable NativeModule getModule(String name, ReactApplicationContext reactContext) { - return null; - } - - @Override - public ReactModuleInfoProvider getReactModuleInfoProvider() { - return new ReactModuleInfoProvider() { - @Override - public Map getReactModuleInfos() { - return Collections.emptyMap(); - } - }; - } - - /** - * @return a map of view managers that should be registered with {@link UIManager} - */ - private Map getViewManagersMap() { - if (mViewManagers == null) { - Map viewManagers = new HashMap<>(); - viewManagers.put( - DebuggingOverlayManager.REACT_CLASS, - ModuleSpec.viewManagerSpec(DebuggingOverlayManager::new)); - mViewManagers = viewManagers; - } - return mViewManagers; - } - - @Override - public List getViewManagers(ReactApplicationContext reactContext) { - return new ArrayList<>(getViewManagersMap().values()); - } - - @Override - public Collection getViewManagerNames(ReactApplicationContext reactContext) { - return getViewManagersMap().keySet(); - } - - @Override - public @Nullable ViewManager createViewManager( - ReactApplicationContext reactContext, String viewManagerName) { - ModuleSpec spec = getViewManagersMap().get(viewManagerName); - return spec != null ? (ViewManager) spec.getProvider().get() : null; - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/DebugCorePackage.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/DebugCorePackage.kt new file mode 100644 index 00000000000..9e1cd7bc107 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/DebugCorePackage.kt @@ -0,0 +1,56 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react + +import com.facebook.react.bridge.ModuleSpec +import com.facebook.react.bridge.NativeModule +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.UIManager +import com.facebook.react.module.annotations.ReactModuleList +import com.facebook.react.module.model.ReactModuleInfoProvider +import com.facebook.react.uimanager.ViewManager +import com.facebook.react.views.debuggingoverlay.DebuggingOverlayManager + +/** + * Package defining core framework modules (e.g. [UIManager]). It should be used for modules that + * require special integration with other framework parts (e.g. with the list of packages to load + * view managers from). + */ +@ReactModuleList(nativeModules = []) +public class DebugCorePackage public constructor() : + BaseReactPackage(), ViewManagerOnDemandReactPackage { + + /** A map of view managers that should be registered with [UIManager] */ + private val viewManagersMap: Map by + lazy(LazyThreadSafetyMode.NONE) { + mapOf( + DebuggingOverlayManager.REACT_CLASS to + ModuleSpec.viewManagerSpec { DebuggingOverlayManager() }) + } + + override fun getReactModuleInfoProvider(): ReactModuleInfoProvider = ReactModuleInfoProvider { + emptyMap() + } + + public override fun getModule( + name: String, + reactContext: ReactApplicationContext + ): NativeModule? = null + + public override fun getViewManagers(reactContext: ReactApplicationContext): List = + viewManagersMap.values.toList() + + override fun getViewManagerNames(reactContext: ReactApplicationContext): Collection = + viewManagersMap.keys + + override fun createViewManager( + reactContext: ReactApplicationContext, + viewManagerName: String + ): ViewManager<*, *>? = + viewManagersMap.getOrDefault(viewManagerName, null)?.provider?.get() as? ViewManager<*, *> +}