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
This commit is contained in:
Fabrizio Cucci
2024-04-09 09:10:50 -07:00
committed by Facebook GitHub Bot
parent 8b8b85bb1f
commit 51552e6a1e
3 changed files with 58 additions and 58 deletions
@@ -1466,11 +1466,11 @@ public class com/facebook/react/bridge/RuntimeScheduler {
public fun <init> (Lcom/facebook/jni/HybridData;)V
}
public class com/facebook/react/bridge/SoftAssertions {
public fun <init> ()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 {
@@ -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> T assertNotNull(@Nullable T instance) {
if (instance == null) {
ReactSoftExceptionLogger.logSoftException(
"SoftAssertions", new AssertionException("Expected object to not be null!"));
}
return instance;
}
}
@@ -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 <T> assertNotNull(instance: T?): T? {
if (instance == null) {
ReactSoftExceptionLogger.logSoftException(
"SoftAssertions", AssertionException("Expected object to not be null!"))
}
return instance
}
}