Showing posts with label access. Show all posts
Showing posts with label access. Show all posts

Monday, July 28, 2008

PropertyAccess/ClassPropertyAdapter/PropertyAdapter of Tapestry5

看了三个接口说明,简要记下三者的关系:
1、PropertyAccess中的get/set方法是用于读取/设置某对象instance中的某属性property,比较明了,另外还有个方法getAdapter是用于获取此对象class或者实例instance的ClassPropertyAdapter对象。
2、而在得到ClassPropertyAdapter后,也可以与PropertyAccess中的get/set方法一样操作一个instance中的property,另外有个方法getPropertyAdapter则根据参数propertyName获取PropertyAdapter对象。
3、在得到PropertyAdapter对象之后,也同前二个接口一样可以传入instance实例,对此实例的propertyName进行get/set操作。其中有个方法getClassAdapter则是反过来用于获取ClassPropertyAdapter对象,以操作此对象的其他属性。

Thursday, July 17, 2008

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.