生成代码,把一个bean的属性复制到另一个bean (两个bean未必同类型,但有一些共同的属性)
${:import(java.lang.reflect.Method)}
public static void main(String args[]) {
Class<?> destinClass = ${DestinBean}.class;
String destinObj = "${destinBean}";
Class<?> srcClass = ${SrcBean}.class;
String srcObj = "${srcBean}";
Method[] destinMethods = destinClass.getMethods();
Method[] srcMethods = srcClass.getMethods();
for (Method destinMethod : destinMethods) {
if (destinMethod.getName().startsWith("set")
&& destinMethod.getParameterTypes().length == 1) {
String setterName = destinMethod.getName();
String prop = setterName.substring("set".length());
if (prop.length() == 1) {
prop = prop.toUpperCase();
} else {
prop = prop.substring(0, 1).toUpperCase()
+ prop.substring(1);
}
String inBracket = "unknown";
for (Method srcMethod : srcMethods) {
if (srcMethod.getName().startsWith("get" + prop)) {
inBracket = srcObj + "." + "get" + prop + "()";
break;
}
if (srcMethod.getName().startsWith("is" + prop)) {
inBracket = srcObj + "." + "is" + prop + "()";
break;
}
}
System.out.println(destinObj + "." + destinMethod.getName()
+ "(" + inBracket + ");");
}
}
}