Discussion:
[BeanUtils] Problem using BeanUtils.copyProperties() to Complex Java Bean
lc kin
2003-12-22 04:49:32 UTC
Permalink
Dear Sir/Madam,

I am new to the BeanUtils component, I am facing
problem of copy properties from 1 complex bean to
another complex

bean by using BeanUtils.copyProperties(), Below is the
java bean's code:

import java.util.ArrayList;
public class ComplexBean implements
java.io.Serializable {
SimpleBean simple;
ArrayList col = new ArrayList();

public ComplexBean() {
super();
}

public void add(Object b) {
col.add(b);

}

public ComplexBean[] getCol() {
return (ComplexBean[])col.toArray(new
ComplexBean[col.size()]);
}

public ComplexBean getCol(int index) {
return (ComplexBean)col.get(index);
}

public void setCol(int index, ComplexBean simple) {
col.set(index, simple);
}

public SimpleBean getSimple() {
return simple;
}

public void setCol(ComplexBean[] complexBeans) {
for (int loop=0;loop < complexBeans.length; loop++)
col.add(complexBeans[loop]);
}

public void setSimple(SimpleBean bean) {
simple = bean;
}

}


import java.sql.Date;

public final class SimpleBean implements
java.io.Serializable {
private String name;
private long value;
private double price;
private Date dob;
public SimpleBean() {
super();
}

public Date getDob() {
return dob;
}

public String getName() {
return name;
}

public double getPrice() {
return price;
}

public long getValue() {
return value;
}

public void setDob(Date date) {
dob = date;
}

public void setName(String string) {
name = string;
}

public void setPrice(double d) {
price = d;
}

public void setValue(long l) {
value = l;
}

}
-----------------------------------------------------------------------------------------
I use the following code to test it,

SimpleBean simple = new SimpleBean();
simple.setDob(new java.sql.Date(new
java.util.Date().getTime()));
simple.setName("Simple Test");
simple.setPrice(12.80);
simple.setValue(12134);

ComplexBean complex = new ComplexBean();
complex.setSimple(simple);
ComplexBean complexEx = new ComplexBean();
complexEx.setSimple(simple);
complexEx.add(complex);

Map complexMapEx = BeanUtils.describe(complexEx);
ComplexBean dest_complex = new ComplexBean();
BeanUtils.copyProperties(dest_complex,
complexMapEx);

but BeanUtils.copyProperties throw
IllegalArgumentException stack trace as following
java.lang.IllegalArgumentException: argument type
mismatch
at java.lang.reflect.Method.invoke(Native Method)
at
org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:1789)
at
org.apache.commons.beanutils.BeanUtils.copyProperty(BeanUtils.java:450)
at
org.apache.commons.beanutils.BeanUtils.copyProperties(BeanUtils.java:248)
at beanutils.BeanUtil.main(BeanUtil.java:90)

May I know how to solve his problem. Is there anything
wrong in SimpleBean and ComplexBean?

Best Regards - CK Lim

__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/
Lim Chee Kin
2003-12-23 06:42:48 UTC
Permalink
Dear Sir or Madam,

I am new to the BeanUtils component, I am facing problem of
copy properties from 1 complex bean to another complex bean by using
BeanUtils.copyProperties(), Below is the java bean's code:

import java.util.ArrayList;
public class ComplexBean implements java.io.Serializable {
SimpleBean simple;
ArrayList col = new ArrayList();

public ComplexBean() {
super();
}

public void add(Object b) {
col.add(b);

}

public ComplexBean[] getCol() {
return (ComplexBean[])col.toArray(new ComplexBean
[col.size()]);
}

public ComplexBean getCol(int index) {
return (ComplexBean)col.get(index);
}

public void setCol(int index, ComplexBean simple) {
col.set(index, simple);
}

public SimpleBean getSimple() {
return simple;
}

public void setCol(ComplexBean[] complexBeans) {
for (int loop=0;loop < complexBeans.length; loop++)
col.add(complexBeans[loop]);
}

public void setSimple(SimpleBean bean) {
simple = bean;
}

}


import java.sql.Date;

public final class SimpleBean implements java.io.Serializable {
private String name;
private long value;
private double price;
private Date dob;
public SimpleBean() {
super();
}

public Date getDob() {
return dob;
}

public String getName() {
return name;
}

public double getPrice() {
return price;
}

public long getValue() {
return value;
}

public void setDob(Date date) {
dob = date;
}

public void setName(String string) {
name = string;
}

public void setPrice(double d) {
price = d;
}

public void setValue(long l) {
value = l;
}

}
----------------------------------------------------------------------
-------------------
I use the following code to test it,

SimpleBean simple = new SimpleBean();
simple.setDob(new java.sql.Date(new java.util.Date
().getTime()));
simple.setName("Simple Test");
simple.setPrice(12.80);
simple.setValue(12134);

ComplexBean complex = new ComplexBean();
complex.setSimple(simple);
ComplexBean complexEx = new ComplexBean();
complexEx.setSimple(simple);
complexEx.add(complex);

Map complexMapEx = BeanUtils.describe(complexEx);
ComplexBean dest_complex = new ComplexBean();
BeanUtils.copyProperties(dest_complex, complexMapEx);

but BeanUtils.copyProperties throw IllegalArgumentException stack
trace as following
java.lang.IllegalArgumentException: argument type mismatch
at java.lang.reflect.Method.invoke(Native Method)
at
org.apache.commons.beanutils.PropertyUtils.setSimpleProperty
(PropertyUtils.java:1789)
at org.apache.commons.beanutils.BeanUtils.copyProperty
(BeanUtils.java:450)
at org.apache.commons.beanutils.BeanUtils.copyProperties
(BeanUtils.java:248)
at beanutils.BeanUtil.main(BeanUtil.java:90)

May I know how to solve his problem. Is there anything wrong in
SimpleBean and ComplexBean?

Best Regards - CK Lim

Loading...