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

3
.gitignore vendored
View File

@ -5,6 +5,9 @@ __pycache__/
# C extensions
*.so
# Java
*.class
# Distribution / packaging
.Python
env/

Binary file not shown.

View File

@ -4,6 +4,6 @@ public class Confidential {
private String hidden = "burn after reading";
public Confidential(String text) {
this.secret = text;
this.secret = text.toUpperCase();
}
}

Binary file not shown.

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) {

View File

@ -1,6 +1,6 @@
import Confidential
message = Confidential("text you shoudn't see")
private_field = Confidential.getDeclaredField('secret')
private_field.setAccessible(True) # break the lock!
print 'message.secret =', private_field.get(message)
message = Confidential('top secret text')
secret_field = Confidential.getDeclaredField('secret')
secret_field.setAccessible(True) # break the lock!
print 'message.secret =', secret_field.get(message)

View File

@ -1,7 +1,7 @@
from java.lang.reflect import Modifier
import Confidential
message = Confidential('never expose this')
message = Confidential('top secret text')
fields = Confidential.getDeclaredFields()
for field in fields:
# list private fields only

View File

@ -1,12 +1,17 @@
# In the Jython registry:
# python.security.respectJavaAccessibility = false
# Setting this to false will allow Jython to provide access to
# non-public fields, methods, and constructors of Java objects.
"""
In the Jython registry file there is this line:
python.security.respectJavaAccessibility = true
Set this to false and Jython provides access to non-public
fields, methods, and constructors of Java objects.
"""
import Confidential
message = Confidential("text you shoudn't see")
message = Confidential('top secret text')
for name in dir(message):
attr = getattr(message, name)
if not callable(attr): # ignore methods
print name, '=', attr
if not callable(attr): # non-methods only
print name + '\t=', attr