Android里面要实现和 BeanUtils.copyProperties 一样的功能,有没有方案?

DavidWu 发布于 2014/07/01 21:35
阅读 4K+
收藏 1
 BeanUtils.copyProperties(object, activityTrackerVO); 
无法在android里面使用 
因为android sdk不带java.beans.PropertyDescriptor
有没有其它方案? 
加载中
0
猎户座
猎户座
好像还真没有额,坐等高手。。
0
iuKa
iuKa
把JDK移植过来
0
DavidWu
DavidWu
我自己手工写了个反射工具处理了
0
suju
suju

android里不要使用过多反射特性,在移动设备上不是很好

suju
suju
@DavidWu 13 原来如此
DavidWu
DavidWu
回复 @atearsan : 我们项目遇到了一个问题,就是从服务器拿过来的json 要映射到数据库的实体类,但是字段名有个别不一样,类型也有个别不一样,假如不用反射做,手工一个个提出弄,要写5 6千行的。 用反射做的话,只需要写少量的不同属性就可以完成这工作了。现代手机性能很强,损失点性能换取开发便捷还是划算的。
suju
suju
回复 @atearsan : 倒也不是,其实,大部分客户端需要反射的场景,都可以用普通方式去解决,仅大部分而已
atearsan
atearsan
现在智能机硬件发展速度太快,使用反射也不会带来太多影响吧
0
DavidWu
DavidWu
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//这个类没什么特别的,网上多的是
public class ReflectionUtils {

	public static Method getDeclaredMethod(Object object, String methodName, Class<?>... parameterTypes) {
		Method method = null;

		for (Class<?> clazz = object.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
			try {
				method = clazz.getDeclaredMethod(methodName, parameterTypes);
				return method;
			} catch (Exception e) {

			}
		}

		return null;
	}

	public static Object invokeMethod(Object object, String methodName, Class<?>[] parameterTypes, Object[] parameters) {
		Method method = getDeclaredMethod(object, methodName, parameterTypes);
		method.setAccessible(true);

		try {
			if (null != method) {

				return method.invoke(object, parameters);
			}
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}

		return null;
	}

	public static Field getDeclaredField(Object object, String fieldName) {
		Field field = null;

		Class<?> clazz = object.getClass();

		for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
			try {
				field = clazz.getDeclaredField(fieldName);
				return field;
			} catch (Exception e) {

			}
		}

		return null;
	}

	public static void setFieldValue(Object object, String fieldName, Object value) {

		Field field = getDeclaredField(object, fieldName);

		field.setAccessible(true);

		try {
			field.set(object, value);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

	}

	public static Object getFieldValue(Object object, String fieldName) {

		Field field = getDeclaredField(object, fieldName);

		field.setAccessible(true);

		try {
			return field.get(object);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
}


// 下面这个类是根据项目实际情况写的,写的有点死。

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

public class BeanUtils {
	static ReflectionUtils reflectionUtils = new ReflectionUtils();

	public static void copyProperties(Object dist, Object orig) {

		Field[] fields_orig = orig.getClass().getDeclaredFields();
		Field[] fields_dist = dist.getClass().getDeclaredFields();

		try {
			for (Field field : fields_orig) {
				System.out.println(field.getName());

				String typeNameOfOrig = field.getType().getName();
				String fieldNameOrig = field.getName();
				Method getmethod = reflectionUtils.getDeclaredMethod(orig, addGetString(field.getName()), null);

				Object obj = getmethod.invoke(orig, null);
				for (Field field2 : fields_dist) {
					String typeNameOfdist = field2.getType().getName();
					String fieldNameDist = field2.getName();
					if (StringUtils.equals(fieldNameOrig, fieldNameDist)) {
						Class clazz = Class.forName(typeNameOfdist);
						if (StringUtils.containsIgnoreCase(typeNameOfOrig, "string") && StringUtils.containsIgnoreCase(typeNameOfdist, "string")) {
							Method setmethod = reflectionUtils.getDeclaredMethod(dist, addSetString(field.getName()), clazz);
							setmethod.invoke(dist, obj);

						}
						if (StringUtils.containsIgnoreCase(typeNameOfOrig, "long") && StringUtils.containsIgnoreCase(typeNameOfdist, "long")) {
							Method setmethod = reflectionUtils.getDeclaredMethod(dist, addSetString(field.getName()), clazz);
							setmethod.invoke(dist, obj);

						}

						if (StringUtils.containsIgnoreCase(typeNameOfOrig, "int") && StringUtils.containsIgnoreCase(typeNameOfdist, "int")) {

							Method setmethod = reflectionUtils.getDeclaredMethod(dist, addSetString(field.getName()), clazz);
							setmethod.invoke(dist, obj);
						}

						if (StringUtils.containsIgnoreCase(typeNameOfOrig, "float") && StringUtils.containsIgnoreCase(typeNameOfdist, "float")) {

							Method setmethod = reflectionUtils.getDeclaredMethod(dist, addSetString(field.getName()), clazz);
							setmethod.invoke(dist, obj);
						}

						if (StringUtils.containsIgnoreCase(typeNameOfOrig, "int") && StringUtils.containsIgnoreCase(typeNameOfdist, "float")) {

							Method setmethod = reflectionUtils.getDeclaredMethod(dist, addSetString(field.getName()), clazz);
							setmethod.invoke(dist, obj);
						}

						if (StringUtils.containsIgnoreCase(typeNameOfOrig, "String") && StringUtils.containsIgnoreCase(typeNameOfdist, "date")) {
							Method setmethod = reflectionUtils.getDeclaredMethod(dist, addSetString(field.getName()), clazz);
							Date date = new Date(Long.parseLong(String.valueOf(obj)));
							setmethod.invoke(dist, date);
						}

					}
				}

			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

	}

	public static String addGetString(String fieldName) {
		StringBuffer sb = new StringBuffer();
		sb.append("get");
		sb.append(fieldName.substring(0, 1).toUpperCase());
		sb.append(fieldName.substring(1));
		return sb.toString();
	}

	public static String addSetString(String fieldName) {
		StringBuffer sb = new StringBuffer();
		sb.append("set");
		sb.append(fieldName.substring(0, 1).toUpperCase());
		sb.append(fieldName.substring(1));
		return sb.toString();
	}

}


希望能帮助遇上同样问题的人。

返回顶部
顶部