有这样一种场景,ClassA 与 ClassB 俩个类,这俩个类中的属性有很大一部分是相同的。比如:在使用MongoDB存储时,ClassA
是商品信息,ClassB 是库存信息,ClassB的很多属性会和ClassA相同(原因:MongoDB关联查询性能会急速下降)。
这个时候已经创建好ClassA 类型的实例,需要创建ClassB类型的实例。
有如下俩种方式:
1、调用ClassB 每一个属性的set 方法。
2、通过反射设置;具体如下:
a> 获取ClassA 的所有属性名称
/**
* 获取所有属性
* @param instance
* @return
*/
public static String[] getAllTrributeNames(Object instance){
Field[] fields = instance.getClass().getDeclaredFields();
String[] attNames = new String[fields.length];
for(int i = 0; i < fields.length; i++){
attNames[i] = fields[i].getName();
}
return attNames;
}
b>循环属性,给ClassB 设置值
/**
* 获取属性值
* @param instance
* @param name
* @param value
*/
public static void setAttribute(Object instance, String name, Object value){
try {
Field field = instance.getClass().getDeclaredField(name);
field.setAccessible(true);
field.set(instance, value);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/**
* 设置属性值
* @param instance
* @param name
* @return
*/
public static Object getAttribute(Object instance, String name){
Object obj = null;
Field field;
try {
field = instance.getClass().getDeclaredField(name);
field.setAccessible(true);
obj = field.get(instance);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return obj;
}
UserBean bean2 = (UserBean) getInstance(UserBean.class);
System.out.println(bean2.toString());
String[] attNames = getAllTrributeNames(bean1);
for(String att : attNames){
Object value = getAttribute(bean1, att);
setAttribute(bean2, att, value);
}
但是在获取类的属性的时候,有俩个方法:getField 和 getDeclaredField。
看JDK源码,发现 getField 只获取 public 的属性,getDeclaredField 获取私有的属性 ;
俩个方法获取属性,都会调用 searchFields 方法,在getField 的时候,privateGetDeclaredFields会是false,但是getDeclaredField 是 true
public Field getDeclaredField(String name)
throws NoSuchFieldException, SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
Field field = searchFields(privateGetDeclaredFields(false), name);
if (field == null) {
throw new NoSuchFieldException(name);
}
return field;
}
public Field getField(String name)
throws NoSuchFieldException, SecurityException {
checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
Field field = getField0(name);
if (field == null) {
throw new NoSuchFieldException(name);
}
return field;
}
private Field getField0(String name) throws NoSuchFieldException {
// Note: the intent is that the search algorithm this routine
// uses be equivalent to the ordering imposed by
// privateGetPublicFields(). It fetches only the declared
// public fields for each class, however, to reduce the number
// of Field objects which have to be created for the common
// case where the field being requested is declared in the
// class which is being queried.
Field res;
// Search declared public fields
if ((res = searchFields(privateGetDeclaredFields(true), name)) != null) {
return res;
}