From 51552e6a1ec1023e00a8696c4cba69f0229eed32 Mon Sep 17 00:00:00 2001 From: Fabrizio Cucci Date: Tue, 9 Apr 2024 09:10:50 -0700 Subject: [PATCH] Kotlinify SoftAssertions (#43911) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43911 Changelog: [Internal] As part of the Sustainability Week (see [post](https://fb.workplace.com/groups/251759413609061/permalink/742797531171911/)). Reviewed By: tdn120 Differential Revision: D55797031 fbshipit-source-id: eb53ae671b0a7b5d277b931a11eb0fcd84c51a19 --- .../ReactAndroid/api/ReactAndroid.api | 10 ++-- .../facebook/react/bridge/SoftAssertions.java | 53 ------------------- .../facebook/react/bridge/SoftAssertions.kt | 53 +++++++++++++++++++ 3 files changed, 58 insertions(+), 58 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/SoftAssertions.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/SoftAssertions.kt diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 8a4e10def6ee8a..bb08f31c64eb97 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -1466,11 +1466,11 @@ public class com/facebook/react/bridge/RuntimeScheduler { public fun (Lcom/facebook/jni/HybridData;)V } -public class com/facebook/react/bridge/SoftAssertions { - public fun ()V - public static fun assertCondition (ZLjava/lang/String;)V - public static fun assertNotNull (Ljava/lang/Object;)Ljava/lang/Object; - public static fun assertUnreachable (Ljava/lang/String;)V +public final class com/facebook/react/bridge/SoftAssertions { + public static final field INSTANCE Lcom/facebook/react/bridge/SoftAssertions; + public static final fun assertCondition (ZLjava/lang/String;)V + public static final fun assertNotNull (Ljava/lang/Object;)Ljava/lang/Object; + public static final fun assertUnreachable (Ljava/lang/String;)V } public abstract interface class com/facebook/react/bridge/Systrace : com/facebook/react/bridge/JavaScriptModule { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/SoftAssertions.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/SoftAssertions.java deleted file mode 100644 index e69f95cd0c3596..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/SoftAssertions.java +++ /dev/null @@ -1,53 +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; - -/** - * Utility class to make assertions that should not hard-crash the app but instead be handled by the - * Catalyst app {@link JSExceptionHandler}. See the javadoc on that class for more information about - * our opinion on when these assertions should be used as opposed to assertions that might throw - * AssertionError Throwables that will cause the app to hard crash. - */ -public class SoftAssertions { - - /** - * Throw {@link AssertionException} with a given message. Use this method surrounded with {@code - * if} block with assert condition in case you plan to do string concatenation to produce the - * message. This logs an assertion with ReactSoftExceptionLogger, which decides whether or not to - * actually throw. - */ - public static void assertUnreachable(String message) { - ReactSoftExceptionLogger.logSoftException("SoftAssertions", new AssertionException(message)); - } - - /** - * Asserts the given condition, throwing an {@link AssertionException} if the condition doesn't - * hold. This logs an assertion with ReactSoftExceptionLogger, which decides whether or not to - * actually throw. - */ - public static void assertCondition(boolean condition, String message) { - if (!condition) { - ReactSoftExceptionLogger.logSoftException("SoftAssertions", new AssertionException(message)); - } - } - - /** - * Asserts that the given Object isn't null, throwing an {@link AssertionException} if it was. - * This logs an assertion with ReactSoftExceptionLogger, which decides whether or not to actually - * throw. - */ - public static T assertNotNull(@Nullable T instance) { - if (instance == null) { - ReactSoftExceptionLogger.logSoftException( - "SoftAssertions", new AssertionException("Expected object to not be null!")); - } - return instance; - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/SoftAssertions.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/SoftAssertions.kt new file mode 100644 index 00000000000000..111006f34d7bae --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/SoftAssertions.kt @@ -0,0 +1,53 @@ +/* + * 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 + +/** + * Utility class to make assertions that should not hard-crash the app but instead be handled by the + * Catalyst app [JSExceptionHandler]. See the javadoc on that class for more information about our + * opinion on when these assertions should be used as opposed to assertions that might throw + * AssertionError Throwables that will cause the app to hard crash. + */ +public object SoftAssertions { + + /** + * Throw [AssertionException] with a given message. Use this method surrounded with `if` block + * with assert condition in case you plan to do string concatenation to produce the message. This + * logs an assertion with ReactSoftExceptionLogger, which decides whether or not to actually + * throw. + */ + @JvmStatic + public fun assertUnreachable(message: String): Unit { + ReactSoftExceptionLogger.logSoftException("SoftAssertions", AssertionException(message)) + } + + /** + * Asserts the given condition, throwing an [AssertionException] if the condition doesn't hold. + * This logs an assertion with ReactSoftExceptionLogger, which decides whether or not to actually + * throw. + */ + @JvmStatic + public fun assertCondition(condition: Boolean, message: String): Unit { + if (!condition) { + ReactSoftExceptionLogger.logSoftException("SoftAssertions", AssertionException(message)) + } + } + + /** + * Asserts that the given object isn't null, throwing an [AssertionException] if it was. This logs + * an assertion with ReactSoftExceptionLogger, which decides whether or not to actually throw. + */ + @JvmStatic + public fun assertNotNull(instance: T?): T? { + if (instance == null) { + ReactSoftExceptionLogger.logSoftException( + "SoftAssertions", AssertionException("Expected object to not be null!")) + } + return instance + } +}