代码
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public class GetSetUtil {
public static void main(String[] args) {
setMethod(RightsItemDTO.class,RightsItemVO.class,"rightsItemDTO","rightsItemVO");
}
private static void setMethod(Class sourceClass,Class targetClass,String sourceName,String targeName){
Method[] sourceMethods = sourceClass.getMethods();
Method[] targetMethods = targetClass.getMethods();
Map sourceMethodMap = new HashMap<>();
Map targetMethodMap = new HashMap<>();
for (Method sourceMethod : sourceMethods) {
sourceMethodMap.put(sourceMethod.getName(),sourceMethod);
}
for (Method targetMethod : targetMethods) {
targetMethodMap.put(targetMethod.getName(),targetMethod);
}
for (Map.Entry stringMethodEntry : sourceMethodMap.entrySet()) {
Method method = targetMethodMap.get(stringMethodEntry.getKey());
if(method == null){
continue;
}
String name = method.getName();
if(name.startsWith("get") && !"getClass".equals(name)){
String setName = name.substring(3);
System.out.println(targeName+".set"+setName+"("+sourceName+"."+name+"());");
}
}
}
}