对不同对象的相同属性进行赋值,被改变的是des

来源:互联网 发布:飞鸽翻墙软件 编辑:程序博客网 时间:2024/04/29 22:15
/**
* 潜复制,对不同对象的相同属性进行赋值,被改变的是des

* @param source
* @param desObject目标对象
*/
public static void simpleValueCopy(Object source, Object desObject) {
if (source == null || desObject == null)
return;
Method[] sourceMethods = source.getClass().getMethods();
Method[] desMethods = desObject.getClass().getMethods();
for (Method m : sourceMethods) {
String methodName = m.getName();
if (methodName.startsWith("get")) {
String methodTmp = methodName.replaceFirst("get", "set");
for (Method desMethod : desMethods) {
try {
if (methodTmp.equals(desMethod.getName())) {
desMethod.invoke(desObject,
m.invoke(source, new Object[] {}));
}
} catch (Exception e) {
}
}
} else if (methodName.startsWith("is")) {
String methodTmp = methodName.replaceFirst("is", "set");
for (Method desMethod : desMethods) {
try {
if (methodTmp.equals(desMethod.getName())) {
desMethod.invoke(desObject,
m.invoke(source, new Object[] {}));
}
} catch (Exception e) {
}
}
}
}
}
0 0
原创粉丝点击