From 93efaeb241fcf037b22a92b127d5ffd9fe8bde0c Mon Sep 17 00:00:00 2001 From: Vin-Xi Date: Mon, 28 Apr 2025 09:20:56 -0700 Subject: [PATCH] refactor: rewrite Inspector from Java to Kotlin (#50947) Summary: Rewrite of the Inspector class from Java to Kotlin in scope of https://github.com/facebook/react-native/issues/50513 ## Changelog: [ANDROID] [CHANGED] - Migrated Inspector to Kotlin Pull Request resolved: https://github.com/facebook/react-native/pull/50947 Test Plan: Tested using RNTester app, on both old and new arch, and tested by navigating to multiple pages Reviewed By: cortinico Differential Revision: D73767386 Pulled By: javache fbshipit-source-id: e0098568aa0ed9863503e206a88d3b171c8f9966 --- .../ReactAndroid/api/ReactAndroid.api | 26 ++-- .../com/facebook/react/bridge/Inspector.java | 112 ------------------ .../com/facebook/react/bridge/Inspector.kt | 78 ++++++++++++ 3 files changed, 94 insertions(+), 122 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/Inspector.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/Inspector.kt diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index f879b8f0b24..b3cdd036b39 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -737,20 +737,26 @@ public abstract class com/facebook/react/bridge/GuardedRunnable : java/lang/Runn public abstract fun runGuarded ()V } -public class com/facebook/react/bridge/Inspector { - public static fun connect (ILcom/facebook/react/bridge/Inspector$RemoteConnection;)Lcom/facebook/react/bridge/Inspector$LocalConnection; - public static fun getPages ()Ljava/util/List; +public final class com/facebook/react/bridge/Inspector { + public static final field Companion Lcom/facebook/react/bridge/Inspector$Companion; + public static final fun connect (ILcom/facebook/react/bridge/Inspector$RemoteConnection;)Lcom/facebook/react/bridge/Inspector$LocalConnection; + public static final fun getPages ()Ljava/util/List; } -public class com/facebook/react/bridge/Inspector$LocalConnection { - public fun disconnect ()V - public fun sendMessage (Ljava/lang/String;)V +public final class com/facebook/react/bridge/Inspector$Companion { + public final fun connect (ILcom/facebook/react/bridge/Inspector$RemoteConnection;)Lcom/facebook/react/bridge/Inspector$LocalConnection; + public final fun getPages ()Ljava/util/List; } -public class com/facebook/react/bridge/Inspector$Page { - public fun getId ()I - public fun getTitle ()Ljava/lang/String; - public fun getVM ()Ljava/lang/String; +public final class com/facebook/react/bridge/Inspector$LocalConnection { + public final fun disconnect ()V + public final fun sendMessage (Ljava/lang/String;)V +} + +public final class com/facebook/react/bridge/Inspector$Page { + public final fun getId ()I + public final fun getTitle ()Ljava/lang/String; + public final fun getVM ()Ljava/lang/String; public fun toString ()Ljava/lang/String; } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/Inspector.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/Inspector.java deleted file mode 100644 index 4eb002fb089..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/Inspector.java +++ /dev/null @@ -1,112 +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 com.facebook.common.logging.FLog; -import com.facebook.infer.annotation.Nullsafe; -import com.facebook.jni.HybridData; -import com.facebook.proguard.annotations.DoNotStrip; -import com.facebook.react.common.ReactConstants; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -@Nullsafe(Nullsafe.Mode.LOCAL) -@DoNotStrip -public class Inspector { - static { - BridgeSoLoader.staticInit(); - } - - private final HybridData mHybridData; - - public static List getPages() { - try { - return Arrays.asList(instance().getPagesNative()); - } catch (UnsatisfiedLinkError e) { - FLog.e(ReactConstants.TAG, "Inspector doesn't work in open source yet", e); - return Collections.emptyList(); - } - } - - public static LocalConnection connect(int pageId, RemoteConnection remote) { - try { - final LocalConnection local = instance().connectNative(pageId, remote); - if (local == null) { - throw new IllegalStateException("Can't open failed connection"); - } - return local; - } catch (UnsatisfiedLinkError e) { - FLog.e(ReactConstants.TAG, "Inspector doesn't work in open source yet", e); - throw new RuntimeException(e); - } - } - - private static native Inspector instance(); - - private native Page[] getPagesNative(); - - private native LocalConnection connectNative(int pageId, RemoteConnection remote); - - private Inspector(HybridData hybridData) { - mHybridData = hybridData; - } - - @DoNotStrip - public static class Page { - private final int mId; - private final String mTitle; - private final String mVM; - - public int getId() { - return mId; - } - - public String getTitle() { - return mTitle; - } - - public String getVM() { - return mVM; - } - - @Override - public String toString() { - return "Page{" + "mId=" + mId + ", mTitle='" + mTitle + '\'' + '}'; - } - - @DoNotStrip - private Page(int id, String title, String vm) { - mId = id; - mTitle = title; - mVM = vm; - } - } - - @DoNotStrip - public interface RemoteConnection { - @DoNotStrip - void onMessage(String message); - - @DoNotStrip - void onDisconnect(); - } - - @DoNotStrip - public static class LocalConnection { - private final HybridData mHybridData; - - public native void sendMessage(String message); - - public native void disconnect(); - - private LocalConnection(HybridData hybridData) { - mHybridData = hybridData; - } - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/Inspector.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/Inspector.kt new file mode 100644 index 00000000000..2cab88aadc2 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/Inspector.kt @@ -0,0 +1,78 @@ +/* + * 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.jni.HybridData +import com.facebook.proguard.annotations.DoNotStrip +import com.facebook.react.common.ReactConstants + +@DoNotStrip +public class Inspector +private constructor(@Suppress("NoHungarianNotation") private val mHybridData: HybridData) { + + private external fun getPagesNative(): Array + + private external fun connectNative(pageId: Int, remote: RemoteConnection): LocalConnection? + + @DoNotStrip + public class Page + private constructor(private val id: Int, private val title: String, private val vm: String) { + public fun getId(): Int = id + + public fun getTitle(): String = title + + public fun getVM(): String = vm + + override fun toString(): String = "Page{id=$id, title='$title'}" + } + + @DoNotStrip + public interface RemoteConnection { + @DoNotStrip public fun onMessage(message: String) + + @DoNotStrip public fun onDisconnect() + } + + @DoNotStrip + public class LocalConnection + private constructor(@Suppress("NoHungarianNotation") private val mHybridData: HybridData) { + public external fun sendMessage(message: String) + + public external fun disconnect() + } + + public companion object { + init { + BridgeSoLoader.staticInit() + } + + @JvmStatic + public fun getPages(): List { + return try { + instance().getPagesNative().toList() + } catch (e: UnsatisfiedLinkError) { + FLog.e(ReactConstants.TAG, "Inspector doesn't work in open source yet", e) + emptyList() + } + } + + @JvmStatic + public fun connect(pageId: Int, remote: RemoteConnection): LocalConnection { + return try { + instance().connectNative(pageId, remote) + ?: throw IllegalStateException("Can't open failed connection") + } catch (e: UnsatisfiedLinkError) { + FLog.e(ReactConstants.TAG, "Inspector doesn't work in open source yet", e) + throw RuntimeException(e) + } + } + + @JvmStatic private external fun instance(): Inspector + } +}