前边有写过给相同类的对象赋值,今天有研究了比较两个类对象是否相同,当然前提是两个对象属于同一个类。比较的方法都是在ClassReflection类中实现的。下面是主要代码:
package com.example.administrator.beancompare;
import android.util.Log;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
/**
* Created by Administrator on 2015/11/18.
*/
public class ClassReflection {
private static String str = "";
/**
* @param class1 原实体类
* @param class2 已变化实体类
* @author ym
* @CreateTime 2012-11-22下午03:23:23
* 描述:反射实体类赋值
*/
public static String reflectionAttr(Object class1, Object class2) throws Exception {
// Class.forName()方法返回一个类
Class clazz1 = Class.forName(class1.getClass().getName());
Class clazz2 = Class.forName(class2.getClass().getName());
// 获取两个实体类的所有属性
Field[] fields1 = clazz1.getDeclaredFields();
Field[] fields2 = clazz2.getDeclaredFields();
ClassReflection cr = new ClassReflection();
// 遍历class1Bean,获取逐个属性值,然后遍历class2Bean查找是否有相同的属性,如有相同则值进行比较
for (Field f1 : fields1) {
//对于像id之类的属性,如不想比较课忽略,如下
if (f1.getName().equals("id"))
continue;
if (f1.getType() == List.class) {
continue;
}
Log.v("fields1.getType()", f1.getType() + "");
Log.v("getType().getName()", f1.getType().getName() + "");
Log.v("getDeclaringClass()", f1.getDeclaringClass() + "");
Object value1 = cr.invokeGetMethod(class1, f1.getName(), null);
Log.v("value1------>", value1 + "");
for (Field f2 : fields2) {
if (f1.getName().equals(f2.getName())) {
Object value2 = cr.invokeGetMethod(class2, f2.getName(), null);
Log.v("value2------>", value2 + "");
if (!value1.equals(value2)) {
str += f1.getName().toString() + ";";
}
//判断是否是List
// if (f1.getType().getName() == "java.util.List") {
// //返回List中的类型
//
// //判断是否是自定义类
//
// //将两个类传入,重新调用reflectionAttra()
// reflectionAttr(class1, class2);
// continue;
// } else {
// if (!value1.equals(value2)) {
// str += f1.getName().toString() + ";";
// }
// }
}
}
}
return str;
}
/**
* 执行某个Field的getField方法
*
* @param clazz 类
* @param fieldName 类的属性名称
* @param args 参数,默认为null
* @return
*/
private Object invokeGetMethod(Object clazz, String fieldName, Object[] args) {
String methodName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
Method method = null;
try {
method = Class.forName(clazz.getClass().getName()).getDeclaredMethod("get" + methodName);
return method.invoke(clazz);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* 执行某个Field的setField方法
*
* @param clazz 类
* @param fieldName 类的属性名称
* @param args 参数,默认为null
* @return
*/
private Object invokeSetMethod(Object clazz, String fieldName, Object[] args) {
String methodName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
Method method = null;
try {
Class[] parameterTypes = new Class[1];
Class c = Class.forName(clazz.getClass().getName());
Field field = c.getDeclaredField(fieldName);
parameterTypes[0] = field.getType();
method = c.getDeclaredMethod("set" + methodName, parameterTypes);
return method.invoke(clazz, args);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
}
完整的源码下载地址:http://download.csdn.net/detail/hyp1006346386/9347631
如有不足或者不当的地方,欢迎指正!