You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Th...@partner.bmw.ch on 2008/09/23 09:46:19 UTC
PropertyUtils.copyProperties() and inherited classes
Hi @all,
as it seems, the PropertyUtils.copyProperties() as well as
BeanUtils.copyProperties() only do a copy on "class-level".
As I would like to reuse my copy-methods for an inherited
class-structure I would like to ask if there is also a chance to do a
"full"-object copy for inherited classes for all properties.
==========================================
Example:
public class Class1 {
private String attribute1;
}
public class Class2 extends Class1 {
private String attribute2;
}
public class Business {
public static copy(Class1 source, Class1 dest) {
PropertyUtils.copyProperties(source, dest)
}
}
==========================================
If I call Business.copy() with 2 instances of Class1, attribute1 is
perfectly copied. But if I call the same method with 2 instances of
Class2, only attribute1 is copied although I would also like to copy
attribute2 as well. Any chance to do that?
Furthermore I realized that enumerations are not copied, too. Is there a
chance to copy the bean-instance including enumerations?
Thanks for your help
Thomas
Re: PropertyUtils.copyProperties() and inherited classes
Posted by Hanson Char <ha...@gmail.com>.
Hi Niall,
You can do the copying (with BeanPopulator) or replication (with
BeanReplicator) from beanlib which supports jdk 1.5+.
http://beanlib.sourceforge.net/
Sorry for the shameless plug.
Hanson
On Tue, Sep 23, 2008 at 12:46 AM, <Th...@partner.bmw.ch> wrote:
> Hi @all,
>
> as it seems, the PropertyUtils.copyProperties() as well as
> BeanUtils.copyProperties() only do a copy on "class-level".
> As I would like to reuse my copy-methods for an inherited
> class-structure I would like to ask if there is also a chance to do a
> "full"-object copy for inherited classes for all properties.
>
> ==========================================
> Example:
>
> public class Class1 {
> private String attribute1;
> }
>
> public class Class2 extends Class1 {
> private String attribute2;
> }
>
> public class Business {
>
> public static copy(Class1 source, Class1 dest) {
> PropertyUtils.copyProperties(source, dest)
> }
> }
> ==========================================
>
> If I call Business.copy() with 2 instances of Class1, attribute1 is
> perfectly copied. But if I call the same method with 2 instances of
> Class2, only attribute1 is copied although I would also like to copy
> attribute2 as well. Any chance to do that?
> Furthermore I realized that enumerations are not copied, too. Is there a
> chance to copy the bean-instance including enumerations?
>
> Thanks for your help
>
> Thomas
>
>
>
Re: PropertyUtils.copyProperties() and inherited classes
Posted by Niall Pemberton <ni...@gmail.com>.
On Tue, Sep 23, 2008 at 8:46 AM, <Th...@partner.bmw.ch> wrote:
> Hi @all,
>
> as it seems, the PropertyUtils.copyProperties() as well as
> BeanUtils.copyProperties() only do a copy on "class-level".
> As I would like to reuse my copy-methods for an inherited
> class-structure I would like to ask if there is also a chance to do a
> "full"-object copy for inherited classes for all properties.
>
> ==========================================
> Example:
>
> public class Class1 {
> private String attribute1;
> }
>
> public class Class2 extends Class1 {
> private String attribute2;
> }
>
> public class Business {
>
> public static copy(Class1 source, Class1 dest) {
> PropertyUtils.copyProperties(source, dest)
> }
> }
> ==========================================
>
> If I call Business.copy() with 2 instances of Class1, attribute1 is
> perfectly copied. But if I call the same method with 2 instances of
> Class2, only attribute1 is copied although I would also like to copy
> attribute2 as well. Any chance to do that?
It does do this - but looks like you have the parameters the wrong way
round, try
PropertyUtils.copyProperties(dest, source)
> Furthermore I realized that enumerations are not copied, too. Is there a
> chance to copy the bean-instance including enumerations?
BeanUtils doesn't support JDK 1.5 features and it doesn't look very
likely it will do so since its pretty much in maintenance mode.
Niall
> Thanks for your help
>
> Thomas
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org