From a97109ef2ec1b74f6eaf53776423589e4e25f37b Mon Sep 17 00:00:00 2001 From: Fabrizio Cucci Date: Thu, 4 Apr 2024 02:00:36 -0700 Subject: [PATCH] Kotlinify ContextUtils (#43832) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43832 Changelog: [Internal] As part of the Sustainability Week (see [post](https://fb.workplace.com/groups/251759413609061/permalink/742797531171911/)). Reviewed By: arushikesarwani94 Differential Revision: D55708157 fbshipit-source-id: 8c89a569f5ace30c480dc562c2d8e4e5cab208dc --- .../ReactAndroid/api/ReactAndroid.api | 6 ++-- .../{ContextUtils.java => ContextUtils.kt} | 33 +++++++++---------- 2 files changed, 18 insertions(+), 21 deletions(-) rename packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/common/{ContextUtils.java => ContextUtils.kt} (51%) diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index df25ef1f266..bbad17e405b 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -5871,9 +5871,9 @@ public abstract interface class com/facebook/react/viewmanagers/UnimplementedNat public abstract fun setName (Landroid/view/View;Ljava/lang/String;)V } -public class com/facebook/react/views/common/ContextUtils { - public fun ()V - public static fun findContextOfType (Landroid/content/Context;Ljava/lang/Class;)Ljava/lang/Object; +public final class com/facebook/react/views/common/ContextUtils { + public static final field INSTANCE Lcom/facebook/react/views/common/ContextUtils; + public static final fun findContextOfType (Landroid/content/Context;Ljava/lang/Class;)Ljava/lang/Object; } public class com/facebook/react/views/common/ViewUtils { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/common/ContextUtils.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/common/ContextUtils.kt similarity index 51% rename from packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/common/ContextUtils.java rename to packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/common/ContextUtils.kt index 040070d45fd..9ca1d9fcc72 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/common/ContextUtils.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/common/ContextUtils.kt @@ -5,18 +5,15 @@ * LICENSE file in the root directory of this source tree. */ -package com.facebook.react.views.common; +package com.facebook.react.views.common -import android.content.Context; -import android.content.ContextWrapper; -import androidx.annotation.Nullable; -import com.facebook.infer.annotation.Nullsafe; +import android.content.Context +import android.content.ContextWrapper /** * Class containing static methods involving manipulations of Contexts and their related subclasses. */ -@Nullsafe(Nullsafe.Mode.LOCAL) -public class ContextUtils { +public object ContextUtils { /** * Returns the nearest context in the chain (as defined by ContextWrapper.getBaseContext()) which @@ -24,23 +21,23 @@ public class ContextUtils { * * @param context Initial context * @param clazz Class instance to look for - * @param * @return the first context which is an instance of the specified class, or null if none exists */ - public static @Nullable T findContextOfType( - @Nullable Context context, Class clazz) { - while (!(clazz.isInstance(context))) { - if (context instanceof ContextWrapper) { - Context baseContext = ((ContextWrapper) context).getBaseContext(); - if (context == baseContext) { - return null; + @JvmStatic + public fun findContextOfType(context: Context?, clazz: Class): T? { + var currentContext = context + while (!clazz.isInstance(currentContext)) { + if (currentContext is ContextWrapper) { + val baseContext = currentContext.baseContext + if (currentContext === baseContext) { + return null } else { - context = baseContext; + currentContext = baseContext } } else { - return null; + return null } } - return (T) context; + @Suppress("UNCHECKED_CAST") return currentContext as T? } }