Skip to content

Commit 2e65440

Browse files
committed
Add @Contract annotations and simplify code
1 parent 8caf50c commit 2e65440

File tree

19 files changed

+108
-37
lines changed

19 files changed

+108
-37
lines changed

junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertFalse.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.function.Supplier;
1717

1818
import org.jspecify.annotations.Nullable;
19+
import org.junit.platform.commons.annotation.Contract;
1920

2021
/**
2122
* {@code AssertFalse} is a collection of utility methods that support asserting
@@ -29,16 +30,19 @@ private AssertFalse() {
2930
/* no-op */
3031
}
3132

33+
@Contract(value = "true -> fail")
3234
static void assertFalse(boolean condition) {
3335
assertFalse(condition, (String) null);
3436
}
3537

38+
@Contract(value = "true, _ -> fail")
3639
static void assertFalse(boolean condition, @Nullable String message) {
3740
if (condition) {
3841
failNotFalse(message);
3942
}
4043
}
4144

45+
@Contract(value = "true, _ -> fail")
4246
static void assertFalse(boolean condition, Supplier<@Nullable String> messageSupplier) {
4347
if (condition) {
4448
failNotFalse(messageSupplier);

junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertInstanceOf.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.function.Supplier;
1616

1717
import org.jspecify.annotations.Nullable;
18+
import org.junit.platform.commons.annotation.Contract;
1819

1920
/**
2021
* {@code AssertInstanceOf} is a collection of utility methods that support
@@ -29,14 +30,17 @@ private AssertInstanceOf() {
2930
/* no-op */
3031
}
3132

33+
@Contract("_, null -> fail")
3234
static <T> T assertInstanceOf(Class<T> expectedType, @Nullable Object actualValue) {
3335
return assertInstanceOf(expectedType, actualValue, (Object) null);
3436
}
3537

38+
@Contract("_, null, _ -> fail")
3639
static <T> T assertInstanceOf(Class<T> expectedType, @Nullable Object actualValue, @Nullable String message) {
3740
return assertInstanceOf(expectedType, actualValue, (Object) message);
3841
}
3942

43+
@Contract("_, null, _ -> fail")
4044
static <T> T assertInstanceOf(Class<T> expectedType, @Nullable Object actualValue,
4145
Supplier<@Nullable String> messageSupplier) {
4246
return assertInstanceOf(expectedType, actualValue, (Object) messageSupplier);

junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertNotNull.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.function.Supplier;
1616

1717
import org.jspecify.annotations.Nullable;
18+
import org.junit.platform.commons.annotation.Contract;
1819

1920
/**
2021
* {@code AssertNotNull} is a collection of utility methods that support asserting
@@ -28,16 +29,19 @@ private AssertNotNull() {
2829
/* no-op */
2930
}
3031

32+
@Contract(value = "null -> fail")
3133
static void assertNotNull(@Nullable Object actual) {
3234
assertNotNull(actual, (String) null);
3335
}
3436

37+
@Contract(value = "null, _ -> fail")
3538
static void assertNotNull(@Nullable Object actual, @Nullable String message) {
3639
if (actual == null) {
3740
failNull(message);
3841
}
3942
}
4043

44+
@Contract(value = "null, _ -> fail")
4145
static void assertNotNull(@Nullable Object actual, Supplier<@Nullable String> messageSupplier) {
4246
if (actual == null) {
4347
failNull(messageSupplier);

junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertNull.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.function.Supplier;
1616

1717
import org.jspecify.annotations.Nullable;
18+
import org.junit.platform.commons.annotation.Contract;
1819

1920
/**
2021
* {@code AssertNull} is a collection of utility methods that support asserting
@@ -28,16 +29,19 @@ private AssertNull() {
2829
/* no-op */
2930
}
3031

32+
@Contract(value = "!null -> fail")
3133
static void assertNull(@Nullable Object actual) {
3234
assertNull(actual, (String) null);
3335
}
3436

37+
@Contract(value = "!null, _ -> fail")
3538
static void assertNull(@Nullable Object actual, @Nullable String message) {
3639
if (actual != null) {
3740
failNotNull(actual, message);
3841
}
3942
}
4043

44+
@Contract(value = "!null, _ -> fail")
4145
static void assertNull(@Nullable Object actual, Supplier<@Nullable String> messageSupplier) {
4246
if (actual != null) {
4347
failNotNull(actual, messageSupplier);

junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertTrue.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.function.Supplier;
1717

1818
import org.jspecify.annotations.Nullable;
19+
import org.junit.platform.commons.annotation.Contract;
1920

2021
/**
2122
* {@code AssertTrue} is a collection of utility methods that support asserting
@@ -29,16 +30,19 @@ private AssertTrue() {
2930
/* no-op */
3031
}
3132

33+
@Contract(value = "false -> fail")
3234
static void assertTrue(boolean condition) {
3335
assertTrue(condition, (String) null);
3436
}
3537

38+
@Contract(value = "false, _ -> fail")
3639
static void assertTrue(boolean condition, @Nullable String message) {
3740
if (!condition) {
3841
failNotTrue(message);
3942
}
4043
}
4144

45+
@Contract(value = "false, _ -> fail")
4246
static void assertTrue(boolean condition, Supplier<@Nullable String> messageSupplier) {
4347
if (!condition) {
4448
failNotTrue(messageSupplier);

junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertionUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.function.Supplier;
1717

1818
import org.jspecify.annotations.Nullable;
19+
import org.junit.platform.commons.annotation.Contract;
1920
import org.junit.platform.commons.util.UnrecoverableExceptions;
2021
import org.opentest4j.AssertionFailedError;
2122

@@ -31,22 +32,27 @@ private AssertionUtils() {
3132
/* no-op */
3233
}
3334

35+
@Contract(value = " -> fail")
3436
static void fail() {
3537
throw new AssertionFailedError();
3638
}
3739

40+
@Contract(value = "_ -> fail")
3841
static void fail(@Nullable String message) {
3942
throw new AssertionFailedError(message);
4043
}
4144

45+
@Contract(value = "_, _ -> fail")
4246
static void fail(@Nullable String message, @Nullable Throwable cause) {
4347
throw new AssertionFailedError(message, cause);
4448
}
4549

50+
@Contract(value = "_ -> fail")
4651
static void fail(@Nullable Throwable cause) {
4752
throw new AssertionFailedError(null, cause);
4853
}
4954

55+
@Contract(value = "_ -> fail")
5056
static void fail(Supplier<@Nullable String> messageSupplier) {
5157
throw new AssertionFailedError(nullSafeGet(messageSupplier));
5258
}

junit-jupiter-api/src/main/java/org/junit/jupiter/api/Assertions.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.jspecify.annotations.Nullable;
2727
import org.junit.jupiter.api.function.Executable;
2828
import org.junit.jupiter.api.function.ThrowingSupplier;
29+
import org.junit.platform.commons.annotation.Contract;
2930
import org.opentest4j.MultipleFailuresError;
3031

3132
/**
@@ -116,6 +117,7 @@ protected Assertions() {
116117
* <p>See Javadoc for {@link #fail(String)} for an explanation of this method's
117118
* generic return type {@code V}.
118119
*/
120+
@Contract(value = " -> fail")
119121
@SuppressWarnings({ "NullAway", "TypeParameterUnusedInFormals" })
120122
public static <V> V fail() {
121123
AssertionUtils.fail();
@@ -136,6 +138,7 @@ public static <V> V fail() {
136138
* Stream.of().map(entry -> fail("should not be called"));
137139
* }</pre>
138140
*/
141+
@Contract(value = "_ -> fail")
139142
@SuppressWarnings({ "NullAway", "TypeParameterUnusedInFormals" })
140143
public static <V> V fail(@Nullable String message) {
141144
AssertionUtils.fail(message);
@@ -149,6 +152,7 @@ public static <V> V fail(@Nullable String message) {
149152
* <p>See Javadoc for {@link #fail(String)} for an explanation of this method's
150153
* generic return type {@code V}.
151154
*/
155+
@Contract(value = "_, _ -> fail")
152156
@SuppressWarnings({ "NullAway", "TypeParameterUnusedInFormals" })
153157
public static <V> V fail(@Nullable String message, @Nullable Throwable cause) {
154158
AssertionUtils.fail(message, cause);
@@ -161,6 +165,7 @@ public static <V> V fail(@Nullable String message, @Nullable Throwable cause) {
161165
* <p>See Javadoc for {@link #fail(String)} for an explanation of this method's
162166
* generic return type {@code V}.
163167
*/
168+
@Contract(value = "_ -> fail")
164169
@SuppressWarnings({ "NullAway", "TypeParameterUnusedInFormals" })
165170
public static <V> V fail(@Nullable Throwable cause) {
166171
AssertionUtils.fail(cause);
@@ -174,6 +179,7 @@ public static <V> V fail(@Nullable Throwable cause) {
174179
* <p>See Javadoc for {@link #fail(String)} for an explanation of this method's
175180
* generic return type {@code V}.
176181
*/
182+
@Contract(value = "_ -> fail")
177183
@SuppressWarnings({ "NullAway", "TypeParameterUnusedInFormals" })
178184
public static <V> V fail(Supplier<@Nullable String> messageSupplier) {
179185
AssertionUtils.fail(messageSupplier);
@@ -185,6 +191,7 @@ public static <V> V fail(Supplier<@Nullable String> messageSupplier) {
185191
/**
186192
* <em>Assert</em> that the supplied {@code condition} is {@code true}.
187193
*/
194+
@Contract(value = "false -> fail")
188195
public static void assertTrue(boolean condition) {
189196
AssertTrue.assertTrue(condition);
190197
}
@@ -193,6 +200,7 @@ public static void assertTrue(boolean condition) {
193200
* <em>Assert</em> that the supplied {@code condition} is {@code true}.
194201
* <p>If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}.
195202
*/
203+
@Contract(value = "false, _ -> fail")
196204
public static void assertTrue(boolean condition, Supplier<@Nullable String> messageSupplier) {
197205
AssertTrue.assertTrue(condition, messageSupplier);
198206
}
@@ -216,6 +224,7 @@ public static void assertTrue(BooleanSupplier booleanSupplier, @Nullable String
216224
* <em>Assert</em> that the supplied {@code condition} is {@code true}.
217225
* <p>Fails with the supplied failure {@code message}.
218226
*/
227+
@Contract(value = "false, _ -> fail")
219228
public static void assertTrue(boolean condition, @Nullable String message) {
220229
AssertTrue.assertTrue(condition, message);
221230
}
@@ -233,6 +242,7 @@ public static void assertTrue(BooleanSupplier booleanSupplier, Supplier<@Nullabl
233242
/**
234243
* <em>Assert</em> that the supplied {@code condition} is {@code false}.
235244
*/
245+
@Contract(value = "true -> fail")
236246
public static void assertFalse(boolean condition) {
237247
AssertFalse.assertFalse(condition);
238248
}
@@ -241,6 +251,7 @@ public static void assertFalse(boolean condition) {
241251
* <em>Assert</em> that the supplied {@code condition} is {@code false}.
242252
* <p>Fails with the supplied failure {@code message}.
243253
*/
254+
@Contract(value = "true, _ -> fail")
244255
public static void assertFalse(boolean condition, @Nullable String message) {
245256
AssertFalse.assertFalse(condition, message);
246257
}
@@ -249,6 +260,7 @@ public static void assertFalse(boolean condition, @Nullable String message) {
249260
* <em>Assert</em> that the supplied {@code condition} is {@code false}.
250261
* <p>If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}.
251262
*/
263+
@Contract(value = "true, _ -> fail")
252264
public static void assertFalse(boolean condition, Supplier<@Nullable String> messageSupplier) {
253265
AssertFalse.assertFalse(condition, messageSupplier);
254266
}
@@ -281,6 +293,7 @@ public static void assertFalse(BooleanSupplier booleanSupplier, Supplier<@Nullab
281293
/**
282294
* <em>Assert</em> that {@code actual} is {@code null}.
283295
*/
296+
@Contract(value = "!null -> fail")
284297
public static void assertNull(@Nullable Object actual) {
285298
AssertNull.assertNull(actual);
286299
}
@@ -289,6 +302,7 @@ public static void assertNull(@Nullable Object actual) {
289302
* <em>Assert</em> that {@code actual} is {@code null}.
290303
* <p>Fails with the supplied failure {@code message}.
291304
*/
305+
@Contract(value = "!null, _ -> fail")
292306
public static void assertNull(@Nullable Object actual, @Nullable String message) {
293307
AssertNull.assertNull(actual, message);
294308
}
@@ -297,6 +311,7 @@ public static void assertNull(@Nullable Object actual, @Nullable String message)
297311
* <em>Assert</em> that {@code actual} is {@code null}.
298312
* <p>If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}.
299313
*/
314+
@Contract(value = "!null, _ -> fail")
300315
public static void assertNull(@Nullable Object actual, Supplier<@Nullable String> messageSupplier) {
301316
AssertNull.assertNull(actual, messageSupplier);
302317
}
@@ -306,6 +321,7 @@ public static void assertNull(@Nullable Object actual, Supplier<@Nullable String
306321
/**
307322
* <em>Assert</em> that {@code actual} is not {@code null}.
308323
*/
324+
@Contract(value = "null -> fail")
309325
public static void assertNotNull(@Nullable Object actual) {
310326
AssertNotNull.assertNotNull(actual);
311327
}
@@ -314,6 +330,7 @@ public static void assertNotNull(@Nullable Object actual) {
314330
* <em>Assert</em> that {@code actual} is not {@code null}.
315331
* <p>Fails with the supplied failure {@code message}.
316332
*/
333+
@Contract(value = "null, _ -> fail")
317334
public static void assertNotNull(@Nullable Object actual, @Nullable String message) {
318335
AssertNotNull.assertNotNull(actual, message);
319336
}
@@ -322,6 +339,7 @@ public static void assertNotNull(@Nullable Object actual, @Nullable String messa
322339
* <em>Assert</em> that {@code actual} is not {@code null}.
323340
* <p>If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}.
324341
*/
342+
@Contract(value = "null, _ -> fail")
325343
public static void assertNotNull(@Nullable Object actual, Supplier<@Nullable String> messageSupplier) {
326344
AssertNotNull.assertNotNull(actual, messageSupplier);
327345
}
@@ -3696,6 +3714,7 @@ public static void assertTimeoutPreemptively(Duration timeout, Executable execut
36963714
* @since 5.8
36973715
*/
36983716
@API(status = STABLE, since = "5.10")
3717+
@Contract("_, null -> fail")
36993718
public static <T> T assertInstanceOf(Class<T> expectedType, @Nullable Object actualValue) {
37003719
return AssertInstanceOf.assertInstanceOf(expectedType, actualValue);
37013720
}
@@ -3712,6 +3731,7 @@ public static <T> T assertInstanceOf(Class<T> expectedType, @Nullable Object act
37123731
* @since 5.8
37133732
*/
37143733
@API(status = STABLE, since = "5.10")
3734+
@Contract("_, null, _ -> fail")
37153735
public static <T> T assertInstanceOf(Class<T> expectedType, @Nullable Object actualValue,
37163736
@Nullable String message) {
37173737
return AssertInstanceOf.assertInstanceOf(expectedType, actualValue, message);
@@ -3729,6 +3749,7 @@ public static <T> T assertInstanceOf(Class<T> expectedType, @Nullable Object act
37293749
*
37303750
* @since 5.8
37313751
*/
3752+
@Contract("_, null, _ -> fail")
37323753
@API(status = STABLE, since = "5.10")
37333754
public static <T> T assertInstanceOf(Class<T> expectedType, @Nullable Object actualValue,
37343755
Supplier<@Nullable String> messageSupplier) {

0 commit comments

Comments
 (0)