updated Java/Jython private field examples

This commit is contained in:
Luciano Ramalho
2014-10-19 15:41:11 -02:00
parent 1d48cdbde5
commit 9da839023e
8 changed files with 26 additions and 18 deletions

View File

@@ -3,18 +3,18 @@ import java.lang.reflect.Field;
public class Expose {
public static void main(String[] args) {
Confidential message = new Confidential("text you shoudn't see");
Field privateField = null;
Confidential message = new Confidential("top secret text");
Field secretField = null;
try {
privateField = Confidential.class.getDeclaredField("secret");
secretField = Confidential.class.getDeclaredField("secret");
}
catch (NoSuchFieldException e) {
System.err.println(e);
System.exit(1);
}
privateField.setAccessible(true); // break the lock!
secretField.setAccessible(true); // break the lock!
try {
String wasHidden = (String) privateField.get(message);
String wasHidden = (String) secretField.get(message);
System.out.println("message.secret = " + wasHidden);
}
catch (IllegalAccessException e) {