You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by "YAWN,MIKE (HP-PaloAlto,ex1)" <mi...@hp.com> on 2002/12/11 20:25:57 UTC
Digester selecting wrong property setter method
Digester gurus,
I have certain properties within a bean which are represented as numeric
values. However, these numeric values are an internal implementation detail
I don't really want to expose to users, so I'd rather have the bean
configured using meaningful text strings.
For example, consider a field indicating how to contact someone:
private int preferredContact;
public final static int PHONE = 1;
public final static int EMAIL = 2;
public int getPreferredContact() { return preferredContact; }
public void setPreferredContact(int pc) { preferredContact = pc; }
Now, I would have expected that digester could not have set this field even
if I wanted to expose the numeric values, simply because XML contains text,
not integers. So I altered this class to work with Digester by adding the
following method:
public void setPreferredContact(String pc) {
if (pc.equals("phone"))
preferredContact = PHONE;
else if (pc.equals("email"))
preferredContact = EMAIL;
else
// log an error
}
And XML input file contains (vastly simplified):
<personInfo preferredContact="email" />
Now, when I run digester, my nice String-based setPreferredContact method is
ignored in favor of the one taking an int. The int value comes in as zero,
not a legal value for the field and thus causing problems later.
Given that the parsed XML is a string value that doesn't even resemble an
int, and a setter method taking a String is provided, I would really expect
Digester to use the String variant, rather than go through ConvertUtils and
come up with a (seemingly random) integer value. How can I force Digester
to select the interface I created specifically for its use?
Thanks,
Mike Yawn
Re: Digester selecting wrong property setter method
Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
(as has been pointed out by the previous posted, the reasons why the
standard digester rules don't work is that java beans are not supposed to
have overloaded setters.)
a more elegant solution to this problem might be to create a custom Rule
which knows about your enums. if string-to-int enums are a common enough
pattern, then maybe someone will volunteer to contribute a general
implementation.
(i might even do so myself if there's enough demand from users...)
- robert
On Wednesday, December 11, 2002, at 07:25 PM, YAWN,MIKE (HP-PaloAlto,ex1)
wrote:
> Digester gurus,
>
> I have certain properties within a bean which are represented as numeric
> values. However, these numeric values are an internal implementation
> detail
> I don't really want to expose to users, so I'd rather have the bean
> configured using meaningful text strings.
>
> For example, consider a field indicating how to contact someone:
>
> private int preferredContact;
> public final static int PHONE = 1;
> public final static int EMAIL = 2;
> public int getPreferredContact() { return preferredContact; }
> public void setPreferredContact(int pc) { preferredContact = pc; }
>
> Now, I would have expected that digester could not have set this field
> even
> if I wanted to expose the numeric values, simply because XML contains
> text,
> not integers. So I altered this class to work with Digester by adding the
> following method:
>
> public void setPreferredContact(String pc) {
> if (pc.equals("phone"))
> preferredContact = PHONE;
> else if (pc.equals("email"))
> preferredContact = EMAIL;
> else
> // log an error
> }
>
> And XML input file contains (vastly simplified):
> <personInfo preferredContact="email" />
>
> Now, when I run digester, my nice String-based setPreferredContact method
> is
> ignored in favor of the one taking an int. The int value comes in as
> zero,
> not a legal value for the field and thus causing problems later.
>
> Given that the parsed XML is a string value that doesn't even resemble an
> int, and a setter method taking a String is provided, I would really
> expect
> Digester to use the String variant, rather than go through ConvertUtils
> and
> come up with a (seemingly random) integer value. How can I force Digester
> to select the interface I created specifically for its use?
>
> Thanks,
> Mike Yawn
>
> --
> To unsubscribe, e-mail: <mailto:commons-user-unsubscribe@jakarta.apache.
> org>
> For additional commands, e-mail: <mailto:commons-user-help@jakarta.apache.
> org>
>