java 代码分享 (一)

2014-11-24 07:34:25 · 作者: · 浏览: 0

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

* HashMapToPo 转换为集合对象
*/
public static ArrayList HashMapToPoList(
final ArrayList> hmList,
final Class clazz, boolean encode, boolean isEncode)
throws ForerDealArgumentException {
if (hmList == null || clazz == null) {
String msg = className
+ ".HashMapToPoList(final ArrayList> hmList,final Class< > clazz,boolean encode) Args is not null";
logger.error(msg);
throw new ForerDealArgumentException(msg);
}
final HashMap dic = GetObjectPropertyName(clazz);
final HashMap dic2 = GetObjectMethodName(clazz);
final ArrayList lt = new ArrayList();
for (HashMap hm : hmList) {
T o = null;
try {
o = clazz.newInstance();
} catch (final Exception e) {
e.printStackTrace();
}
for (Map.Entry 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.2cto.com
*/
public static T HashMapToSinglePo(final HashMap hm,
final Class clazz, boolean encode, boolean isEncode)
throws Exception {
if (hm == null || clazz == null) {
String msg = className
+ ".HashMapToSinglePo(final ArrayList,boolean encode> hmList,final Class clazz) Args is null";
logger.error(msg);
}
T o = null;
ArrayList> hmList = new ArrayList>();
hmList.add(hm);
ArrayList lt = HashMapToPoList(hmList, clazz, encode, isEncode);
if (lt != null && lt.size() > 0) {
o = lt.get(0);
}
return o;
}
/*
* 对象比较 ,结果HashMap key 为checkColums 字段,value 相同 Same
* ,不同Different,不存在Object does n