Discussion:
[beanutils] BeanUtils.copyProperties when used with nested properties.
E***@nokia.com
2002-10-08 07:46:22 UTC
Permalink
Hi,

If I have this bean:

public class TestBean extends Object {
private String []roles = null;
private TestBean nested = null;

public TestBean() {
}

public String[] getRoles() {
return roles;
}

public void setRoles(String[] roles) {
this.roles = roles;
}

public String getRoles(int i) {
return roles[i];
}

public void setRoles(int i, String s) {
roles[i] = s;
}

public TestBean getNested() {
return nested;
}

public void setNested(TestBean nested) {
this.nested = nested;
}
}

And I run this code:

public static void main(String args[]) throws Exception {
TestBean testBean = new TestBean();
testBean.setNested(new TestBean());
Map values = new HashMap();
final String []roles = {"role1","role2"};
values.put("roles",roles);
values.put("nested.roles",roles);
BeanUtils.copyProperties(testBean,values);
System.out.println(testBean.getRoles().length);
System.out.println(testBean.getNested().getRoles().length);
}

I get this exception:

java.lang.IllegalArgumentException: Nested property names are not allowed
at org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:1612)
at org.apache.commons.beanutils.BeanUtils.copyProperty(BeanUtils.java:363)
at org.apache.commons.beanutils.BeanUtils.copyProperties(BeanUtils.java:241)
at TestBeanUtils.main(TestBeanUtils.java:16)

Can somebody please tell me how to copy from a Map to nested bean properties?

Many thanks,
Mike.
Craig R. McClanahan
2002-10-08 16:11:36 UTC
Permalink
Currently, the copyProperties() method of both BeanUtils and PropertyUtils
does a "shallow" copy of the source bean's properties (enforced by calling
the getSimpleProperty() method rather than the more general getProperty()
method. This was chosen to mirror the recommended semantics of the
Object.clone() method for standard object cloning.

That being said, I can sort of see the use case for supporting
nested/indexed/mapped properties as well, so I'm going to change these
methods to use getProperty() instead. This should be updated in the
20021003 nightly build (i.e. tonight) of beanutils, available at:

http://jakarta.apache.org/builds/jakarta-commons/nightly/commons-beanutils

Craig McClanahan
Date: Tue, 8 Oct 2002 10:46:22 +0300
Subject: [beanutils] BeanUtils.copyProperties when used with nested
properties.
Hi,
public class TestBean extends Object {
private String []roles = null;
private TestBean nested = null;
public TestBean() {
}
public String[] getRoles() {
return roles;
}
public void setRoles(String[] roles) {
this.roles = roles;
}
public String getRoles(int i) {
return roles[i];
}
public void setRoles(int i, String s) {
roles[i] = s;
}
public TestBean getNested() {
return nested;
}
public void setNested(TestBean nested) {
this.nested = nested;
}
}
public static void main(String args[]) throws Exception {
TestBean testBean = new TestBean();
testBean.setNested(new TestBean());
Map values = new HashMap();
final String []roles = {"role1","role2"};
values.put("roles",roles);
values.put("nested.roles",roles);
BeanUtils.copyProperties(testBean,values);
System.out.println(testBean.getRoles().length);
System.out.println(testBean.getNested().getRoles().length);
}
java.lang.IllegalArgumentException: Nested property names are not allowed
at org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:1612)
at org.apache.commons.beanutils.BeanUtils.copyProperty(BeanUtils.java:363)
at org.apache.commons.beanutils.BeanUtils.copyProperties(BeanUtils.java:241)
at TestBeanUtils.main(TestBeanUtils.java:16)
Can somebody please tell me how to copy from a Map to nested bean properties?
Many thanks,
Mike.
--
Loading...