java 代码分享
2029 点击·0 回帖
![]() | ![]() | |
![]() | 以下代码可以在有“代码混淆器,混淆后正常工作”,希望分享给有可以使用到的人,(*^__^*) 嘻嘻 /* * HashMapToPo 转换为集合对象 */ public static <T> ArrayList<T> HashMapToPoList( final ArrayList<HashMap<String, Object>> hmList, final Class<T> clazz, boolean encode, boolean isEncode) throws ForerDealArgumentException { if (hmList == null || clazz == null) { String msg = className + ".HashMapToPoList(final ArrayList<HashMap<String, Object>> hmList,final Class<?> clazz,boolean encode) Args is not null"; logger.error(msg); throw new ForerDealArgumentException(msg); } final HashMap<String, String> dic = GetObjectPropertyName(clazz); final HashMap<String, Method> dic2 = GetObjectMethodName(clazz); final ArrayList<T> lt = new ArrayList<T>(); for (HashMap<String, Object> hm : hmList) { T o = null; try { o = clazz.newInstance(); } catch (final Exception e) { e.printStackTrace(); } for (Map.Entry<String, Object> entry : hm.entrySet()) { String key = entry.getKey().toUpperCase().replace("_", ""); Object value = entry.getValue(); if (dic.containsKey(key) ;; value != null) { try { String str = dic.get(key); String temp = str.substring(0, 1); str = str.replaceFirst(temp, temp.toUpperCase()); str = "set" + str; Class<?> paraType = dic2.get(str).getParameterTypes()[0]; if (paraType != value.getClass())// 类型一致 value = StringToObject(paraType, value.toString()); if (isEncode) { if (encode) {// iso8859-1 编码 if (paraType == String.class) { value = CharsetConvert.charsetConvert(value .toString()); } } else {// GBK if (paraType == String.class) { value = CharsetConvert .ISO_8859_1ToGBK(value.toString()); } } } try { dic2.get(str).invoke(o, value); } catch (Exception e) { logger.error("paraType:" + paraType + ";valueType:" + value.getClass() + "[" + str + ":" + value + "]"); } } catch (Exception e) { e.printStackTrace(); } } else if (!dic.containsKey(key)) { String msg = clazz.getName() + "Object does not contain the '" + key + "' field"; logger.debug(msg); } } lt.add(o); } return lt; } /* * 字段类型转换 */ private static Object StringToObject(Class<?> clazz, String str) { Object o = str; if (clazz == Date.class ;; str != null ;; str != "") { DateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd HH:mms"); try { o = dt1.parse(str); } catch (ParseException e1) { DateFormat dt2 = new SimpleDateFormat("yyyy/MM/dd HH:mms"); try { o = dt2.parse(str); } catch (ParseException e2) { DateFormat dt3 = new SimpleDateFormat("yyyyMMdd HH:mms"); try { o = dt3.parse(str); } catch (ParseException e3) { DateFormat dt4 = new SimpleDateFormat("yyyyMMdd"); try { o = dt4.parse(str); } catch (ParseException e4) { e4.printStackTrace(); } } } } } else if (clazz == BigDecimal.class) { o = new BigDecimal(str); } else if (clazz == Long.class) { o = new Long(str); } else if (clazz == Integer.class) { o = new Integer(str); } else if (clazz == int.class) { o = Integer.parseInt(str); } else if (clazz == float.class) { o = Float.parseFloat(str); } else if (clazz == boolean.class) { o = Boolean.parseBoolean(str); } else if (clazz == byte.class) { o = Byte.parseByte(str); } return o; } /* * HashMap转换为单一对象www.atcpu.com */ public static <T> T HashMapToSinglePo(final HashMap<String, Object> hm, final Class<T> clazz, boolean encode, boolean isEncode) throws Exception { if (hm == null || clazz == null) { String msg = className + ".HashMapToSinglePo(final ArrayList<HashMap<String, Object>,boolean encode> hmList,final Class<T> clazz) Args is null"; logger.error(msg); } T o = null; ArrayList<HashMap<String, Object>> hmList = new ArrayList<HashMap<String, Object>>(); hmList.add(hm); ArrayList<T> lt = HashMapToPoList(hmList, clazz, encode, isEncode); if (lt != null ;; lt.size() > 0) { o = lt.get(0); } return o; } /* * 对象比较 ,结果HashMap<String, String> key 为checkColums 字段,value 相同 Same * ,不同Different,不存在Object does not contain the */ public static <T> HashMap<String, String> Compare(Class<T> clazz, T o1, T o2, String[] checkColums) { HashMap<String, String> ht = new HashMap<String, String>(); if (clazz == null || o1 == null || o2 == null) { String msg = className + ".Compare(Class<T> clazz,T o1, T o2,List<String> checkColums) Args clazz,o1,o2 is null"; logger.error(msg); } final HashMap<String, String> dic1 = GetObjectPropertyName(clazz); final HashMap<String, Method> dic2 = GetObjectMethodName(clazz); if (checkColums == null) { int count = dic1.size(); checkColums = new String[count]; dic1.values().toArray(checkColums); } for (String str : checkColums) { try { String temp = str.toUpperCase(); if (dic1.containsKey(temp)) { String str1 = dic1.get(temp); String temp1 = str1.substring(0, 1); str1 = str1.replaceFirst(temp1, temp1.toUpperCase()); str1 = "get" + str1; Object result1 = dic2.get(str1).invoke(o1); Object result2 = dic2.get(str1).invoke(o2); if (result1 != null || result2 != null) { if (result1 != null ;; result2 != null) { if (!result1.equals(result2)) { ht.put(str, "Different"); } else { ht.put(str, "Same"); } } else { ht.put(str, "Different"); } } } else { String msg = clazz.getName() + " Object does not contain the '" + str + "' field"; ht.put(str, msg); logger.debug(msg); } } catch (Exception e) { e.printStackTrace(); logger.equals(e); } } return ht; } /* * 对象克隆 */ public static <T> T Clone(Class<T> clazz, T o) { if (o == null || clazz == null) { String msg = className + ".Clone(Class<T> clazz,Object o) Args clazz or o is null"; logger.error(msg); } T cloneObject = null; try { cloneObject = clazz.newInstance(); } catch (final Exception e) { e.printStackTrace(); } final HashMap<String, String> dic = GetObjectPropertyName(clazz); final HashMap<String, Method> dic2 = GetObjectMethodName(clazz); for (Map.Entry<String, String> entry : dic.entrySet()) { try { String str = entry.getValue(); String temp = str.substring(0, 1); str = str.replaceFirst(temp, temp.toUpperCase()); String str1 = "get" + str; String str2 = "set" + str; if (dic2.containsKey(str1) ;; dic2.containsKey(str2)) { Object result = dic2.get(str1).invoke(o); dic2.get(str2).invoke(cloneObject, result); } else { String msg = str1 + ";" + str2 + "Error"; System.out.println(msg); logger.error(msg); } } catch (Exception e) { e.printStackTrace(); logger.error(e); } } return cloneObject; } /* * 反射获取对象字段 */ private static HashMap<String, HashMap<String, String>> propertys = new HashMap<String, HashMap<String, String>>(); private static HashMap<String, HashMap<String, Method>> methods = new HashMap<String, HashMap<String, Method>>(); private static HashMap<String, String> GetObjectPropertyName(Class<?> clazz) { HashMap<String, Method> method = GetObjectMethodName(clazz); String className = clazz.getName(); HashMap<String, String> ht = new HashMap<String, String>(); if (propertys.containsKey(className)) { ht = propertys.get(className); } else { ht = new HashMap<String, String>(); getPropertys(clazz, ht, method);// 递归父类 propertys.put(className, ht); } return ht; } private static HashMap<String, String> getPropertys(Class<?> clazz, HashMap<String, String> ht, HashMap<String, Method> method) { Method[] md = clazz.getMethods(); for (int i = 0; i < md.length; i++) { String str = md.getName().replace("get", "").replace("set", ""); if (method.containsKey("get" + str) ;; method.containsKey("set" + str)) { if (!ht.containsKey(str)) { ht.put(str.toUpperCase(), str); } } } Class<?> superClazz = clazz.getSuperclass(); if (superClazz != null ;; superClazz != Object.class) { getPropertys(superClazz, ht, method); } return ht; } /* * 反射获取类的方法名 */ private static HashMap<String, Method> GetObjectMethodName(Class<?> clazz) { HashMap<String, Method> ht = null; String className = clazz.getName(); if (methods.containsKey(className)) { ht = methods.get(className); } else { ht = new HashMap<String, Method>(); getMethods(clazz, ht);// 递归父类 methods.put(className, ht); } return ht; } private static HashMap<String, Method> getMethods(Class<?> clazz, HashMap<String, Method> ht) { Method[] md = clazz.getMethods(); for (int i = 0; i < md.length; i++) { ht.put(md.getName(), md); } Class<?> superClazz = clazz.getSuperclass(); if (superClazz != null ;; superClazz != Object.class) { getMethods(superClazz, ht); } return ht; } | |
![]() | ![]() |