Skip to content

Commit 7d48458

Browse files
committed
Add @Owning and @NotOwning
1 parent 2a28eab commit 7d48458

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515
#
1616

17-
projectVersion=26.0.2
17+
projectVersion=27.0.0
1818
kotlin.code.style=official
1919
kotlin.mpp.stability.nowarn=true
2020
kotlin.stdlib.default.dependency=false
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.jetbrains.annotations;
2+
3+
import java.io.Closeable;
4+
import java.io.InputStream;
5+
import java.io.StringWriter;
6+
import java.lang.annotation.Documented;
7+
import java.lang.annotation.ElementType;
8+
import java.lang.annotation.Retention;
9+
import java.lang.annotation.RetentionPolicy;
10+
import java.lang.annotation.Target;
11+
import java.util.Properties;
12+
13+
/**
14+
* Indicates that responsibility for closing a {@link Closeable} resource is <em>not</em> transferred.
15+
*
16+
* <p>
17+
* When marked on a method, the return value of the method is still owned and managed by the callee and therefore the
18+
* caller must <em>not</em> close it. For example, a class may encapsulate an {@code OutputStream}, and expose it via a
19+
* getter. The getter does not transfer responsibility for closing the stream.
20+
*
21+
* <p>
22+
* When marked on a parameter, the callee does not take responsibility for closing the {@code Closeable}, and
23+
* responsibility for closing it stays with the caller. For example, {@link Properties#load(InputStream)} doesn't close
24+
* its parameter, and relying on that method to close the {@code InputStream} would be a mistake.
25+
*
26+
* <p>
27+
* When marked on a class or interface implementing {@code Closeable}, indicates that this class and its subclasses do
28+
* <em>not</em> contain resources which must be closed, and therefore not closing them won't lead to any resource leak.
29+
* For example, {@link StringWriter} does not hold any external resources despite implementing {@code Closeable}, and
30+
* closing it has no effect. Note that this does <em>not</em> mean that the class <em>cannot</em> be closed, only that
31+
* it does not <em>have</em> to be closed.
32+
*
33+
* <p>
34+
* This annotation also could be used as a meta-annotation, to define other annotations for convenience.
35+
*
36+
* <p>
37+
* This annotation may be used by tools to check when a closeable resource hasn't been closed or closed by the wrong
38+
* party. Note that this annotation has no effect when used for types that don't implement {@link Closeable}.
39+
*
40+
* @see Owning
41+
*/
42+
@Documented
43+
@Retention(RetentionPolicy.CLASS)
44+
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE})
45+
public @interface NotOwning {
46+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.jetbrains.annotations;
2+
3+
import java.io.BufferedWriter;
4+
import java.io.Closeable;
5+
import java.io.Writer;
6+
import java.lang.annotation.Documented;
7+
import java.lang.annotation.ElementType;
8+
import java.lang.annotation.Retention;
9+
import java.lang.annotation.RetentionPolicy;
10+
import java.lang.annotation.Target;
11+
import java.nio.file.Files;
12+
13+
/**
14+
* Indicates a transfer of responsibility for closing a {@link Closeable} resource.
15+
*
16+
* <p>
17+
* When marked on a method, the responsibility for closing the return value of this method is transferred to the caller.
18+
* For example, the {@link Files#newInputStream} method constructs an {@code InputStream} and returns it, then the
19+
* caller is responsible for closing this {@code InputStream}.
20+
*
21+
* <p>
22+
* When marked on a parameter, the responsibility for closing is transferred from the caller to the callee. For example,
23+
* the following method's parameter may be annotated with {@code @Owning}:
24+
* <pre>
25+
* {@code
26+
* public static void closeQuietly(@Owning Closeable resource) {
27+
* try {
28+
* resource.close();
29+
* } catch (IOException ignored) {
30+
* }
31+
* }
32+
* }
33+
* </pre>
34+
* Another example is {@link BufferedWriter#BufferedWriter(Writer)} which wraps a {@code Writer}, as the responsibility
35+
* for closing the wrapped writer is transferred away from the caller (to the {@code BufferedWriter}).
36+
*
37+
* <p>
38+
* When marked on a class or interface implementing {@code Closeable}, indicates that this class and its subclasses
39+
* contain resources which must be closed, and therefore {@link Closeable#close close()} <em>must</em> be closed at some
40+
* point. This is the default assumption for most closeable classes; this annotation may be used to override a
41+
* {@link NotOwning} annotation in a superclass.
42+
*
43+
* <p>
44+
* This annotation also could be used as a meta-annotation, to define other annotations for convenience.
45+
*
46+
* <p>
47+
* This annotation may be used by tools to check when a closeable resource hasn't been closed or closed by the wrong
48+
* party. Note that this annotation has no effect when used for types that don't implement {@link Closeable}.
49+
*
50+
* @see NotOwning
51+
*/
52+
@Documented
53+
@Retention(RetentionPolicy.CLASS)
54+
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE})
55+
public @interface Owning {
56+
}

0 commit comments

Comments
 (0)