java 代码分享 (二)

2014-11-24 07:34:25 · 作者: · 浏览: 1
ot contain the
*/
public static HashMap Compare(Class clazz, T o1,
T o2, String[] checkColums) {
HashMap ht = new HashMap();
if (clazz == null || o1 == null || o2 == null) {
String msg = className
+ ".Compare(Class clazz,T o1, T o2,List checkColums) Args clazz,o1,o2 is null";
logger.error(msg);
}
final HashMap dic1 = GetObjectPropertyName(clazz);
final HashMap 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 Clone(Class clazz, T o) {
if (o == null || clazz == null) {
String msg = className
+ ".Clone(Class 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 dic = GetObjectPropertyName(clazz);
final HashMap dic2 = GetObjectMethodName(clazz);
for (Map.Entry 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> propertys = new HashMap>();
private static HashMap> methods = new HashMap>();
private static HashMap GetObjectPropertyName(Class< > clazz) {
HashMap method = GetObjectMethodName(clazz);
String className = clazz.getName();
HashMap ht = new HashMap();
if (propertys.containsKey(className)) {
ht = propertys.get(className);
} else {
ht = new HashMap();
getPropertys(clazz, ht, method);// 递归父类
propertys.put(className, ht);
}
return ht;
}
private static HashMap getPropertys(Class< > clazz,
HashMap ht, HashMap method) {
Method[] md = clazz.getMethods();
for (int i = 0; i < md.length; i++) {
String str = md[i].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 GetObjectMethodName(Class< > clazz) {
HashMap ht = null;
String className = clazz.getName();
if (methods.containsKey(className)) {
ht = methods.get(className);
} else {
ht = new HashMap();
getMethods(clazz, ht);// 递归父类
methods.put(className, ht);
}
return ht