java反射代码

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetExceptio n;
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, InvocationTargetExceptio n, 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 (InvocationTargetExceptio n 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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值