标签:exce tcl string 暴力 declare case ble get str
private void ReflectionClass() throws Exception{
User user = new User();
//得到类
Class clazz = user.getClass();
Class clazz1 = Class.forName("java.lang.String");
//创建类的实例
Object object = clazz1.getConstructor().newInstance();
//获取构造器
Constructor[] constructor = null;
//getConstructors返回公共构造函数
constructor = clazz.getConstructors();
//getDeclaredConstructors返回所有构造函数包括私有
Constructor[] constructor1= clazz.getDeclaredConstructors();
//获取对象公共属性
Field[] fields = clazz.getFields();
//获取指定公共属性
Field field = clazz.getField("name");
//获取所有属性包括私有
Field[] decl_fields = clazz.getDeclaredFields();
//获取指定属性
Field decl_field = clazz.getDeclaredField("name");
//属性名
String fieldName = decl_field.getName();
decl_field.setAccessible(true);//设置属性可见 暴力破解
String value = String.valueOf(decl_field.get(user)==null?"":decl_field.get(user));//获取属性值
//getMethods获取公共方法 getDeclaredMethods后去所有方法
Method[] methods = clazz.getMethods();
//获取指定公共方法
Method method = clazz.getMethod("set"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1,fieldName.length()),String.class);
method.invoke(user,"张三");//设置值
}
标签:exce tcl string 暴力 declare case ble get str
原文地址:https://www.cnblogs.com/mxsyfyj/p/12567527.html