Skip to content

Commit 51552e6

Browse files
fabriziocuccifacebook-github-bot
authored andcommitted
Kotlinify SoftAssertions (#43911)
Summary: Pull Request resolved: #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
1 parent 8b8b85b commit 51552e6

File tree

3 files changed

+58
-58
lines changed

3 files changed

+58
-58
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,11 +1466,11 @@ public class com/facebook/react/bridge/RuntimeScheduler {
14661466
public fun <init> (Lcom/facebook/jni/HybridData;)V
14671467
}
14681468

1469-
public class com/facebook/react/bridge/SoftAssertions {
1470-
public fun <init> ()V
1471-
public static fun assertCondition (ZLjava/lang/String;)V
1472-
public static fun assertNotNull (Ljava/lang/Object;)Ljava/lang/Object;
1473-
public static fun assertUnreachable (Ljava/lang/String;)V
1469+
public final class com/facebook/react/bridge/SoftAssertions {
1470+
public static final field INSTANCE Lcom/facebook/react/bridge/SoftAssertions;
1471+
public static final fun assertCondition (ZLjava/lang/String;)V
1472+
public static final fun assertNotNull (Ljava/lang/Object;)Ljava/lang/Object;
1473+
public static final fun assertUnreachable (Ljava/lang/String;)V
14741474
}
14751475

14761476
public abstract interface class com/facebook/react/bridge/Systrace : com/facebook/react/bridge/JavaScriptModule {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/SoftAssertions.java

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.bridge
9+
10+
/**
11+
* Utility class to make assertions that should not hard-crash the app but instead be handled by the
12+
* Catalyst app [JSExceptionHandler]. See the javadoc on that class for more information about our
13+
* opinion on when these assertions should be used as opposed to assertions that might throw
14+
* AssertionError Throwables that will cause the app to hard crash.
15+
*/
16+
public object SoftAssertions {
17+
18+
/**
19+
* Throw [AssertionException] with a given message. Use this method surrounded with `if` block
20+
* with assert condition in case you plan to do string concatenation to produce the message. This
21+
* logs an assertion with ReactSoftExceptionLogger, which decides whether or not to actually
22+
* throw.
23+
*/
24+
@JvmStatic
25+
public fun assertUnreachable(message: String): Unit {
26+
ReactSoftExceptionLogger.logSoftException("SoftAssertions", AssertionException(message))
27+
}
28+
29+
/**
30+
* Asserts the given condition, throwing an [AssertionException] if the condition doesn't hold.
31+
* This logs an assertion with ReactSoftExceptionLogger, which decides whether or not to actually
32+
* throw.
33+
*/
34+
@JvmStatic
35+
public fun assertCondition(condition: Boolean, message: String): Unit {
36+
if (!condition) {
37+
ReactSoftExceptionLogger.logSoftException("SoftAssertions", AssertionException(message))
38+
}
39+
}
40+
41+
/**
42+
* Asserts that the given object isn't null, throwing an [AssertionException] if it was. This logs
43+
* an assertion with ReactSoftExceptionLogger, which decides whether or not to actually throw.
44+
*/
45+
@JvmStatic
46+
public fun <T> assertNotNull(instance: T?): T? {
47+
if (instance == null) {
48+
ReactSoftExceptionLogger.logSoftException(
49+
"SoftAssertions", AssertionException("Expected object to not be null!"))
50+
}
51+
return instance
52+
}
53+
}

0 commit comments

Comments
 (0)