Skip to content

Commit feb5f6f

Browse files
authored
Annotate fail methods with custom @Contract (#3882)
1 parent 43e8b65 commit feb5f6f

9 files changed

Lines changed: 96 additions & 5 deletions

File tree

assertj-core/src/main/java/org/assertj/core/api/AbstractAssert.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.assertj.core.internal.Conditions;
5252
import org.assertj.core.internal.Failures;
5353
import org.assertj.core.internal.Objects;
54+
import org.assertj.core.internal.annotation.Contract;
5455
import org.assertj.core.presentation.PredicateDescription;
5556
import org.assertj.core.presentation.Representation;
5657
import org.assertj.core.util.VisibleForTesting;
@@ -136,6 +137,7 @@ public WritableAssertionInfo getWritableAssertionInfo() {
136137
* @see #failWithActualExpectedAndMessage(Object, Object, String, Object...)
137138
* @see #failure(String, Object...)
138139
*/
140+
@Contract("_, _ -> fail")
139141
protected void failWithMessage(String errorMessage, Object... arguments) {
140142
throw failure(errorMessage, arguments);
141143
}
@@ -198,6 +200,7 @@ protected AssertionError failure(String errorMessage, Object... arguments) {
198200
* @see #failWithMessage(String, Object...)
199201
* @see #failureWithActualExpected(Object, Object, String, Object...)
200202
*/
203+
@Contract("_, _, _, _ -> fail")
201204
protected void failWithActualExpectedAndMessage(Object actual, Object expected, String errorMessageFormat,
202205
Object... arguments) {
203206
throw failureWithActualExpected(actual, expected, errorMessageFormat, arguments);

assertj-core/src/main/java/org/assertj/core/api/AbstractSoftAssertions.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.assertj.core.annotation.CanIgnoreReturnValue;
2020
import org.assertj.core.error.AssertionErrorCreator;
2121
import org.assertj.core.internal.Failures;
22+
import org.assertj.core.internal.annotation.Contract;
2223

2324
public abstract class AbstractSoftAssertions extends DefaultAssertionErrorCollector
2425
implements SoftAssertionsProvider, InstanceOfAssertFactories {
@@ -57,6 +58,7 @@ public void assertAll() {
5758
* @since 2.6.0 / 3.6.0
5859
*/
5960
@CanIgnoreReturnValue
61+
@Contract("_ -> fail")
6062
public <T> T fail(String failureMessage) {
6163
AssertionError error = Failures.instance().failure(failureMessage);
6264
collectAssertionError(error);
@@ -72,6 +74,7 @@ public <T> T fail(String failureMessage) {
7274
* @since 3.26.0
7375
*/
7476
@CanIgnoreReturnValue
77+
@Contract(" -> fail")
7578
public <T> T fail() {
7679
// pass an empty string because passing null results in a "null" error message.
7780
return fail("");
@@ -87,6 +90,7 @@ public <T> T fail() {
8790
* @since 2.6.0 / 3.6.0
8891
*/
8992
@CanIgnoreReturnValue
93+
@Contract("_, _ -> fail")
9094
public <T> T fail(String failureMessage, Object... args) {
9195
return fail(format(failureMessage, args));
9296
}
@@ -101,6 +105,7 @@ public <T> T fail(String failureMessage, Object... args) {
101105
* @since 2.6.0 / 3.6.0
102106
*/
103107
@CanIgnoreReturnValue
108+
@Contract("_, _ -> fail")
104109
public <T> T fail(String failureMessage, Throwable realCause) {
105110
AssertionError error = Failures.instance().failure(failureMessage);
106111
error.initCause(realCause);
@@ -120,6 +125,7 @@ public <T> T fail(String failureMessage, Throwable realCause) {
120125
* @since 3.26.0
121126
*/
122127
@CanIgnoreReturnValue
128+
@Contract("_ -> fail")
123129
public <T> T fail(Throwable realCause) {
124130
return fail("", realCause);
125131
}
@@ -133,8 +139,9 @@ public <T> T fail(Throwable realCause) {
133139
* not been.
134140
* @since 2.6.0 / 3.6.0
135141
*
136-
* {@link Fail#shouldHaveThrown(Class)} can be used as a replacement.
142+
* @see #shouldHaveThrown(Class)
137143
*/
144+
@Contract("_ -> fail")
138145
public void failBecauseExceptionWasNotThrown(Class<? extends Throwable> throwableClass) {
139146
shouldHaveThrown(throwableClass);
140147
}
@@ -148,6 +155,7 @@ public void failBecauseExceptionWasNotThrown(Class<? extends Throwable> throwabl
148155
* not been.
149156
* @since 2.6.0 / 3.6.0
150157
*/
158+
@Contract("_ -> fail")
151159
public void shouldHaveThrown(Class<? extends Throwable> throwableClass) {
152160
AssertionError error = Failures.instance().expectedThrowableNotThrown(throwableClass);
153161
collectAssertionError(error);

assertj-core/src/main/java/org/assertj/core/api/Assertions.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
import org.assertj.core.description.Description;
103103
import org.assertj.core.groups.Properties;
104104
import org.assertj.core.groups.Tuple;
105+
import org.assertj.core.internal.annotation.Contract;
105106
import org.assertj.core.presentation.BinaryRepresentation;
106107
import org.assertj.core.presentation.HexadecimalRepresentation;
107108
import org.assertj.core.presentation.Representation;
@@ -1922,6 +1923,7 @@ public static void setRemoveAssertJRelatedElementsFromStackTrace(boolean removeA
19221923
* @throws AssertionError with the given message.
19231924
*/
19241925
@CanIgnoreReturnValue
1926+
@Contract("_ -> fail")
19251927
public static <T> T fail(String failureMessage) {
19261928
return Fail.fail(failureMessage);
19271929
}
@@ -1936,6 +1938,7 @@ public static <T> T fail(String failureMessage) {
19361938
* @since 3.26.0
19371939
*/
19381940
@CanIgnoreReturnValue
1941+
@Contract(" -> fail")
19391942
public static <T> T fail() {
19401943
return Fail.fail();
19411944
}
@@ -1950,6 +1953,7 @@ public static <T> T fail() {
19501953
* @throws AssertionError with the given built message.
19511954
*/
19521955
@CanIgnoreReturnValue
1956+
@Contract("_, _ -> fail")
19531957
public static <T> T fail(String failureMessage, Object... args) {
19541958
return Fail.fail(failureMessage, args);
19551959
}
@@ -1963,6 +1967,7 @@ public static <T> T fail(String failureMessage, Object... args) {
19631967
* @throws AssertionError with the given message and with the {@link Throwable} that caused the failure.
19641968
*/
19651969
@CanIgnoreReturnValue
1970+
@Contract("_, _ -> fail")
19661971
public static <T> T fail(String failureMessage, Throwable realCause) {
19671972
return Fail.fail(failureMessage, realCause);
19681973
}
@@ -1979,6 +1984,7 @@ public static <T> T fail(String failureMessage, Throwable realCause) {
19791984
* @throws AssertionError with the {@link Throwable} that caused the failure.
19801985
*/
19811986
@CanIgnoreReturnValue
1987+
@Contract("_ -> fail")
19821988
public static <T> T fail(Throwable realCause) {
19831989
// pass an empty string because passing null results in a "null" error message.
19841990
return fail("", realCause);
@@ -1997,6 +2003,7 @@ public static <T> T fail(Throwable realCause) {
19972003
* not been.
19982004
*/
19992005
@CanIgnoreReturnValue
2006+
@Contract("_ -> fail")
20002007
public static <T> T failBecauseExceptionWasNotThrown(Class<? extends Throwable> throwableClass) {
20012008
return Fail.shouldHaveThrown(throwableClass);
20022009
}
@@ -2011,6 +2018,7 @@ public static <T> T failBecauseExceptionWasNotThrown(Class<? extends Throwable>
20112018
* not been.
20122019
*/
20132020
@CanIgnoreReturnValue
2021+
@Contract("_ -> fail")
20142022
public static <T> T shouldHaveThrown(Class<? extends Throwable> throwableClass) {
20152023
return Fail.shouldHaveThrown(throwableClass);
20162024
}

assertj-core/src/main/java/org/assertj/core/api/AssertionsForClassTypes.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.assertj.core.data.Percentage;
6161
import org.assertj.core.groups.Properties;
6262
import org.assertj.core.groups.Tuple;
63+
import org.assertj.core.internal.annotation.Contract;
6364
import org.assertj.core.presentation.StandardRepresentation;
6465
import org.assertj.core.util.Files;
6566
import org.assertj.core.util.Paths;
@@ -1010,6 +1011,7 @@ public static void setRemoveAssertJRelatedElementsFromStackTrace(boolean removeA
10101011
* @param failureMessage error message.
10111012
* @throws AssertionError with the given message.
10121013
*/
1014+
@Contract("_ -> fail")
10131015
public static void fail(String failureMessage) {
10141016
Fail.fail(failureMessage);
10151017
}
@@ -1020,6 +1022,7 @@ public static void fail(String failureMessage) {
10201022
*
10211023
* @throws AssertionError without message.
10221024
*/
1025+
@Contract(" -> fail")
10231026
public static void fail() {
10241027
Fail.fail();
10251028
}
@@ -1032,6 +1035,7 @@ public static void fail() {
10321035
* @param realCause cause of the error.
10331036
* @throws AssertionError with the given message and with the {@link Throwable} that caused the failure.
10341037
*/
1038+
@Contract("_, _ -> fail")
10351039
public static void fail(String failureMessage, Throwable realCause) {
10361040
Fail.fail(failureMessage, realCause);
10371041
}
@@ -1043,6 +1047,7 @@ public static void fail(String failureMessage, Throwable realCause) {
10431047
* @param realCause cause of the error.
10441048
* @throws AssertionError with the {@link Throwable} that caused the failure.
10451049
*/
1050+
@Contract("_ -> fail")
10461051
public static void fail(Throwable realCause) {
10471052
Fail.fail(realCause);
10481053
}
@@ -1057,6 +1062,7 @@ public static void fail(Throwable realCause) {
10571062
* @throws AssertionError with a message explaining that a {@link Throwable} of given class was expected to be thrown but had
10581063
* not been.
10591064
*/
1065+
@Contract("_ -> fail")
10601066
public static void failBecauseExceptionWasNotThrown(Class<? extends Throwable> throwableClass) {
10611067
Fail.shouldHaveThrown(throwableClass);
10621068
}
@@ -1069,6 +1075,7 @@ public static void failBecauseExceptionWasNotThrown(Class<? extends Throwable> t
10691075
* @throws AssertionError with a message explaining that a {@link Throwable} of given class was expected to be thrown but had
10701076
* not been.
10711077
*/
1078+
@Contract("_ -> fail")
10721079
public static void shouldHaveThrown(Class<? extends Throwable> throwableClass) {
10731080
Fail.shouldHaveThrown(throwableClass);
10741081
}

assertj-core/src/main/java/org/assertj/core/api/BDDAssertions.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
import org.assertj.core.description.Description;
9494
import org.assertj.core.groups.Properties;
9595
import org.assertj.core.groups.Tuple;
96+
import org.assertj.core.internal.annotation.Contract;
9697
import org.assertj.core.presentation.BinaryRepresentation;
9798
import org.assertj.core.presentation.HexadecimalRepresentation;
9899
import org.assertj.core.presentation.Representation;
@@ -2415,6 +2416,7 @@ public static void setRemoveAssertJRelatedElementsFromStackTrace(boolean removeA
24152416
* @since 3.20.0
24162417
*/
24172418
@CanIgnoreReturnValue
2419+
@Contract("_ -> fail")
24182420
public static <T> T fail(String failureMessage) {
24192421
return Assertions.fail(failureMessage);
24202422
}
@@ -2429,6 +2431,7 @@ public static <T> T fail(String failureMessage) {
24292431
* @since 3.26.0
24302432
*/
24312433
@CanIgnoreReturnValue
2434+
@Contract(" -> fail")
24322435
public static <T> T fail() {
24332436
return Assertions.fail();
24342437
}
@@ -2445,6 +2448,7 @@ public static <T> T fail() {
24452448
* @since 3.20.0
24462449
*/
24472450
@CanIgnoreReturnValue
2451+
@Contract("_, _ -> fail")
24482452
public static <T> T fail(String failureMessage, Object... args) {
24492453
return Assertions.fail(failureMessage, args);
24502454
}
@@ -2460,6 +2464,7 @@ public static <T> T fail(String failureMessage, Object... args) {
24602464
* @since 3.20.0
24612465
*/
24622466
@CanIgnoreReturnValue
2467+
@Contract("_, _ -> fail")
24632468
public static <T> T fail(String failureMessage, Throwable realCause) {
24642469
return Assertions.fail(failureMessage, realCause);
24652470
}
@@ -2472,6 +2477,7 @@ public static <T> T fail(String failureMessage, Throwable realCause) {
24722477
* @throws AssertionError with the {@link Throwable} that caused the failure.
24732478
*/
24742479
@CanIgnoreReturnValue
2480+
@Contract("_ -> fail")
24752481
public static <T> T fail(Throwable realCause) {
24762482
return fail(null, realCause);
24772483
}
@@ -2488,6 +2494,7 @@ public static <T> T fail(Throwable realCause) {
24882494
* @since 3.20.0
24892495
*/
24902496
@CanIgnoreReturnValue
2497+
@Contract("_ -> fail")
24912498
public static <T> T shouldHaveThrown(Class<? extends Throwable> throwableClass) {
24922499
return Assertions.shouldHaveThrown(throwableClass);
24932500
}

assertj-core/src/main/java/org/assertj/core/api/Fail.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import org.assertj.core.annotation.CanIgnoreReturnValue;
1616
import org.assertj.core.internal.Failures;
17+
import org.assertj.core.internal.annotation.Contract;
1718

1819
/**
1920
* Common failures.
@@ -42,6 +43,7 @@ public static void setRemoveAssertJRelatedElementsFromStackTrace(boolean removeA
4243
* @throws AssertionError with the given message.
4344
*/
4445
@CanIgnoreReturnValue
46+
@Contract("_ -> fail")
4547
public static <T> T fail(String failureMessage) {
4648
throw Failures.instance().failure(failureMessage);
4749
}
@@ -56,6 +58,7 @@ public static <T> T fail(String failureMessage) {
5658
* @since 3.26.0
5759
*/
5860
@CanIgnoreReturnValue
61+
@Contract(" -> fail")
5962
public static <T> T fail() {
6063
// pass an empty string because passing null results in a "null" error message.
6164
return fail("");
@@ -71,6 +74,7 @@ public static <T> T fail() {
7174
* @throws AssertionError with the given built message.
7275
*/
7376
@CanIgnoreReturnValue
77+
@Contract("_, _ -> fail")
7478
public static <T> T fail(String failureMessage, Object... args) {
7579
return fail(String.format(failureMessage, args));
7680
}
@@ -85,6 +89,7 @@ public static <T> T fail(String failureMessage, Object... args) {
8589
* @throws AssertionError with the given message and with the {@link Throwable} that caused the failure.
8690
*/
8791
@CanIgnoreReturnValue
92+
@Contract("_, _ -> fail")
8893
public static <T> T fail(String failureMessage, Throwable realCause) {
8994
AssertionError error = Failures.instance().failure(failureMessage);
9095
error.initCause(realCause);
@@ -100,6 +105,7 @@ public static <T> T fail(String failureMessage, Throwable realCause) {
100105
* @throws AssertionError with the {@link Throwable} that caused the failure.
101106
*/
102107
@CanIgnoreReturnValue
108+
@Contract("_ -> fail")
103109
public static <T> T fail(Throwable realCause) {
104110
return fail(null, realCause);
105111
}
@@ -114,9 +120,10 @@ public static <T> T fail(Throwable realCause) {
114120
* @throws AssertionError with a message explaining that a {@link Throwable} of given class was expected to be thrown but had
115121
* not been.
116122
*
117-
* {@link Fail#shouldHaveThrown(Class)} can be used as a replacement.
123+
* @see #shouldHaveThrown(Class)
118124
*/
119125
@CanIgnoreReturnValue
126+
@Contract("_ -> fail")
120127
public static <T> T failBecauseExceptionWasNotThrown(Class<? extends Throwable> throwableClass) {
121128
return shouldHaveThrown(throwableClass);
122129
}
@@ -132,6 +139,7 @@ public static <T> T failBecauseExceptionWasNotThrown(Class<? extends Throwable>
132139
* not been.
133140
*/
134141
@CanIgnoreReturnValue
142+
@Contract("_ -> fail")
135143
public static <T> T shouldHaveThrown(Class<? extends Throwable> throwableClass) {
136144
throw Failures.instance().expectedThrowableNotThrown(throwableClass);
137145
}

assertj-core/src/main/java/org/assertj/core/api/Java6Assertions.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.assertj.core.data.Percentage;
6262
import org.assertj.core.groups.Properties;
6363
import org.assertj.core.groups.Tuple;
64+
import org.assertj.core.internal.annotation.Contract;
6465
import org.assertj.core.presentation.StandardRepresentation;
6566
import org.assertj.core.util.Files;
6667
import org.assertj.core.util.URLs;
@@ -1438,6 +1439,7 @@ public static <THROWABLE extends Throwable> THROWABLE catchThrowableOfType(Class
14381439
* @param failureMessage error message.
14391440
* @throws AssertionError with the given message.
14401441
*/
1442+
@Contract("_ -> fail")
14411443
public static void fail(String failureMessage) {
14421444
Fail.fail(failureMessage);
14431445
}
@@ -1449,6 +1451,7 @@ public static void fail(String failureMessage) {
14491451
* @param args Arguments referenced by the format specifiers in the format string.
14501452
* @throws AssertionError with the given built message.
14511453
*/
1454+
@Contract("_, _ -> fail")
14521455
public static void fail(String failureMessage, Object... args) {
14531456
Fail.fail(failureMessage, args);
14541457
}
@@ -1459,6 +1462,7 @@ public static void fail(String failureMessage, Object... args) {
14591462
* @param realCause cause of the error.
14601463
* @throws AssertionError with the given message and with the {@link Throwable} that caused the failure.
14611464
*/
1465+
@Contract("_, _ -> fail")
14621466
public static void fail(String failureMessage, Throwable realCause) {
14631467
Fail.fail(failureMessage, realCause);
14641468
}
@@ -1472,8 +1476,8 @@ public static void fail(String failureMessage, Throwable realCause) {
14721476
* @param throwableClass the Throwable class that was expected to be thrown.
14731477
* @throws AssertionError with a message explaining that a {@link Throwable} of given class was expected to be thrown but had
14741478
* not been.
1475-
*
14761479
*/
1480+
@Contract("_ -> fail")
14771481
public static void failBecauseExceptionWasNotThrown(Class<? extends Throwable> throwableClass) {
14781482
Fail.shouldHaveThrown(throwableClass);
14791483
}
@@ -1485,6 +1489,7 @@ public static void failBecauseExceptionWasNotThrown(Class<? extends Throwable> t
14851489
* @throws AssertionError with a message explaining that a {@link Throwable} of given class was expected to be thrown but had
14861490
* not been.
14871491
*/
1492+
@Contract("_ -> fail")
14881493
public static void shouldHaveThrown(Class<? extends Throwable> throwableClass) {
14891494
Fail.shouldHaveThrown(throwableClass);
14901495
}

0 commit comments

Comments
 (0)