java反射实体对象

2017-09-13  本文已影响0人  沉默的鱼fish

原文:http://blog.csdn.net/qq_26525215
自己利用类反射来实现BeanUtils的功能。需要先学习类反射!

准备一个测试实体对象User类

public class User {
private String uuid;
private String name;
private int age;
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (uuid == null) {
if (other.uuid != null)
return false;
} else if (!uuid.equals(other.uuid))
return false;
return true;
}
@Override
public String toString() {
return "User [uuid=" + uuid + ", name=" + name + ", age=" + age + "]";
}

接着,需要定义实体反射的方法

  public static Object populate(Class cls, Map map)
        throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {

    Object obj = null;
    //1、用类反射new出对象
    obj = cls.newInstance();
    //2 再用类反射对新new的对象设置属性值(必须遵守Java设置规范)--即通过setter方法设置
    //2.1遍历出所有该类声明的属性
    Field flds[] = cls.getDeclaredFields();//getDeclaredFields()返回Class中所有的字段,包括私有字段;

    for (Field fld: flds) {
        String fldName = fld.getName();
        //根据属性名,到map中去读取数据,只有数据非空才需要给该属性设置值
        Object value = map.get(fldName);
        if(value==null){//如果map中不存在对应的属性数据,我们在这里给出提示信息
            System.out.println(fldName+"的数据为空");
        }else{
            //如果map中存在对应的属性数据,则由属性名得出它的setter方法的名字
            String mothodName = "set"+fldName.substring(0, 1).toUpperCase()+fldName.substring(1);

            //根据方法名和参数的数据类型(其实就是属性的类型),获得Method对象
            Class paramTypes[] = new Class[1];
            paramTypes[0] = fld.getType();
            Method method = cls.getDeclaredMethod(mothodName, paramTypes);

            //调用该method对象所代表的方法
            Object args[] = new Object[1];
            args[0]=value;
            method.invoke(obj, args);
        }
    }
    return obj;
}

单元测试

@Test
public void test()
{
Map map = new HashMap();
map.put("uuid","001");
map.put("name","你好");
map.put("age",20);
try {
User user = (User)MyBeanUtils.populate(User.class,map);
System.out.println(user.toString());
} catch (Exception e) {
e.printStackTrace();
}
}

会发现,每次调用时都需要进行实体转换,接下来进行改进。

public static <T> T populate(Class<T> cls, Map map)
throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {

    T obj = null;
    //1、用类反射new出对象
    obj = cls.newInstance();
    //2 再用类反射对新new的对象设置属性值(必须遵守Java设置规范)--即通过setter方法设置
    //2.1遍历出所有该类声明的属性
    Field flds[] = cls.getDeclaredFields();//getDeclaredFields()返回Class中所有的字段,包括私有字段;

    for (Field fld : flds) {
        String fldName = fld.getName();
        //根据属性名,到map中去读取数据,只有数据非空才需要给该属性设置值
        Object value = map.get(fldName);
        if (value == null) {//如果map中不存在对应的属性数据,我们在这里给出提示信息
            System.out.println(fldName + "的数据为空");
        } else {
            //如果map中存在对应的属性数据,则由属性名得出它的setter方法的名字
            String mothodName = "set" + fldName.substring(0, 1).toUpperCase() + fldName.substring(1);

            //根据方法名和参数的数据类型(其实就是属性的类型),获得Method对象
            Class<?> paramTypes[] = new Class[1];
            paramTypes[0] = fld.getType();
            Method method = cls.getDeclaredMethod(mothodName, paramTypes);

            //调用该method对象所代表的方法
            Object args[] = new Object[1];
            args[0] = value;
            method.invoke(obj, args);
        }
    }
    return obj;
}

单元测试

@Test
public void test()
{
Map map = new HashMap();
map.put("uuid","001");
map.put("name","你好");
map.put("age",20);
try {
User user = MyBeanUtils.populate(User.class,map);
System.out.println(user.toString());
} catch (Exception e) {
e.printStackTrace();
}
}

上一篇 下一篇

猜你喜欢

热点阅读