Java反射机制

package com.utils;

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;

public class ReflectUtil {

    public static <T> T initlize(Class<T> clazz, HttpServletRequest request)
            throws Exception {
        T t = clazz.newInstance();
        Field[] fields = clazz.getDeclaredFields();// 变量的对象
        for (Field field : fields) {
            String filedName = field.getName();// 变量对象的名字
            Class<?> fieldType = field.getType();// 变量的数据类型
            Enumeration<String> enumeration = request.getParameterNames();// 获取表单文本的的对象
            while (enumeration.hasMoreElements()) {
                String pName = enumeration.nextElement();// 获取表单文本对象的名称
                if (filedName.equals(pName)) {
                    String preflexStr = filedName.substring(0, 1);
                    String sufflexStr = filedName.substring(1);
                    String writeMethodStr = "set" + preflexStr.toUpperCase()
                            + sufflexStr;// 方法名
                    Method writeMethod = clazz.getDeclaredMethod(
                            writeMethodStr, new Class<?>[] { fieldType });// 造方法
                    String pValue = request.getParameter(pName);
                    if (fieldType.toString().equals("int")) {
                        writeMethod.invoke(t, Integer.valueOf(pValue)
                                .intValue());
                        continue;
                    } else if (fieldType.newInstance() instanceof Date) {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                                "yyyy-MM-dd");
                        Date date = simpleDateFormat.parse(pValue);
                        System.out.println("********" + pValue);
                        System.out.println("+++++++++" + date);
                        writeMethod.invoke(t, date);
                        continue;
                    } else if (fieldType.newInstance() instanceof Integer) {
                        writeMethod.invoke(t, Integer.valueOf(pValue)
                                .intValue());
                        continue;
                    }
                    writeMethod.invoke(t, pValue);

                }

            }
        }
        return t;
    }

    public static <T> T initlize01(Class<T> clazz, HttpServletRequest request)
            throws Exception {
        T t = clazz.newInstance();
        PropertyDescriptor[] propertyDscriptors = Introspector.getBeanInfo(
                clazz).getPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : propertyDscriptors) {
            String fileName = propertyDescriptor.getName();
            Method writeMothod = propertyDescriptor.getWriteMethod();
            if (writeMothod != null) {
                Enumeration<String> enumeration = request.getParameterNames();
                while (enumeration.hasMoreElements()) {
                    String pName = enumeration.nextElement();
                    if (fileName.equals(pName)) {
                        String pValue = request.getParameter(pName);
                        writeMothod.invoke(t, pValue);
                    }

                }
            }
        }
        return t;
    }

}

 

转载于:https://www.cnblogs.com/lhconglhc/p/3698635.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值