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
This commit is contained in:
Fabrizio Cucci
2024-04-04 02:00:36 -07:00
committed by Facebook GitHub Bot
parent ce811a009d
commit a97109ef2e
2 changed files with 18 additions and 21 deletions
@@ -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 <init> ()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 {
@@ -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 <T>
* @return the first context which is an instance of the specified class, or null if none exists
*/
public static @Nullable <T> T findContextOfType(
@Nullable Context context, Class<? extends T> clazz) {
while (!(clazz.isInstance(context))) {
if (context instanceof ContextWrapper) {
Context baseContext = ((ContextWrapper) context).getBaseContext();
if (context == baseContext) {
return null;
@JvmStatic
public fun <T> findContextOfType(context: Context?, clazz: Class<out T>): 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?
}
}