From e80bdb88be696caa0a4399e9600d394413ca3960 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Wed, 20 Nov 2024 04:05:26 -0800 Subject: [PATCH] Convert com.facebook.react.ReactPackageHelper to Kotlin (#47683) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/47683 Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D66131407 fbshipit-source-id: 73d637afea71a77140051efd8001d6ceebc32a43 --- .../facebook/react/ReactPackageHelper.java | 56 ------------------- .../com/facebook/react/ReactPackageHelper.kt | 43 ++++++++++++++ 2 files changed, 43 insertions(+), 56 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackageHelper.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackageHelper.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackageHelper.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackageHelper.java deleted file mode 100644 index 6b65e35afec..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackageHelper.java +++ /dev/null @@ -1,56 +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 com.facebook.common.logging.FLog; -import com.facebook.react.bridge.ModuleHolder; -import com.facebook.react.bridge.NativeModule; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.common.ReactConstants; -import java.util.Iterator; -import java.util.List; - -class ReactPackageHelper { - /** - * A helper method to iterate over a list of Native Modules and convert them to an iterable. - * - * @param reactPackage - * @param reactApplicationContext - * @param reactInstanceManager - * @return - */ - /** package */ - static Iterable getNativeModuleIterator( - ReactPackage reactPackage, ReactApplicationContext reactApplicationContext) { - FLog.d( - ReactConstants.TAG, - reactPackage.getClass().getSimpleName() - + " is not a LazyReactPackage, falling back to old version."); - final List nativeModules = - reactPackage.createNativeModules(reactApplicationContext); - return () -> - new Iterator() { - int position = 0; - - @Override - public ModuleHolder next() { - return new ModuleHolder(nativeModules.get(position++)); - } - - @Override - public boolean hasNext() { - return position < nativeModules.size(); - } - - @Override - public void remove() { - throw new UnsupportedOperationException("Cannot remove methods "); - } - }; - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackageHelper.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackageHelper.kt new file mode 100644 index 00000000000..2838e0523dd --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackageHelper.kt @@ -0,0 +1,43 @@ +/* + * 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.common.logging.FLog +import com.facebook.react.bridge.ModuleHolder +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.common.ReactConstants + +internal object ReactPackageHelper { + + /** + * A helper method to iterate over a list of Native Modules and convert them to an iterable. + * + * @param reactPackage + * @param reactApplicationContext + * @param reactInstanceManager + * @return + */ + fun getNativeModuleIterator( + reactPackage: ReactPackage, + reactApplicationContext: ReactApplicationContext + ): Iterable { + FLog.d( + ReactConstants.TAG, + "${reactPackage.javaClass.simpleName} is not a LazyReactPackage, falling back to old version.") + val nativeModules = reactPackage.createNativeModules(reactApplicationContext) + return Iterable { + object : Iterator { + var position = 0 + + override fun next(): ModuleHolder = ModuleHolder(nativeModules[position++]) + + override fun hasNext(): Boolean = position < nativeModules.size + } + } + } +}