• 获取对象的属性和值


      /**
    * 获取属性名数组
    * */
    public static List<String> getFiledName(Object o){
    Field[] fields=o.getClass().getDeclaredFields();
    // String[] fieldNames=new String[fields.length];
    List<String> fieldNames=new ArrayList<String>();
    List<Class> fieldTypes=new ArrayList<Class>();
    for(int i=0;i<fields.length;i++){
    Class fieldClazz =fields[i].getType(); // 得到field的class及类型全路径

    if(fields[i].getName().equals("serialVersionUID")) continue;
    if (fields[i].isSynthetic()) continue;// 是否动态加入属性

    fieldNames.add(fields[i].getName());
    fieldTypes.add(fieldClazz);
    }
    return fieldNames;
    }
    /**
    * 获取属性类型
    * */
    public static List<Class> getFiledType(Object o){
    Field[] fields=o.getClass().getDeclaredFields();

    List<Class> fieldTypes=new ArrayList<Class>();
    for(int i=0;i<fields.length;i++){
    Class fieldClazz =fields[i].getType(); // 得到field的class及类型全路径

    if(fields[i].getName().equals("serialVersionUID")) continue;
    if (fields[i].isSynthetic()) continue;// 是否动态加入属性

    fieldTypes.add(fieldClazz);
    }
    return fieldTypes;
    }
    /**
    * 根据属性名获取属性值
    * */
    public static Object getFieldValueByName(String fieldName, Object o) {
    try {
    String firstLetter = fieldName.substring(0, 1).toUpperCase();
    String getter = "get" + firstLetter + fieldName.substring(1);
    // Method method = o.getClass().getMethod(getter, new Class[] {});
    // Object value = method.invoke(o, new Object[] {});

    Method m = o.getClass().getMethod(getter);
    Object value3 = (Object) m.invoke(o);

    return value3;
    } catch (Exception e) {
    String bb= e.getCause().toString();
    return null;
    }
    }
    -===================================================
    1. /** 
    2.  * 根据属性名获取属性值 
    3.  * */  
    4.    private Object getFieldValueByName(String fieldName, Object o) {  
    5.        try {    
    6.            String firstLetter = fieldName.substring(0, 1).toUpperCase();    
    7.            String getter = "get" + firstLetter + fieldName.substring(1);    
    8.            Method method = o.getClass().getMethod(getter, new Class[] {});    
    9.            Object value = method.invoke(o, new Object[] {});    
    10.            return value;    
    11.        } catch (Exception e) {    
    12.            log.error(e.getMessage(),e);    
    13.            return null;    
    14.        }    
    15.    }   
    16.      
    17.    /** 
    18.     * 获取属性名数组 
    19.     * */  
    20.    private String[] getFiledName(Object o){  
    21.     Field[] fields=o.getClass().getDeclaredFields();  
    22.         String[] fieldNames=new String[fields.length];  
    23.     for(int i=0;i<fields.length;i++){  
    24.         System.out.println(fields[i].getType());  
    25.         fieldNames[i]=fields[i].getName();  
    26.     }  
    27.     return fieldNames;  
    28.    }  
    29.      
    30.    /** 
    31.     * 获取属性类型(type),属性名(name),属性值(value)的map组成的list 
    32.     * */  
    33.    private List getFiledsInfo(Object o){  
    34.     Field[] fields=o.getClass().getDeclaredFields();  
    35.         String[] fieldNames=new String[fields.length];  
    36.         List list = new ArrayList();  
    37.         Map infoMap=null;  
    38.     for(int i=0;i<fields.length;i++){  
    39.         infoMap = new HashMap();  
    40.         infoMap.put("type", fields[i].getType().toString());  
    41.         infoMap.put("name", fields[i].getName());  
    42.         infoMap.put("value", getFieldValueByName(fields[i].getName(), o));  
    43.         list.add(infoMap);  
    44.     }  
    45.     return list;  
    46.    }  
    47.      
    48.    /** 
    49.     * 获取对象的所有属性值,返回一个对象数组 
    50.     * */  
    51.    public Object[] getFiledValues(Object o){  
    52.     String[] fieldNames=this.getFiledName(o);  
    53.     Object[] value=new Object[fieldNames.length];  
    54.     for(int i=0;i<fieldNames.length;i++){  
    55.         value[i]=this.getFieldValueByName(fieldNames[i], o);  
    56.     }  
    57.     return value;  
    58.    }      
      1. http://blog.csdn.net/linshutao/article/details/7693625
        1. =======================================================
        2.     public  static  void  aa (Object model) throws SecurityException,
          NoSuchMethodException, IllegalArgumentException,
          IllegalAccessException, InvocationTargetException, InstantiationException{
          Field[] fs = model.getClass().getDeclaredFields(); // 得到所有的fields

          for(Field f : fs)
          {
          Class fieldClazz = f.getType(); // 得到field的class及类型全路径

          if(fieldClazz.isPrimitive()) continue; //【1】 //判断是否为基本类型

          if(fieldClazz.getName().startsWith("java.lang")) continue; //getName()返回field的类型全路径;

          if(fieldClazz.isAssignableFrom(List.class)) //【2】
          {
          Type fc = f.getGenericType(); // 关键的地方,如果是List类型,得到其Generic的类型

          if(fc == null) continue;

          if(fc instanceof ParameterizedType) // 【3】如果是泛型参数的类型
          {
          ParameterizedType pt = (ParameterizedType) fc;

          Class genericClazz = (Class)pt.getActualTypeArguments()[0]; //【4】 得到泛型里的class类型对象。
          Map<String, Class> m=new HashMap<String, Class>();
          m.put(f.getName(), genericClazz);

          // Map<String, Class> m1 = prepareMap(genericClazz);
          //
          // m.putAll(m1);
          }
          }
          }
          }
  • 相关阅读:
    WIN10解决:失败 – 检测到病毒文件下载失败问题
    Jinja2学习
    div设置百分比高度 宽度
    flask静态html
    python排序之冒泡排序
    python中的break continue之用法
    python中的break continue用法
    python格式化输出(% format用法)
    python基础数据类型
    linux下anaconda使用教程
  • 原文地址:https://www.cnblogs.com/manmanlu/p/7595700.html
Copyright © 2020-2023  润新知