当前位置:编程学习 > JAVA >>

java 代码分享

以下代码可以在有“代码混淆器,混淆后正常工作”,希望分享给有可以使用到的人,(*^__^*) 嘻嘻
/*

* 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:mm:ss");
try {
o = dt1.parse(str);
} catch (ParseException e1) {
DateFormat dt2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
o = dt2.parse(str);
} catch (ParseException e2) {
DateFormat dt3 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
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.zzzyk.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 + ";" + s

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,