Skip to content

Commit 9794f19

Browse files
committed
Add @Contract annotations and simplify code
1 parent 9ed20d7 commit 9794f19

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("true -> fail")
3234
static void assertFalse(boolean condition) {
3335
assertFalse(condition, (String) null);
3436
}
3537

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

45+
@Contract("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("null -> fail")
3133
static void assertNotNull(@Nullable Object actual) {
3234
assertNotNull(actual, (String) null);
3335
}
3436

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

44+
@Contract("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("!null -> fail")
3133
static void assertNull(@Nullable Object actual) {
3234
assertNull(actual, (String) null);
3335
}
3436

37+
@Contract("!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("!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("false -> fail")
3234
static void assertTrue(boolean condition) {
3335
assertTrue(condition, (String) null);
3436
}
3537

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

45+
@Contract("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(" -> fail")
3436
static void fail() {
3537
throw new AssertionFailedError();
3638
}
3739

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

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

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

55+
@Contract("_ -> 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
@@ -25,6 +25,7 @@
2525
import org.jspecify.annotations.Nullable;
2626
import org.junit.jupiter.api.function.Executable;
2727
import org.junit.jupiter.api.function.ThrowingSupplier;
28+
import org.junit.platform.commons.annotation.Contract;
2829
import org.opentest4j.MultipleFailuresError;
2930

3031
/**
@@ -115,6 +116,7 @@ protected Assertions() {
115116
* <p>See Javadoc for {@link #fail(String)} for an explanation of this method's
116117
* generic return type {@code V}.
117118
*/
119+
@Contract(" -> fail")
118120
@SuppressWarnings({ "NullAway", "TypeParameterUnusedInFormals" })
119121
public static <V> V fail() {
120122
AssertionUtils.fail();
@@ -135,6 +137,7 @@ public static <V> V fail() {
135137
* Stream.of().map(entry -> fail("should not be called"));
136138
* }</pre>
137139
*/
140+
@Contract("_ -> fail")
138141
@SuppressWarnings({ "NullAway", "TypeParameterUnusedInFormals" })
139142
public static <V> V fail(@Nullable String message) {
140143
AssertionUtils.fail(message);
@@ -148,6 +151,7 @@ public static <V> V fail(@Nullable String message) {
148151
* <p>See Javadoc for {@link #fail(String)} for an explanation of this method's
149152
* generic return type {@code V}.
150153
*/
154+
@Contract("_, _ -> fail")
151155
@SuppressWarnings({ "NullAway", "TypeParameterUnusedInFormals" })
152156
public static <V> V fail(@Nullable String message, @Nullable Throwable cause) {
153157
AssertionUtils.fail(message, cause);
@@ -160,6 +164,7 @@ public static <V> V fail(@Nullable String message, @Nullable Throwable cause) {
160164
* <p>See Javadoc for {@link #fail(String)} for an explanation of this method's
161165
* generic return type {@code V}.
162166
*/
167+
@Contract("_ -> fail")
163168
@SuppressWarnings({ "NullAway", "TypeParameterUnusedInFormals" })
164169
public static <V> V fail(@Nullable Throwable cause) {
165170
AssertionUtils.fail(cause);
@@ -173,6 +178,7 @@ public static <V> V fail(@Nullable Throwable cause) {
173178
* <p>See Javadoc for {@link #fail(String)} for an explanation of this method's
174179
* generic return type {@code V}.
175180
*/
181+
@Contract("_ -> fail")
176182
@SuppressWarnings({ "NullAway", "TypeParameterUnusedInFormals" })
177183
public static <V> V fail(Supplier<@Nullable String> messageSupplier) {
178184
AssertionUtils.fail(messageSupplier);
@@ -184,6 +190,7 @@ public static <V> V fail(Supplier<@Nullable String> messageSupplier) {
184190
/**
185191
* <em>Assert</em> that the supplied {@code condition} is {@code true}.
186192
*/
193+
@Contract("false -> fail")
187194
public static void assertTrue(boolean condition) {
188195
AssertTrue.assertTrue(condition);
189196
}
@@ -192,6 +199,7 @@ public static void assertTrue(boolean condition) {
192199
* <em>Assert</em> that the supplied {@code condition} is {@code true}.
193200
* <p>If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}.
194201
*/
202+
@Contract("false, _ -> fail")
195203
public static void assertTrue(boolean condition, Supplier<@Nullable String> messageSupplier) {
196204
AssertTrue.assertTrue(condition, messageSupplier);
197205
}
@@ -215,6 +223,7 @@ public static void assertTrue(BooleanSupplier booleanSupplier, @Nullable String
215223
* <em>Assert</em> that the supplied {@code condition} is {@code true}.
216224
* <p>Fails with the supplied failure {@code message}.
217225
*/
226+
@Contract("false, _ -> fail")
218227
public static void assertTrue(boolean condition, @Nullable String message) {
219228
AssertTrue.assertTrue(condition, message);
220229
}
@@ -232,6 +241,7 @@ public static void assertTrue(BooleanSupplier booleanSupplier, Supplier<@Nullabl
232241
/**
233242
* <em>Assert</em> that the supplied {@code condition} is {@code false}.
234243
*/
244+
@Contract("true -> fail")
235245
public static void assertFalse(boolean condition) {
236246
AssertFalse.assertFalse(condition);
237247
}
@@ -240,6 +250,7 @@ public static void assertFalse(boolean condition) {
240250
* <em>Assert</em> that the supplied {@code condition} is {@code false}.
241251
* <p>Fails with the supplied failure {@code message}.
242252
*/
253+
@Contract("true, _ -> fail")
243254
public static void assertFalse(boolean condition, @Nullable String message) {
244255
AssertFalse.assertFalse(condition, message);
245256
}
@@ -248,6 +259,7 @@ public static void assertFalse(boolean condition, @Nullable String message) {
248259
* <em>Assert</em> that the supplied {@code condition} is {@code false}.
249260
* <p>If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}.
250261
*/
262+
@Contract("true, _ -> fail")
251263
public static void assertFalse(boolean condition, Supplier<@Nullable String> messageSupplier) {
252264
AssertFalse.assertFalse(condition, messageSupplier);
253265
}
@@ -280,6 +292,7 @@ public static void assertFalse(BooleanSupplier booleanSupplier, Supplier<@Nullab
280292
/**
281293
* <em>Assert</em> that {@code actual} is {@code null}.
282294
*/
295+
@Contract("!null -> fail")
283296
public static void assertNull(@Nullable Object actual) {
284297
AssertNull.assertNull(actual);
285298
}
@@ -288,6 +301,7 @@ public static void assertNull(@Nullable Object actual) {
288301
* <em>Assert</em> that {@code actual} is {@code null}.
289302
* <p>Fails with the supplied failure {@code message}.
290303
*/
304+
@Contract("!null, _ -> fail")
291305
public static void assertNull(@Nullable Object actual, @Nullable String message) {
292306
AssertNull.assertNull(actual, message);
293307
}
@@ -296,6 +310,7 @@ public static void assertNull(@Nullable Object actual, @Nullable String message)
296310
* <em>Assert</em> that {@code actual} is {@code null}.
297311
* <p>If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}.
298312
*/
313+
@Contract("!null, _ -> fail")
299314
public static void assertNull(@Nullable Object actual, Supplier<@Nullable String> messageSupplier) {
300315
AssertNull.assertNull(actual, messageSupplier);
301316
}
@@ -305,6 +320,7 @@ public static void assertNull(@Nullable Object actual, Supplier<@Nullable String
305320
/**
306321
* <em>Assert</em> that {@code actual} is not {@code null}.
307322
*/
323+
@Contract("null -> fail")
308324
public static void assertNotNull(@Nullable Object actual) {
309325
AssertNotNull.assertNotNull(actual);
310326
}
@@ -313,6 +329,7 @@ public static void assertNotNull(@Nullable Object actual) {
313329
* <em>Assert</em> that {@code actual} is not {@code null}.
314330
* <p>Fails with the supplied failure {@code message}.
315331
*/
332+
@Contract("null, _ -> fail")
316333
public static void assertNotNull(@Nullable Object actual, @Nullable String message) {
317334
AssertNotNull.assertNotNull(actual, message);
318335
}
@@ -321,6 +338,7 @@ public static void assertNotNull(@Nullable Object actual, @Nullable String messa
321338
* <em>Assert</em> that {@code actual} is not {@code null}.
322339
* <p>If necessary, the failure message will be retrieved lazily from the supplied {@code messageSupplier}.
323340
*/
341+
@Contract("null, _ -> fail")
324342
public static void assertNotNull(@Nullable Object actual, Supplier<@Nullable String> messageSupplier) {
325343
AssertNotNull.assertNotNull(actual, messageSupplier);
326344
}
@@ -3665,6 +3683,7 @@ public static void assertTimeoutPreemptively(Duration timeout, Executable execut
36653683
* @since 5.8
36663684
*/
36673685
@API(status = STABLE, since = "5.10")
3686+
@Contract("_, null -> fail")
36683687
public static <T> T assertInstanceOf(Class<T> expectedType, @Nullable Object actualValue) {
36693688
return AssertInstanceOf.assertInstanceOf(expectedType, actualValue);
36703689
}
@@ -3681,6 +3700,7 @@ public static <T> T assertInstanceOf(Class<T> expectedType, @Nullable Object act
36813700
* @since 5.8
36823701
*/
36833702
@API(status = STABLE, since = "5.10")
3703+
@Contract("_, null, _ -> fail")
36843704
public static <T> T assertInstanceOf(Class<T> expectedType, @Nullable Object actualValue,
36853705
@Nullable String message) {
36863706
return AssertInstanceOf.assertInstanceOf(expectedType, actualValue, message);
@@ -3698,6 +3718,7 @@ public static <T> T assertInstanceOf(Class<T> expectedType, @Nullable Object act
36983718
*
36993719
* @since 5.8
37003720
*/
3721+
@Contract("_, null, _ -> fail")
37013722
@API(status = STABLE, since = "5.10")
37023723
public static <T> T assertInstanceOf(Class<T> expectedType, @Nullable Object actualValue,
37033724
Supplier<@Nullable String> messageSupplier) {

0 commit comments

Comments
 (0)