how to protect private field from reflect get and set in java
public class ReflectField {
protected String test = "test string in class ReflectField!";
}
/**
* subclass of ReflectField
*/
import java.lang.reflect.Field;
public class ReflectExtendsField extends ReflectField {
// protected String test = "test string in class ReflectExtendsField!";
private String test = "test string in class ReflectExtendsField!";
public static void main(String[] args) throws Exception {
// Class ref = Class.forName("ReflectExtendsField");
Class ref = ReflectExtendsField.class;
Field[] fields = ref.getFields();
System.out.println(fields.length);
for (Field field : fields) {
field.setAccessible(true);
System.out.println(field.get(ref));
}
Field testField = ref.getDeclaredField("test");
testField.setAccessible(true);
System.out.println(testField);
System.out.println(testField.get(ref));
}
}
It is just a hack, and usally protect private field from access using security manage.
No comments :
Post a Comment