1 importjava.io.Serializable;2 importjava.lang.reflect.Field;3 importjava.lang.reflect.Method;4 importjava.util.Map;5 importorg.apache.commons.lang.SerializationUtils;6 importorg.apache.commons.lang.StringUtils;7
8 /**
9 * 对象工具类10 *11 */
12 public classObjectUtils {13
14 private static final Class>[] BASIC_NUMBER_CLASSES = newClass[] {15 short.class, int.class, long.class, float.class, double.class};16
17 /**
18 * 对象克隆19 *20 *@paramt21 * a T object.22 *@param23 * a T object.24 *@returna T object.25 */
26 @SuppressWarnings("unchecked")27 public static final T clone(T t) {28 if (t == null) {29 return null;30 }31 if (t instanceofSerializable) {32 return(T) SerializationUtils.clone((Serializable) t);33 }34 T result = null;35 if (t instanceofCloneable) {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 returnresult;46 }47
48 /**
49 * 访问对象成员属性值值50 *51 *@paramobj52 *@paramfield53 *@return
54 *@throwsYichaException55 */
56 @SuppressWarnings("rawtypes")57 private static final Object getFieldValue(Object obj, String field) throwsException {58 Object result = null;59 if (obj instanceofMap) {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 returnresult;94 }95
96 /**
97 * 获取对象属性值98 *99 *@paramobj100 * 被取值的对象101 *@paramclazz102 * 返回值的类型103 *@parampath104 * 格式:field1.field2.field3105 *@param106 * a T object.107 *@returna T object.108 */
109 @SuppressWarnings("unchecked")110 public static final T getValue(Object obj, Classclazz, String path) {111 Object o =getValue(obj, path);112 return o == null ? null: (T) o;113 }114
115 /**
116 *
117 * getValue.118 *
119 *120 *@paramobj121 * a {@linkjava.lang.Object} object.122 *@parampath123 * a {@linkjava.lang.String} object.124 *@returna {@linkjava.lang.Object} object.125 */126 public static finalObject 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 returno;141 }142
143 /**
144 * 判断是否是数字类型145 *146 *@paramobj147 * a {@linkjava.lang.Object} object.148 *@returna boolean.149 */
150 public static final booleanisNumberType(Object obj) {151 if (obj == null) {152 throw new RuntimeException("object is null.");153 }154 if (obj instanceofNumber) {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 *@paramobj170 * a {@linkjava.lang.Object} object.171 *@returna boolean.172 */
173 public static final booleanisZero(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 *@parammap185 * a {@linkjava.util.Map} object.186 *@paramclazz187 * a {@linkjava.lang.Class} object.188 *@param189 * a T object.190 *@returna T object.191 */
192 public static final T fromMap(final Map map, Classclazz) {193 returnJsonUtils.fromJson(JsonUtils.toJson(map), clazz);194 }195
196 /**
197 * 对象转Map198 *199 *@paramobject200 * a {@linkjava.lang.Object} object.201 *@returna {@linkjava.util.Map} object.202 */
203 public static final Map toMap(finalObject object) {204 returnJsonUtils.fromJson(JsonUtils.toJson(object));205 }206
207 /**
208 * 设置对象属性209 *210 *@paramobject211 * a {@linkjava.lang.Object} object.212 *@paramfield213 * a {@linkjava.lang.String} object.214 *@paramvalue215 * a T object.216 *@paramparamType217 * a {@linkjava.lang.Class} object.218 *@param219 * a T object.220 */
221 @SuppressWarnings("rawtypes")222 public static final void setValue(final Object object, final String field, final T value, finalClass 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 newRuntimeException(e);230 }231 }232
233 /**
234 * 设置对象属性235 *236 *@paramobject237 * a {@linkjava.lang.Object} object.238 *@paramfield239 * a {@linkjava.lang.String} object.240 *@paramvalue241 * a T object.242 *@param243 * a T object.244 */
245 public static final void setValue(final Object object, final String field, finalT 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 newRuntimeException(e);255 }256 }257
258 }