Skip to content

Commit 1e12a4f

Browse files
committed
Add CheckReturnValue annotation
1 parent ff945b1 commit 1e12a4f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.jetbrains.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Target;
5+
6+
/**
7+
* Specifies that the method is impure and that its return value must be used.
8+
* <p>
9+
* For pure methods (annotated with {@code @Contract(pure = true)}),
10+
* it's implied that the resulting value is important,
11+
* so this annotation would be redundant.
12+
* <p>
13+
* Some impure methods have side effects and still require the return value to be used.
14+
* For example, {@link java.io.InputStream#read(byte[])}
15+
* returns the number of bytes actually stored in the byte array.
16+
* Without checking the return value, it's impossible to say how many bytes were actually read.
17+
* <p>
18+
* This annotation should not be used if the return value of the method
19+
* provides only <i>additional</i> information.
20+
* For example, the main purpose of {@link java.util.Collection#add(Object)}
21+
* is to modify the collection, and the return value is only interesting
22+
* when adding an element to a set, to see if the set already contained that element before.
23+
* <p>
24+
* When used on a type, the annotation applies to all methods that do not return {@code void}.
25+
* <p>
26+
* When used on a package, the annotation applies to all types of that package.
27+
*
28+
* @see Contract#pure()
29+
*/
30+
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE, ElementType.PACKAGE})
31+
@ApiStatus.Experimental
32+
public @interface CheckReturnValue {
33+
}

0 commit comments

Comments
 (0)