forked from fluentpython/example-code-2e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpose.java
More file actions
25 lines (23 loc) · 791 Bytes
/
Expose.java
File metadata and controls
25 lines (23 loc) · 791 Bytes
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
import java.lang.reflect.Field;
public class Expose {
public static void main(String[] args) {
Confidential message = new Confidential("top secret text");
Field secretField = null;
try {
secretField = Confidential.class.getDeclaredField("secret");
}
catch (NoSuchFieldException e) {
System.err.println(e);
System.exit(1);
}
secretField.setAccessible(true); // break the lock!
try {
String wasHidden = (String) secretField.get(message);
System.out.println("message.secret = " + wasHidden);
}
catch (IllegalAccessException e) {
// this will not happen after setAccessible(true)
System.err.println(e);
}
}
}