-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathHello.java
More file actions
64 lines (56 loc) · 1.47 KB
/
Hello.java
File metadata and controls
64 lines (56 loc) · 1.47 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package hello;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
class Hello {
void doesNotCauseNPE() {
Pointers.A a = Pointers.mayReturnNull(10);
a.method();
}
void mayCauseNPE() {
Random rng = new Random();
Pointers.A a = Pointers.mayReturnNull(rng.nextInt());
// FIXME: should check for null before calling method()
a.method();
}
void mayLeakResource() throws IOException {
OutputStream stream = Resources.allocateResource();
if (stream == null) {
return;
}
try {
stream.write(12);
} finally {
// FIXME: should close the stream
}
}
/**
* This method should be rewritten with nested try { ... } finally { ... } statements, or the
* possible exception raised by fis.close() should be swallowed.
*/
void twoResources() throws IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File("whatever.txt"));
fos = new FileOutputStream(new File("everwhat.txt"));
fos.write(fis.read());
} finally {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
}
}
}