tags:

views:

733

answers:

2

BeanUtils copyProperties, out of the box, doesn't seem to handle copying from Boolean object properties to boolean primitive properties.

I figured I could create and register a converter to handle this, but that just didn't seem to work.

So, how can I use BeanUtils to copy the properties from class Source to class Destination where:

public class Destination {

    private boolean property;

    public boolean isProperty() {
     return property;
    }

    public void setProperty(boolean property) {
     this.property = property;
    }
}


public class Source{

    private Boolean property;

    public Boolean getProperty() {
     return property;
    }

    public void setProperty(Boolean property) {
     this.property = property;
    }
}
A: 

It is actually vice-versa:

public static void main(String[] args) throws Exception {
    Source d = new Source();
    d.setProperty(Boolean.TRUE);
    BeanMap beanMap = new BeanMap(d);

    Destination s = new Destination();
    BeanUtils.populate(s, beanMap);
    System.out.println("s.getProperty()=" + s.isProperty());
}
Boris Pavlović
A: 

public class Destination {

private boolean property;

public boolean isProperty() { //code getProperty() instead
    return property;
}

public void setProperty(boolean property) {
    this.property = property;
}

}

hony