commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Niall Pemberton" <niall.pember...@blueyonder.co.uk>
Subject Re: BeanUtils: proposition for "controlled" copy of bean properties
Date Tue, 29 Nov 2005 15:59:04 GMT
Rodrigues,

This rings a bell, but I can't find an open bugzilla ticket for this. Better
if you attach your patch to a bugzilla ticket (I couldn't open it).

Niall

----- Original Message ----- 
From: "Rodrigues Adelino" <Adelino.Rodrigues@emea.eu.int>
Sent: Tuesday, November 29, 2005 9:47 AM


Hi,

PROBLEM DESCRIPTION

Today Commons BeanUtils offers the possibility to copy properties between
2 java beans:

static void copyProperties(Object dest, Object orig)

As the instrospection does not
care about how the target variable was declared, calling such a method will
copy more properties that one wants.

To illustrate this point, let's consider the following types:

interface EditablePriceableItem {
public int getPrice();
public void setPrice(int val);
}


class PurchaseItem implements EditablePriceableItem {
//... attributes
public String getPurchaseNr() { ... }
public void setPurchaseNr(String nr) { ... }
public int getPrice() { ... } ;
public void setPrice(int val) { ... };
}

class Project implements EditablePriceableItem {
//... attributes
public String getDescription() { ... }
public void setDescription(String desc) { ... }
public int getPrice() { ... } ;
public void setPrice(int val) { ... };
}

Somewhere in a class, we have:

EditablePriceableItem priceableSrc;
EditablePriceableItem priceableTarget;

//(1)
// ...
// priceableSrc is set by some method
// ...
//
//(2)
// priceableTarget gets instanciated
// ...

//(3)
//Now I want to copy Priceable properties (and only priceable properties !)
//from priceableSrc to priceableTarget
BeanUtils.copyProperties(priceableTarget,priceableSrc);

Although variables are declared as EditablePriceableItem, what gets copied
in (3)
depends on the concrete type of priceableSrc and priceableTarget.

a) When priceableSrc and priceableTarget are instances of PurchaseItem,
price and purchaseNr are copied
b) When priceableSrc and priceableTarget are instances of Project,
price and description are copied
c) When priceableSrc is an instance of PurchaseItem and priceableTarget
    is an instance of Project (or the opposite) only price gets copied

As a conclusion the behaviour in (3) is not uniform.


PROPOSITION
To control the scope of properties that are copied, it would be very useful
to have a method such as:

static void copyProperties(Object target, Object source, Class
editableScope)

where editableScope is an interface or class that declares the properties.
Writing (3) as:

BeanUtils.copyProperties(priceableTarget,priceableSrc,
                         EditablePriceableItem.class);

would provide a consistent behaviour.

What do you think of this?



Regards, Adelino.

P.S.: Here attached a simple implementation of such method.

EditablePriceableItem priceableSrc;
EditablePriceableItem priceableTarget;

//(1)
// ...
// priceableSrc is set by some method
// ...
//
//(2)
// priceableTarget gets instanciated
// ...

//(3)
//Now I want to copy Priceable properties (and only priceable properties !)
//from priceableSrc to priceableTarget
BeanUtils.copyProperties(priceableTarget,priceableSrc);

Although variables are declared as EditablePriceableItem, what gets copied
in (3)
depends on the concrete type of priceableSrc and priceableTarget.

a) When priceableSrc and priceableTarget are instances of PurchaseItem,
price and purchaseNr are copied
b) When priceableSrc and priceableTarget are instances of Project,
price and description are copied
c) When priceableSrc is an instance of PurchaseItem and priceableTarget
    is an instance of Project (or the opposite) only price gets copied

As a conclusion the behaviour in (3) is not uniform.


PROPOSITION
To control the scope of properties that are copied, it would be very useful
to have a method such as:

static void copyProperties(Object target, Object source, Class
editableScope)

where editableScope is an interface or class that declares the properties.
Writing (3) as:

BeanUtils.copyProperties(priceableTarget,priceableSrc,
                         EditablePriceableItem.class);

would provide a consistent behaviour.

What do you think of this?

Regards, Adelino.


P.S.: Here attached a simple implementation of such method.

 <<DataCopier.java>>



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message