java中遍历类中的属性、调用getter&setter方法

时间:2022-02-28 21:46:10

public static void testReflect(Object model) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
        Field[] field = model.getClass().getDeclaredFields();        //获取实体类的所有属性,返回Field数组  
        for(int j=0 ; j<field.length ; j++){     //遍历所有属性
                String name = field[j].getName();    //获取属性的名字
                
                System.out.println("attribute name:"+name);     
                name = name.substring(0,1).toUpperCase()+name.substring(1); //将属性的首字符大写,方便构造get,set方法
                String type = field[j].getGenericType().toString();    //获取属性的类型
                if(type.equals("class java.lang.String")){   //如果type是类类型,则前面包含"class ",后面跟类名
                    Method m = model.getClass().getMethod("get"+name);
                    String value = (String) m.invoke(model);    //调用getter方法获取属性值
                    if(value != null){

System.out.println("attribute value:"+value);
                    }
                }
                if(type.equals("class java.lang.Integer")){     
                    Method m = model.getClass().getMethod("get"+name);
                    Integer value = (Integer) m.invoke(model);
                    if(value != null){
                        System.out.println("attribute value:"+value);
                    }
                }
                if(type.equals("class java.lang.Short")){     
                    Method m = model.getClass().getMethod("get"+name);
                    Short value = (Short) m.invoke(model);
                    if(value != null){
                        System.out.println("attribute value:"+value);                    }
                }       
                if(type.equals("class java.lang.Double")){     
                    Method m = model.getClass().getMethod("get"+name);
                    Double value = (Double) m.invoke(model);
                    if(value != null){                    
                        System.out.println("attribute value:"+value);  
                    }
                }                  
                if(type.equals("class java.lang.Boolean")){
                    Method m = model.getClass().getMethod("get"+name);    
                    Boolean value = (Boolean) m.invoke(model);
                    if(value != null){                      
                        System.out.println("attribute value:"+value);
                    }
                }
                if(type.equals("class java.util.Date")){
                    Method m = model.getClass().getMethod("get"+name);                    
                    Date value = (Date) m.invoke(model);
                    if(value != null){
                        System.out.println("attribute value:"+value.toLocaleString());
                    }
                }                
            }
    }

  //遍历sqspb类 成员为String类型 属性为空的全部替换为“/”
  Field[] fields = sqspb.getClass().getDeclaredFields();
  for (int i = 0; i < fields.length; i++) {
    // 获取属性的名字
    String name = fields[i].getName();
    // 将属性的首字符大写,方便构造get,set方法
    name = name.substring(0, 1).toUpperCase() + name.substring(1);
    // 获取属性的类型
    String type = fields[i].getGenericType().toString();
    // 如果type是类类型,则前面包含"class ",后面跟类名
    if (type.equals("class java.lang.String")) {
        Method m = sqspb.getClass().getMethod("get" + name);
        // 调用getter方法获取属性值
        String value = (String) m.invoke(sqspb);
        System.out.println("数据类型为:String");
        if (value == null || value.equals("")) {
          //set值
          Class[] parameterTypes = new Class[1];
          parameterTypes[0] = fields[i].getType();
          m = sqspb.getClass().getMethod("set" + name, parameterTypes);
          String string = new String("/");
          Object[] objects = new Object[1];
          objects[0] = string;
          m.invoke(sqspb, objects);
         }
     }
  }