Skip to content

Commit 72e7f15

Browse files
committed
Refine @Contract Javadoc
This commit refines `@Contract` Javadoc to mention this and new return values. Closes gh-33849
1 parent bbe90a5 commit 72e7f15

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

spring-core/src/main/java/org/springframework/lang/Contract.java

+34-21
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,47 @@
3232
* been introduce in the {@code org.springframework.lang} package to avoid requiring
3333
* an extra dependency, while still following the same semantics.
3434
*
35-
* <p>Method contract has the following syntax:<br/>
36-
* contract ::= (clause ';')* clause<br/>
37-
* clause ::= args '->' effect<br/>
38-
* args ::= ((arg ',')* arg )?<br/>
39-
* arg ::= value-constraint<br/>
40-
* value-constraint ::= 'any' | 'null' | '!null' | 'false' | 'true'<br/>
41-
* effect ::= value-constraint | 'fail'
35+
* <p>Method contract has the following syntax:
36+
* <pre>{@code
37+
* contract ::= (clause ';')* clause
38+
* clause ::= args '->' effect
39+
* args ::= ((arg ',')* arg )?
40+
* arg ::= value-constraint
41+
* value-constraint ::= '_' | 'null' | '!null' | 'false' | 'true'
42+
* effect ::= value-constraint | 'fail' | 'this' | 'new' | 'param<N>'}</pre>
4243
*
43-
* The constraints denote the following:<br/>
44+
* <p>The constraints denote the following:
4445
* <ul>
45-
* <li> <code>_</code> - any value</li>
46-
* <li> <code>null</code> - null value</li>
47-
* <li> <code>!null</code> - a value statically proved to be not-null</li>
48-
* <li> <code>true</code> - true boolean value</li>
49-
* <li> <code>false</code> - false boolean value</li>
50-
* <li> <code>fail</code> - the method throws an exception, if the arguments satisfy argument
51-
* constraints</li>
46+
* <li>{@code _} - any value
47+
* <li>{@code null} - null value
48+
* <li>{@code !null} - a value statically proved to be not-null
49+
* <li>{@code true} - true boolean value
50+
* <li>{@code false} - false boolean value
5251
* </ul>
52+
*
53+
* <p>The additional return values denote the following:
54+
* <ul>
55+
* <li>{@code fail} - the method throws an exception, if the arguments satisfy argument constraints
56+
* <li>{@code new} - the method returns a non-null new object which is distinct from any other object existing in the heap prior to method execution. If method is also pure, then we can be sure that the new object is not stored to any field/array and will be lost if method return value is not used.
57+
* <li>{@code this} - the method returns its qualifier value (not applicable for static methods)
58+
* <li>{@code param1, param2, ...} - the method returns its first (second, ...) parameter value
59+
* </ul>
60+
*
5361
* <p>Examples:
54-
* <code>@Contract("_, null -> null")</code> - method returns null if its second
55-
* argument is null<br/>
56-
* <code>@Contract("_, null -> null; _, !null -> !null")</code> - method returns
57-
* null if its second argument is null and not-null otherwise<br/>
58-
* <code>@Contract("true -> fail")</code> - a typical assertFalse method which
59-
* throws an exception if <code>true</code> is passed to it<br/>
62+
* <ul>
63+
* <li>{@code @Contract("_, null -> null")} - the method returns null if its second argument is null.
64+
* <li>{@code @Contract("_, null -> null; _, !null -> !null")} - the method returns null if its second argument is null and not-null otherwise.
65+
* <li>{@code @Contract("true -> fail")} - a typical {@code assertFalse} method which throws an exception if {@code true} is passed to it.
66+
* <li>{@code @Contract("_ -> this")} - the method always returns its qualifier (e.g. {@link StringBuilder#append(String)}).
67+
* <li>{@code @Contract("null -> fail; _ -> param1")} - the method throws an exception if the first argument is null,
68+
* otherwise it returns the first argument (e.g. {@code Objects.requireNonNull}).
69+
* <li>{@code @Contract("!null, _ -> param1; null, !null -> param2; null, null -> fail")} - the method returns the first non-null argument,
70+
* or throws an exception if both arguments are null (e.g. {@code Objects.requireNonNullElse}).
71+
* </ul>
6072
*
6173
* @author Sebastien Deleuze
6274
* @since 6.2
75+
* @see <a href="https://github.com/JetBrains/java-annotations/blob/master/src/jvmMain/java/org/jetbrains/annotations/Contract.java">org.jetbrains.annotations.Contract</a>
6376
* @see <a href="https://github.com/uber/NullAway/wiki/Configuration#custom-contract-annotations">
6477
* NullAway custom contract annotations</a>
6578
*/

0 commit comments

Comments
 (0)