java BeansUtil 反射原理实现(一)

2014-11-24 08:39:13 · 作者: · 浏览: 0
最近项目好难做啊!后台人员的活几乎都在前台,用 了一个前台框架kendo ui 里面对于js 的使用确实让我学到了不少的思想,对于js这方面的领悟更高了一层,但是对于json的操作实在是不喜欢,对于数据验证和安全这里特别不好做,还好用了spring作为后台框架,数据验证这一块视乎是简单不少,但是代码量也不少,所以自己应用反射技术写了一个进行bean属性的拷贝的功能,主要是因为要对json里面的内容存储 数据库,又不想使用get 和set方法进行一个个的赋值
class User {  
    private int id;  
  
    private String name;  
    private boolean demo = false;  
  
    public boolean isDemo() {  
        return demo;  
    }  
  
    public void setDemo(boolean demo) {  
        this.demo = demo;  
    }  
  
    public int getId() {  
        return id;  
    }  
  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
}  

public class BeanUtils {  
    public static void main(String args[])  
    {  
        User user = new User();  
        User user1 = new User();  
        user1.setDemo(false);  
        user.setName("12");  
        user.setDemo(true);  
        user.setId(1);  
        user1.setName("21");  
        user1.setId(2);  
        BeanUtils beanUtils = new BeanUtils();  
        Set stringSet = new HashSet();  
        stringSet.add("id");  //设置不进行跟新的属性名称  
        System.out.println(user.getId());  
        System.out.println(user.getName());  
        System.out.println(user.isDemo());  
        try {  
            beanUtils.copyProperties(user1,user,stringSet);  
            System.out.println(user.getId());  
            System.out.println(user.getName());  
            System.out.println(user.isDemo());  
        } catch (InvocationTargetException e) {  
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.  
        } catch (IllegalAccessException e) {  
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.  
        }  
    }  
    /** 
     * 
     * t 源类 
     * k 需要进行属性更新的类 
     * */  
    public void  copyProperties(T t,K k,Set
stringSet) throws InvocationTargetException, IllegalAccessException { Class clazz = t.getClass(); Class target = k.getClass(); Field[] fields = clazz.getDeclaredFields(); //获取源类的所用字段 if (stringSet==null) { stringSet = new HashSet(); } for (Field field : fields) //遍历字段 { String fieldName = field.getName(); //获取名称 if (stringSet.contains(fieldName)) //通过这个动态的指定需要更新的字段 continue; Field targetField = null; try { targetField = target.getDeclaredField(fieldName); //根据源类的属性获取目标类的属性 } catch (NoSuchFieldException e) { continue; } if (field.getType()==targetField.getType()) { String getMethodName = null; String setMethodName = null; if (field.getType().getName()=="boolean") { getMethodName = "is"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1); setMethodName = "set" + fieldName.substring(0,1).toUpperCase()+fieldName.substring(1); } else { getMethodName = "get"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1); setMethodName = "set" + fieldName.substring(0,1).toUpperCase()+fieldName.substring(1); } Method getMethod = null; //源类的get方法 Method setMethod = null; //目标类的方法 try { getMethod = clazz.getDeclaredMethod(getMethodName); setMethod = target.getDeclaredMethod(setMethodName,field.getType()); Object value = getMethod.invoke(t); //通过get方法获得值 setMethod.invoke(k,value); } catch (NoSuchMethodException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } } } //针对于list属性 public List copyListProperties(List list,Class clazz,List newList,Set stringSet) { Method[] meth