Showing posts with label Set. Show all posts
Showing posts with label Set. Show all posts

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.

Monday, September 24, 2007

Javascript 检测中文字符集的正则表达式

// 校验中文字符
alert(/[\u4E00-\u9FA5]+/.test('中文'));