updated Java/Jython private field examples
This commit is contained in:
parent
1d48cdbde5
commit
9da839023e
3
.gitignore
vendored
3
.gitignore
vendored
@ -5,6 +5,9 @@ __pycache__/
|
|||||||
# C extensions
|
# C extensions
|
||||||
*.so
|
*.so
|
||||||
|
|
||||||
|
# Java
|
||||||
|
*.class
|
||||||
|
|
||||||
# Distribution / packaging
|
# Distribution / packaging
|
||||||
.Python
|
.Python
|
||||||
env/
|
env/
|
||||||
|
Binary file not shown.
@ -4,6 +4,6 @@ public class Confidential {
|
|||||||
private String hidden = "burn after reading";
|
private String hidden = "burn after reading";
|
||||||
|
|
||||||
public Confidential(String text) {
|
public Confidential(String text) {
|
||||||
this.secret = text;
|
this.secret = text.toUpperCase();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
@ -3,18 +3,18 @@ import java.lang.reflect.Field;
|
|||||||
public class Expose {
|
public class Expose {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Confidential message = new Confidential("text you shoudn't see");
|
Confidential message = new Confidential("top secret text");
|
||||||
Field privateField = null;
|
Field secretField = null;
|
||||||
try {
|
try {
|
||||||
privateField = Confidential.class.getDeclaredField("secret");
|
secretField = Confidential.class.getDeclaredField("secret");
|
||||||
}
|
}
|
||||||
catch (NoSuchFieldException e) {
|
catch (NoSuchFieldException e) {
|
||||||
System.err.println(e);
|
System.err.println(e);
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
privateField.setAccessible(true); // break the lock!
|
secretField.setAccessible(true); // break the lock!
|
||||||
try {
|
try {
|
||||||
String wasHidden = (String) privateField.get(message);
|
String wasHidden = (String) secretField.get(message);
|
||||||
System.out.println("message.secret = " + wasHidden);
|
System.out.println("message.secret = " + wasHidden);
|
||||||
}
|
}
|
||||||
catch (IllegalAccessException e) {
|
catch (IllegalAccessException e) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import Confidential
|
import Confidential
|
||||||
|
|
||||||
message = Confidential("text you shoudn't see")
|
message = Confidential('top secret text')
|
||||||
private_field = Confidential.getDeclaredField('secret')
|
secret_field = Confidential.getDeclaredField('secret')
|
||||||
private_field.setAccessible(True) # break the lock!
|
secret_field.setAccessible(True) # break the lock!
|
||||||
print 'message.secret =', private_field.get(message)
|
print 'message.secret =', secret_field.get(message)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from java.lang.reflect import Modifier
|
from java.lang.reflect import Modifier
|
||||||
import Confidential
|
import Confidential
|
||||||
|
|
||||||
message = Confidential('never expose this')
|
message = Confidential('top secret text')
|
||||||
fields = Confidential.getDeclaredFields()
|
fields = Confidential.getDeclaredFields()
|
||||||
for field in fields:
|
for field in fields:
|
||||||
# list private fields only
|
# list private fields only
|
||||||
|
@ -1,12 +1,17 @@
|
|||||||
# In the Jython registry:
|
|
||||||
# python.security.respectJavaAccessibility = false
|
"""
|
||||||
# Setting this to false will allow Jython to provide access to
|
In the Jython registry file there is this line:
|
||||||
# non-public fields, methods, and constructors of Java objects.
|
|
||||||
|
python.security.respectJavaAccessibility = true
|
||||||
|
|
||||||
|
Set this to false and Jython provides access to non-public
|
||||||
|
fields, methods, and constructors of Java objects.
|
||||||
|
"""
|
||||||
|
|
||||||
import Confidential
|
import Confidential
|
||||||
|
|
||||||
message = Confidential("text you shoudn't see")
|
message = Confidential('top secret text')
|
||||||
for name in dir(message):
|
for name in dir(message):
|
||||||
attr = getattr(message, name)
|
attr = getattr(message, name)
|
||||||
if not callable(attr): # ignore methods
|
if not callable(attr): # non-methods only
|
||||||
print name, '=', attr
|
print name + '\t=', attr
|
||||||
|
Loading…
Reference in New Issue
Block a user