如何将groovy属性复制到JavaBean属性

如何将groovy属性复制到JavaBean属性,java,grails,groovy,Java,Grails,Groovy,我想将groovy对象属性复制到另一个java对象,我知道groovy到groovy是这样的 def copyProperties(source, target) { source.properties.each { key, value -> if (target.hasProperty(key) && !(key in ['class', 'metaClass'])) target[key] = value } }

我想将groovy对象属性复制到另一个java对象,我知道groovy到groovy是这样的

def copyProperties(source, target) {
    source.properties.each { key, value ->
        if (target.hasProperty(key) && !(key in ['class', 'metaClass']))
            target[key] = value
    }
}
java到java我可以使用apache BeanUtils,但是如何将groovy对象属性复制到java对象属性? 附言: groovy对象

class UserInfo {
    Integer age
    String userName
    String password
}
java对象

 public class UserInfo {
    private int age;
    private String userName;
    private String password;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}
def copyProperties(源、对象目标){
source.properties.each{key,value->

类使用
copyProperties()
进行同样的操作时,您是否遇到任何问题?您还不够努力,可能会使这种方法更加冗长。
def copyProperties(source, Object target) {
    source.properties.each { key, value ->
        Class<? extends Object> toClass = target.getClass();

        try {
            BeanInfo toBean = Introspector.getBeanInfo(toClass);

            PropertyDescriptor[] toPd = toBean.getPropertyDescriptors();

            for (PropertyDescriptor propertyDescriptor : toPd) {
                propertyDescriptor.getDisplayName();

                if (key.equals(
                        propertyDescriptor.getDisplayName())
                        && !(key in ['class', 'metaClass'])) {
                    if(propertyDescriptor.getWriteMethod() != null)
                        propertyDescriptor.getWriteMethod().invoke(target, value);
                }

            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}