使用反射给vo赋值

来源:互联网 发布:网络教育英语b统考 编辑:程序博客网 时间:2024/05/22 17:37
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

public class ReflectModel
{
    @SuppressWarnings("unchecked")
    public static void testReflect(Class model) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException
    {
        Object bean = model.newInstance();
        // 获取实体类的所有属性,返回Field数组
        Field[] field = model.getDeclaredFields();
        for (int i = 0; i < field.length; i++)
        {
            // 获取属性的名字
            String name = field[i].getName();
            // 获取属性类型
            String type = field[i].getGenericType().toString();
            Class types = field[i].getType();

            // 关键。。。可访问私有变量
            field[i].setAccessible(true);

            // 将属性的首字母大写
            name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());

            // 获取set方法
            Method method = model.getDeclaredMethod("set" + name, types);
            // 判断当前属性的类型
            if (type.equals("int"))
            {
                // 调用setter方法设置属性值
                method.invoke(bean, 12);
            }
            else if (type.equals("double"))
            {
                // 调用setter方法设置属性值
                method.invoke(bean, 12.00);
            }
            else if (type.equals("short"))
            {
                // 调用setter方法设置属性值
                method.invoke(bean, (short)2);
            }
            else if (type.equals("boolean"))
            {
                // 调用setter方法设置属性值
                method.invoke(bean, true);
            }
            else if (type.equals("class java.lang.String"))
            {
                // 调用setter方法设置属性值
                method.invoke(bean, "kou");
            }
            else if (type.equals("class java.util.Date"))
            {
                method.invoke(bean, new Date());
            }
            else
            {
                method.invoke(bean, null);
            }
        }
    }

    public static void main(String[] args)
    {
        try
        {
            testReflect(VO.class);
        }
        catch (SecurityException e)
        {
            e.printStackTrace();
        }
        catch (IllegalArgumentException e)
        {
            e.printStackTrace();
        }
        catch (NoSuchMethodException e)
        {
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
        catch (InvocationTargetException e)
        {
            e.printStackTrace();
        }
        catch (InstantiationException e)
        {
            e.printStackTrace();
        }
    }

}


import java.util.Date;

public class VO
{
    private int id;
    private String uname;
    private String upass;
    private boolean status;
    public double num;
    public short numShort;
    public Date date;

    public String getUname()
    {
        return uname;
    }

    public void setUname(String uname)
    {
        this.uname = uname;
    }

    public String getUpass()
    {
        return upass;
    }

    public void setUpass(String upass)
    {
        this.upass = upass;
    }

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public boolean isStatus()
    {
        return status;
    }

    public void setStatus(boolean status)
    {
        this.status = status;
    }

    public double getNum()
    {
        return num;
    }

    public void setNum(double num)
    {
        this.num = num;
    }

    public short getNumShort()
    {
        return numShort;
    }

    public void setNumShort(short numShort)
    {
        this.numShort = numShort;
    }

    public Date getDate()
    {
        return date;
    }

    public void setDate(Date date)
    {
        this.date = date;
    }
}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 走路走多了脚痛怎么办 胃疼怎么办简单按摩法 经常胃疼的厉害怎么办 半夜胃疼的厉害怎么办 吃消炎药伤胃了怎么办 吃药伤胃了胃疼怎么办 宝宝吃药伤胃了怎么办 胃胀胃痛怎么办快速解决方法 半夜2点3点胃疼怎么办 晚上吃多了胃疼怎么办 骨折打石膏后痒怎么办 脚脖子崴了肿了怎么办 喝酒喝的吐血了怎么办 感冒后咳嗽有痰怎么办 嗓子里老是有痰怎么办 物业把水停了怎么办 机洗衬衫缩水了怎么办 羊绒大衣洗缩水了怎么办 棉质衣服缩水了怎么办 衣服洗了变小了怎么办 毛衣洗后缩水了怎么办 鼻子又大又塌怎么办 苹果6被停用了怎么办 苹果6s手机停用怎么办 苹果4手机已停用怎么办 苹果手机5停用了怎么办 老公被骗300多万怎么办 苹果手机被抹除怎么办 钓鱼邮件点开了怎么办 幼犬吃多了拉稀怎么办 幼犬半夜醒了叫怎么办 相爱相杀的感情怎么办 冬天玩电脑手冷怎么办 被陌生人骗了钱怎么办 被网上骗了钱怎么办 20岁欠了10万怎么办 我赌博欠了10万怎么办 孩子见到生人不爱说话怎么办 18岁了个子矮小怎么办 1岁宝宝个子矮70怎么办 喋血街头2进监狱怎么办