|
| 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 | +@ApiStatus.Experimental |
| 46 | +public @interface NotOwning { |
| 47 | +} |
0 commit comments