public
Description: JSR308 Typestate Checker
Homepage: http://www.warski.org/typestate.html
Clone URL: git://github.com/adamw/jsr308-typestate-checker.git
100644 29 lines (24 sloc) 0.612 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package checkers.typestate.ioexample;
 
import java.io.InputStream;
import java.io.IOException;
 
import static checkers.typestate.ioexample.InputStreamStates.*;
 
/**
* Cannot read from an input stream after it's closed.
* @author Adam Warski (adam at warski dot org)
*/
public class Example1 {
public int read(@Open InputStream stream) throws IOException {
int ret = 0;
try {
int input;
while ((input = stream.read()) != -1) {
ret += input;
}
} finally {
stream.close();
}
 
// This results in an error: the stream is in the "closed" state.
ret += stream.read();
 
return ret;
}
}