On Fri, 13 Dec 2002, Sri Sankaran wrote:
> Date: Fri, 13 Dec 2002 13:53:43 -0500
> From: Sri Sankaran <[EMAIL PROTECTED]>
> Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]>
> To: Struts Users Mailing List <[EMAIL PROTECTED]>
> Subject: [OT] Overloaded setters in JavaBeans
>
> It is now a well-known fact on this list that if you are trying to access a property
>using a tag you had better not have an overloaded setter for this property. In other
>words don't do
>
> private String foo;
> public String getFoo() { return foo; }
> public void setFoo(String x) { foo = x; }
> public void setFoo(int i) { foo = "" + x; }
>
> Equally well-known is the reason -- "'tis the JavaBeans specification". So, I went
>looking. Section 7.1 (Accessor methods) reads
>
> Begin quote ---
>
> Properties are always accessed via method calls on their owning object. For readable
>properties there will be a getter method to read the property value. For writable
>properties there will be a setter method to allow the property value to be updated.
>
> --- End quote
>
> Section 8.3 ("Design Patterns for Properties") reads
>
> Begin quote ---
>
> By default, we use design patterns to locate properties by looking for methods of
>the form:
>
> public <PropertyType> get<PropertyName>();
> public void set<PropertyName>(<PropertyType> a);
>
> If we discover a matching pair of "get<PropertyName>" and "set<PropertyName>"
>methods that take and return the same type, then we regard these methods as defining
>a read-write property whose name will be "<propertyName>".
> ...
>
> If we find only one of these methods, then we regard it as defining either a
>read-only or a writeonly property called "<propertyName>"
>
> --- End quote
>
> It doesn't say anything about not overloading the accessors. So, why then do we get
>the error?
>
The implementation of java.beans.Introspector (which is what BeanUtils
uses under the covers) has always interpreted the "discover a matching
pair" restriction to mean "discover a matching pair and ONLY the matching
pair; i.e. no other methods by the same name".
Note that you can actually use overloaded setters if you want to, but
you're going to have to go to a fair amount of effort. Essentially, you'd
need to provide a BeanInfo class for each of your beans that declared what
the actual Method implementations of the getter and setter are for each
property.
>
> Sri
>
Craig
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>