初识java反射机制

    反射机制主要是通过Class这个对象,通过class对象我们能得到相应的实例化对象Class.forName(className),
    通过Method获取class对象相应是方法,通过Field获取属性。如下代码:


public ReflexUtils(Object obj, String property, Object val) {
    this.stringUtils = StringUtils.getInstance();//获取搜字母转化对象
    this.obj = obj;//初始化传入对象
    this.property = property;//对象属性
    this.val = val;//对象数据的值
    try {
        handleString();//设置对象属性
        setVal();//设置对象的值
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void handleString() throws Exception {
    String[] propertys = property.split("\\.");//可能没多级属性
    this.sourceObj = this.obj;
    for (int i = 1; i < propertys.length; i++) {
        if (i < propertys.length - 1) {
            //通过反射获取方法
            Method method = this.sourceObj.getClass().getMethod(
                    "get" + stringUtils.getChangeName(propertys[i]));
            //通过反射获取属性
            this.field = this.sourceObj.getClass().getDeclaredField(
                    propertys[i]);
            //通过反射方法实例化反射对象
            sourceObj = method.invoke(this.sourceObj);
        } else {
            this.field = this.sourceObj.getClass().getDeclaredField(
                    propertys[i]);
        }
    }
}

public void setVal() throws Exception {
    String sname = this.field.getType().getSimpleName();//获取数据的名称
    this.field.setAccessible(true);//将私有的数据变成可操作对象
    if (sname.contains("[")) {
        String[] val = (String[]) this.val;
        if (sname.equalsIgnoreCase("String[]")) {
            this.field.set(this.sourceObj, val);//通过反射设置属性的值
        } else if (sname.equalsIgnoreCase("int[]")
                || sname.equalsIgnoreCase("Integer[]")) {
            int[] date = new int[val.length];
            for (int i = 0; i < val.length; i++) {
                date[i] = Integer.parseInt(val[i]);
            }
            this.field.set(this.sourceObj, date);
        } else if (sname.equalsIgnoreCase("double[]")
                || sname.equalsIgnoreCase("Double[]")) {
            double[] date = new double[val.length];
            for (int i = 0; i < val.length; i++) {
                date[i] = Double.parseDouble(val[i]);
            }
            this.field.set(this.sourceObj, date);
        }
    } else {
        String val = this.val.toString();
        if (sname.equalsIgnoreCase("date")
                && val.matches("\\d{4}-\\d{2}-\\d{2}")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            this.field.set(this.sourceObj, sdf.parse(val));
        } else if (sname.equalsIgnoreCase("int")
                || sname.equalsIgnoreCase("Integer")) {
            if (val.matches("\\d+")) {
                this.field.set(this.sourceObj, Integer.parseInt(val));
            }
        } else if (sname.equalsIgnoreCase("double")
                || sname.equalsIgnoreCase("Double")) {
            if (val.matches("\\d+(\\.+\\d+)?")) {
                this.field.set(this.sourceObj, Double.parseDouble(val));
            }
        } else {
            this.field.set(this.sourceObj, this.val);
        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值