-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a complete example of a resource wrapper type to the manual (#6680)
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import java.io.*; | ||
import org.checkerframework.checker.calledmethods.qual.*; | ||
import org.checkerframework.checker.mustcall.qual.*; | ||
|
||
@InheritableMustCall("dispose") | ||
class InputStreamWrapper { | ||
private final @Owning InputStream stream; | ||
|
||
@MustCallAlias InputStreamWrapper(@MustCallAlias InputStream stream) { | ||
this.stream = stream; | ||
} | ||
|
||
@EnsuresCalledMethods(value = "this.stream", methods = "close") | ||
public void dispose() throws IOException { | ||
this.stream.close(); | ||
} | ||
|
||
/** Shows that either the stream or the wrapper can be closed. */ | ||
static void test(@Owning InputStream stream, boolean b) throws IOException { | ||
InputStreamWrapper wrapper = new InputStreamWrapper(stream); | ||
if (b) { | ||
stream.close(); | ||
} else { | ||
wrapper.dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters