• 对象工具类


    对象工具类,提供对象克隆、获取对象属性、类型判断、Map转换对象、对象转Map、设置对象属性等。


    源码如下:(点击下载 -  ObjectUtils.java 、JsonUtils.java 、gson-2.2.4.jar 、commons-lang-2.6.jar)

      1 import java.io.Serializable;
      2 import java.lang.reflect.Field;
      3 import java.lang.reflect.Method;
      4 import java.util.Map;
      5 import org.apache.commons.lang.SerializationUtils;
      6 import org.apache.commons.lang.StringUtils;
      7 
      8 /**
      9  * 对象工具类
     10  * 
     11  */
     12 public class ObjectUtils {
     13 
     14     private static final Class<?>[] BASIC_NUMBER_CLASSES = new Class[] {
     15             short.class, int.class, long.class, float.class, double.class };
     16 
     17     /**
     18      * 对象克隆
     19      *
     20      * @param t
     21      *            a T object.
     22      * @param <T>
     23      *            a T object.
     24      * @return a T object.
     25      */
     26     @SuppressWarnings("unchecked")
     27     public static final <T> T clone(T t) {
     28         if (t == null) {
     29             return null;
     30         }
     31         if (t instanceof Serializable) {
     32             return (T) SerializationUtils.clone((Serializable) t);
     33         }
     34         T result = null;
     35         if (t instanceof Cloneable) {
     36             try {
     37                 result = (T) ObjectUtils.clone(t);
     38             } catch (Throwable e) {
     39             }
     40         }
     41         if (result == null) {
     42             String json = JsonUtils.toJson(t);
     43             result = (T) JsonUtils.fromJson(json, t.getClass());
     44         }
     45         return result;
     46     }
     47 
     48     /**
     49      * 访问对象成员属性值值
     50      * 
     51      * @param obj
     52      * @param field
     53      * @return
     54      * @throws YichaException
     55      */
     56     @SuppressWarnings("rawtypes")
     57     private static final Object getFieldValue(Object obj, String field) throws Exception {
     58         Object result = null;
     59         if (obj instanceof Map) {
     60             return ((Map) obj).get(field);
     61         }
     62 
     63         if (obj == null) {
     64             return null;
     65         }
     66 
     67         Method getterMethod = null;
     68         try {
     69             getterMethod = obj.getClass().getMethod("get" + StringUtils.capitalize(field));
     70         } catch (Exception e) {
     71         }
     72         if (getterMethod == null) {
     73             try {
     74                 getterMethod = obj.getClass().getMethod("is" + StringUtils.capitalize(field));
     75             } catch (Exception e) {
     76             }
     77         }
     78         if (getterMethod == null) {
     79             Field privateField;
     80             try {
     81                 privateField = obj.getClass().getDeclaredField(field);
     82                 privateField.setAccessible(true);
     83                 result = privateField.get(obj);
     84             } catch (Exception e) {
     85                 throw new Exception("field[" + field + "] doesn't exist.");
     86             }
     87         } else {
     88             try {
     89                 result = getterMethod.invoke(obj);
     90             } catch (Exception e) {
     91             }
     92         }
     93         return result;
     94     }
     95 
     96     /**
     97      * 获取对象属性值
     98      *
     99      * @param obj
    100      *            被取值的对象
    101      * @param clazz
    102      *            返回值的类型
    103      * @param path
    104      *            格式:field1.field2.field3
    105      * @param <T>
    106      *            a T object.
    107      * @return a T object.
    108      */
    109     @SuppressWarnings("unchecked")
    110     public static final <T> T getValue(Object obj, Class<T> clazz, String path) {
    111         Object o = getValue(obj, path);
    112         return o == null ? null : (T) o;
    113     }
    114 
    115     /**
    116      * <p>
    117      * getValue.
    118      * </p>
    119      *
    120      * @param obj
    121      *            a {@link java.lang.Object} object.
    122      * @param path
    123      *            a {@link java.lang.String} object.
    124      * @return a {@link java.lang.Object} object.
    125      */
    126     public static final Object getValue(Object obj, String path) {
    127         if (obj == null || StringUtils.isBlank(path)) {
    128             return null;
    129         }
    130         String[] arr = StringUtils.split(path, ".");
    131         Object o = obj;
    132         for (int i = 0, len = arr.length; i < len; i++) {
    133             final String field = StringUtils.strip(arr[i]);
    134             try {
    135                 o = getFieldValue(o, field);
    136             } catch (Exception e) {
    137                 o = null;
    138             }
    139         }
    140         return o;
    141     }
    142 
    143     /**
    144      * 判断是否是数字类型
    145      *
    146      * @param obj
    147      *            a {@link java.lang.Object} object.
    148      * @return a boolean.
    149      */
    150     public static final boolean isNumberType(Object obj) {
    151         if (obj == null) {
    152             throw new RuntimeException("object is null.");
    153         }
    154         if (obj instanceof Number) {
    155             return true;
    156         } else {
    157             for (Class<?> clazz : BASIC_NUMBER_CLASSES) {
    158                 if (obj.getClass().equals(clazz)) {
    159                     return true;
    160                 }
    161             }
    162         }
    163         return false;
    164     }
    165 
    166     /**
    167      * 判断对象是否为零
    168      *
    169      * @param obj
    170      *            a {@link java.lang.Object} object.
    171      * @return a boolean.
    172      */
    173     public static final boolean isZero(Object obj) {
    174         if (!isNumberType(obj)) {
    175             return false;
    176         }
    177         final String foo = String.valueOf(obj);
    178         return StringUtils.equals(foo, "0") || StringUtils.equals(foo, "0.0");
    179     }
    180 
    181     /**
    182      * Map转换对象
    183      *
    184      * @param map
    185      *            a {@link java.util.Map} object.
    186      * @param clazz
    187      *            a {@link java.lang.Class} object.
    188      * @param <T>
    189      *            a T object.
    190      * @return a T object.
    191      */
    192     public static final <T> T fromMap(final Map<String, Object> map, Class<T> clazz) {
    193         return JsonUtils.fromJson(JsonUtils.toJson(map), clazz);
    194     }
    195 
    196     /**
    197      * 对象转Map
    198      *
    199      * @param object
    200      *            a {@link java.lang.Object} object.
    201      * @return a {@link java.util.Map} object.
    202      */
    203     public static final Map<String, Object> toMap(final Object object) {
    204         return JsonUtils.fromJson(JsonUtils.toJson(object));
    205     }
    206 
    207     /**
    208      * 设置对象属性
    209      *
    210      * @param object
    211      *            a {@link java.lang.Object} object.
    212      * @param field
    213      *            a {@link java.lang.String} object.
    214      * @param value
    215      *            a T object.
    216      * @param paramType
    217      *            a {@link java.lang.Class} object.
    218      * @param <T>
    219      *            a T object.
    220      */
    221     @SuppressWarnings("rawtypes")
    222     public static final <T> void setValue(final Object object, final String field, final T value, final Class paramType) {
    223         try {
    224             Method md = object.getClass().getMethod("set" + StringUtils.capitalize(field), paramType);
    225             if (md != null) {
    226                 md.invoke(object, value);
    227             }
    228         } catch (Throwable e) {
    229             throw new RuntimeException(e);
    230         }
    231     }
    232 
    233     /**
    234      * 设置对象属性
    235      *
    236      * @param object
    237      *            a {@link java.lang.Object} object.
    238      * @param field
    239      *            a {@link java.lang.String} object.
    240      * @param value
    241      *            a T object.
    242      * @param <T>
    243      *            a T object.
    244      */
    245     public static final <T> void setValue(final Object object, final String field, final T value) {
    246         try {
    247             for (Method method : object.getClass().getMethods()) {
    248                 if (StringUtils.equals(method.getName(), "set" + StringUtils.capitalize(field))) {
    249                     method.invoke(object, value);
    250                     break;
    251                 }
    252             }
    253         } catch (Throwable e) {
    254             throw new RuntimeException(e);
    255         }
    256     }
    257 
    258 }
  • 相关阅读:
    BZOJ 3157 &数学乱搞...
    BZOJ 2654 & 玄学二分+MST
    概率与期望的一些问题整理
    BZOJ1024&丝帛搜索
    BZOJ 1260&UVa 4394 区间DP
    hdu Atlantis(线段树的面积并)
    hdu Stars in Your Window(线段树,涉及扫描线建立)
    hdu Potted Flower(线段树,求区间的最值)
    hdu Frequent values (线段树 求最值)
    hdu Stars(树状数组)
  • 原文地址:https://www.cnblogs.com/zhoubang521/p/5200601.html
Copyright © 2020-2023  润新知