• 转 利用java反射实现两个具有相同属性bean赋值


    
        package com.dobn.bdgcgl.utils;  
          
          
        import java.lang.reflect.Field;  
        import java.lang.reflect.Method;  
          
          
          
          
        public class ClassReflection {  
              
            /** 
             * @param class1 用于赋值的实体类  
             * @param class1 需要待赋值的实体类 
             * @author ym 
             * @CreateTime 2012-11-22下午03:23:23 
             * 描述:反射实体类赋值 
             */  
            public static void reflectionAttr(Object class1,Object class2) throws Exception{  
                Class clazz1 = Class.forName(class1.getClass().getName());  
                Class clazz2 = Class.forName(class2.getClass().getName());  
        //      获取两个实体类的所有属性  
                Field[] fields1 = clazz1.getDeclaredFields();  
                Field[] fields2 = clazz2.getDeclaredFields();  
                ClassReflection cr = new ClassReflection();  
        //      遍历class1Bean,获取逐个属性值,然后遍历class2Bean查找是否有相同的属性,如有相同则赋值  
                for (Field f1 : fields1) {  
                    if(f1.getName().equals("id"))  
                        continue;  
                    Object value = cr.invokeGetMethod(class1 ,f1.getName(),null);  
                    for (Field f2 : fields2) {  
                        if(f1.getName().equals(f2.getName())){  
                            Object[] obj = new Object[1];  
                            obj[0] = value;  
                            cr.invokeSetMethod(class2, f2.getName(), obj);  
                        }  
                    }  
                }  
                  
            }  
              
            /** 
             *  
             * 执行某个Field的getField方法 
             *  
             * @param clazz 类 
             * @param fieldName 类的属性名称 
             * @param args 参数,默认为null 
             * @return  
             */  
            private Object invokeGetMethod(Object clazz, String fieldName, Object[] args)  
            {  
                String methodName = fieldName.substring(0, 1).toUpperCase()+ fieldName.substring(1);  
                Method method = null;  
                try   
                {  
                    method = Class.forName(clazz.getClass().getName()).getDeclaredMethod("get" + methodName);  
                    return method.invoke(clazz);  
                }   
                catch (Exception e)  
                {  
                    e.printStackTrace();  
                    return "";  
                }  
            }  
              
            /** 
             *  
             * 执行某个Field的setField方法 
             *  
             * @param clazz 类 
             * @param fieldName 类的属性名称 
             * @param args 参数,默认为null 
             * @return  
             */  
            private Object invokeSetMethod(Object clazz, String fieldName, Object[] args)  
            {         
                String methodName = fieldName.substring(0, 1).toUpperCase()+ fieldName.substring(1);  
                Method method = null;  
                try   
                {  
                    Class[] parameterTypes = new Class[1];  
                    Class c = Class.forName(clazz.getClass().getName());  
                    Field field = c.getDeclaredField(fieldName);   
                    parameterTypes[0] = field.getType();  
                    method = c.getDeclaredMethod("set" + methodName,parameterTypes);  
                    return method.invoke(clazz,args);  
                }   
                catch (Exception e)  
                {  
                    e.printStackTrace();  
                    return "";  
                }  
            }  
          
          
        }  
  • 相关阅读:
    [CSAPP笔记][第九章虚拟存储器][吐血1500行]
    [CSAPP笔记][第六章存储器层次结构]
    [CSAPP笔记][第八章异常控制流][呕心沥血千行笔记]
    好吧,刚把CSDN搬家到博客园。。记录一发
    [CSAPP笔记][第二章信息的表示和处理]
    综合练习:词频统计
    组合数据类型综合练习:英文词频统计
    熟悉常用的Linux操作
    1.大数据概述
    语义分析
  • 原文地址:https://www.cnblogs.com/mytzq/p/7205921.html
Copyright © 2020-2023  润新知