Migrate ModuleSpec to Kotlin (#51240)

Summary:
Migrate com.facebook.react.bridge.ModuleSpec to Kotlin.

## Changelog:

[INTERNAL] - Migrate com.facebook.react.bridge.ModuleSpec to Kotlin

Pull Request resolved: https://github.com/facebook/react-native/pull/51240

Test Plan:
```bash
yarn test-android
yarn android
```

Reviewed By: cortinico

Differential Revision: D74568550

Pulled By: rshest

fbshipit-source-id: 15cedc9e97f95aa2582ce12c9086336c12564371
This commit is contained in:
Mateo Guzmán
2025-05-13 05:03:09 -07:00
committed by Facebook GitHub Bot
parent 9b4be02ae8
commit 436bddadda
3 changed files with 75 additions and 81 deletions
@@ -959,12 +959,22 @@ public class com/facebook/react/bridge/ModuleHolder {
public fun isTurboModule ()Z
}
public class com/facebook/react/bridge/ModuleSpec {
public fun getName ()Ljava/lang/String;
public fun getProvider ()Ljavax/inject/Provider;
public static fun nativeModuleSpec (Ljava/lang/Class;Ljavax/inject/Provider;)Lcom/facebook/react/bridge/ModuleSpec;
public static fun nativeModuleSpec (Ljava/lang/String;Ljavax/inject/Provider;)Lcom/facebook/react/bridge/ModuleSpec;
public static fun viewManagerSpec (Ljavax/inject/Provider;)Lcom/facebook/react/bridge/ModuleSpec;
public final class com/facebook/react/bridge/ModuleSpec {
public static final field Companion Lcom/facebook/react/bridge/ModuleSpec$Companion;
public synthetic fun <init> (Ljavax/inject/Provider;Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getName ()Ljava/lang/String;
public final fun getProvider ()Ljavax/inject/Provider;
public final fun moduleName ()Ljava/lang/String;
public static final fun nativeModuleSpec (Ljava/lang/Class;Ljavax/inject/Provider;)Lcom/facebook/react/bridge/ModuleSpec;
public static final fun nativeModuleSpec (Ljava/lang/String;Ljavax/inject/Provider;)Lcom/facebook/react/bridge/ModuleSpec;
public final fun provider ()Ljavax/inject/Provider;
public static final fun viewManagerSpec (Ljavax/inject/Provider;)Lcom/facebook/react/bridge/ModuleSpec;
}
public final class com/facebook/react/bridge/ModuleSpec$Companion {
public final fun nativeModuleSpec (Ljava/lang/Class;Ljavax/inject/Provider;)Lcom/facebook/react/bridge/ModuleSpec;
public final fun nativeModuleSpec (Ljava/lang/String;Ljavax/inject/Provider;)Lcom/facebook/react/bridge/ModuleSpec;
public final fun viewManagerSpec (Ljavax/inject/Provider;)Lcom/facebook/react/bridge/ModuleSpec;
}
public abstract class com/facebook/react/bridge/NativeArray : com/facebook/jni/HybridClassBase, com/facebook/react/bridge/NativeArrayInterface {
@@ -1,75 +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.bridge;
import androidx.annotation.Nullable;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.module.annotations.ReactModule;
import javax.inject.Provider;
/**
* A specification for a native module. This exists so that we don't have to pay the cost for
* creation until/if the module is used.
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
public class ModuleSpec {
private static final String TAG = "ModuleSpec";
private final Provider<? extends NativeModule> mProvider;
private final @Nullable String mName;
public static ModuleSpec viewManagerSpec(Provider<? extends NativeModule> provider) {
return new ModuleSpec(provider);
}
public static ModuleSpec nativeModuleSpec(
Class<? extends NativeModule> type, Provider<? extends NativeModule> provider) {
ReactModule annotation = type.getAnnotation(ReactModule.class);
if (annotation == null) {
FLog.w(
TAG,
"Could not find @ReactModule annotation on "
+ type.getName()
+ ". So creating the module eagerly to get the name. Consider adding an annotation to"
+ " make this Lazy");
NativeModule nativeModule = provider.get();
return new ModuleSpec(provider, nativeModule.getName());
} else {
return new ModuleSpec(provider, annotation.name());
}
}
public static ModuleSpec nativeModuleSpec(
String className, Provider<? extends NativeModule> provider) {
return new ModuleSpec(provider, className);
}
/**
* Called by View Managers
*
* @param provider
*/
private ModuleSpec(Provider<? extends NativeModule> provider) {
mProvider = provider;
mName = null;
}
private ModuleSpec(Provider<? extends NativeModule> provider, String name) {
mProvider = provider;
mName = name;
}
public @Nullable String getName() {
return mName;
}
public Provider<? extends NativeModule> getProvider() {
return mProvider;
}
}
@@ -0,0 +1,59 @@
/*
* 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.bridge
import com.facebook.common.logging.FLog
import com.facebook.react.module.annotations.ReactModule
import javax.inject.Provider
/**
* A specification for a native module. This exists so that we don't have to pay the cost for
* creation until/if the module is used.
*/
public class ModuleSpec
private constructor(
@get:JvmName("provider") public val provider: Provider<out NativeModule>,
@get:JvmName("moduleName") public val name: String? = null
) {
public fun getProvider(): Provider<out NativeModule> = provider
public fun getName(): String? = name
public companion object {
private const val TAG: String = "ModuleSpec"
@JvmStatic
public fun viewManagerSpec(provider: Provider<out NativeModule>): ModuleSpec =
ModuleSpec(provider)
@JvmStatic
public fun nativeModuleSpec(
type: Class<out NativeModule>,
provider: Provider<out NativeModule>
): ModuleSpec {
val annotation: ReactModule? = type.getAnnotation(ReactModule::class.java)
return if (annotation == null) {
FLog.w(
TAG,
"Could not find @ReactModule annotation on ${type.name}. " +
"Creating the module eagerly to get the name. Consider adding the annotation.")
val nativeModule: NativeModule = provider.get()
ModuleSpec(provider, nativeModule.name)
} else {
ModuleSpec(provider, annotation.name)
}
}
@JvmStatic
public fun nativeModuleSpec(
className: String,
provider: Provider<out NativeModule>
): ModuleSpec = ModuleSpec(provider, className)
}
}