commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "YAWN,MIKE (HP-PaloAlto,ex1)" <>
Subject Digester selecting wrong property setter method
Date Wed, 11 Dec 2002 19:25:57 GMT
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;
			// 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?

Mike Yawn

View raw message