|
From: Rodrigues A. <Ade...@em...> - 2005-11-08 10:18:27
|
Hi,
PROBLEM=20DESCRIPTION
Today=20BeanUtils=20in=20Spring=20offers=20the=20possibility=20to=20copy=20=
properties=20between
2=20java=20beans:
static=20void=20copyProperties(Object=20source,=20Object=20target)
static=20void=20copyProperties(Object=20source,=20Object=20target,=20Strin=
g[]=20ignoreProperties)=20
=20
As=20the=20instrospection=20does=20not=20
care=20about=20how=20the=20target=20variable=20was=20declared,=20calling=20=
such=20a=20method=20will
copy=20more=20properties=20that=20one=20wants.
To=20illustrate=20this=20point,=20let's=20consider=20the=20following=20typ=
es:
interface=20EditablePriceableItem=20{
=09public=20int=20getPrice();
=09public=20void=20setPrice(int=20val);
}
class=20PurchaseItem=20implements=20EditablePriceableItem=20{
=09//...=20attributes
=09public=20String=20getPurchaseNr()=20{=20...=20}
=09public=20void=20setPurchaseNr(String=20nr)=20{=20...=20}
=09public=20int=20getPrice()=20{=20...=20}=20;
=09public=20void=20setPrice(int=20val)=20{=20...=20};
}
class=20Project=20implements=20EditablePriceableItem=20{
=09//...=20attributes
=09public=20String=20getDescription()=20{=20...=20}
=09public=20void=20setDescription(String=20desc)=20{=20...=20}
=09public=20int=20getPrice()=20{=20...=20}=20;
=09public=20void=20setPrice(int=20val)=20{=20...=20};
}
Somewhere=20in=20a=20class,=20we=20have:
=09EditablePriceableItem=20priceableSrc;
=09EditablePriceableItem=20priceableTarget;
=09//(1)
=09//=20...
=09//=20priceableSrc=20is=20set=20by=20some=20method
=09//=20...
=09//
=09//(2)
=09//=20priceableTarget=20gets=20instanciated
=09//=20...
=09//(3)
=09//Now=20I=20want=20to=20copy=20Priceable=20properties=20(and=20only=20p=
riceable=20properties=20!)
=09//from=20priceableSrc=20to=20priceableTarget
=09BeanUtils.copyProperties(priceableSrc,priceableTarget);
Although=20variables=20are=20declared=20as=20EditablePriceableItem,=20what=
=20gets=20copied=20in=20(3)
depends=20on=20the=20concrete=20type=20of=20priceableSrc=20and=20priceable=
Target.
a)=20When=20priceableSrc=20and=20priceableTarget=20are=20instances=20of=20=
PurchaseItem,
=09price=20and=20purchaseNr=20are=20copied
b)=20When=20priceableSrc=20and=20priceableTarget=20are=20instances=20of=20=
Project,
=09price=20and=20description=20are=20copied
c)=20When=20priceableSrc=20is=20an=20instance=20of=20PurchaseItem=20and=20=
priceableTarget
=20=20=20=20is=20an=20instance=20of=20Project=20(or=20the=20opposite)=20on=
ly=20price=20gets=20copied
As=20a=20conclusion=20the=20behaviour=20in=20(3)=20is=20not=20uniform.
PROPOSITION
To=20control=20the=20scope=20of=20properties=20that=20are=20copied,=20it=20=
would=20be=20very=20useful
to=20have=20a=20method=20such=20as:
static=20void=20copyProperties(Object=20source,=20Object=20target,=20Class=
=20editableScope)
where=20editableScope=20is=20an=20interface=20or=20class=20that=20declares=
=20the=20properties.
Writing=20(3)=20as:
=09BeanUtils.copyProperties(priceableSrc,priceableTarget,
=09=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20EditablePriceableItem.class);
would=20provide=20a=20consistent=20behaviour.
What=20do=20you=20think=20of=20this?
Regards,=20Adelino.
P.S.:=20Here=20attached=20a=20simple=20implementation=20of=20such=20method=
.
________________________________________________________________________
This=20e-mail=20has=20been=20scanned=20for=20all=20known=20viruses=20by=20=
EMEA.
________________________________________________________________________ |