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;
}
}
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;
}
}