I looked all over for an example. I read the docs and even tried to
create an example, but I can not get this to work. I created a BeanInfo
and defined my MethodDescriptors, here is the code:
public MethodDescriptor[] getMethodDescriptors()
{
MethodDescriptor rv[] = null;
try
{
Method setEmailString = beanClass.getMethod("setEmail", new Class[]
{String.class} );
Method setEmailForm = beanClass.getMethod("setEmail", new Class[]
{EmailForm.class} );
Method getEmail = beanClass.getMethod("getEmail", new Class[] {});
Method setName = beanClass.getMethod("setName", new Class[]
{String.class});
Method getName = beanClass.getMethod("getName", new Class[] {});
Method[] methods = new Method[] { setEmailString, setEmailForm,
getEmail, setName, getName };
rv = new MethodDescriptor[methods.length];
for(int i=0;i<methods.length; i++)
{
rv[i] = new MethodDescriptor(methods[i]);
}
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
return rv;
}
This is all that is in my BeanInfo. The Form only has two properties:
String name, EmailForm email. The email property has two setters, one
for EmailForm and one for String. I still get the
IllegalArgumentException: argument type mismatch. Would someone please
fill in the blanks? What am I missing here?
Carl
-----Original Message-----
From: Craig R. McClanahan [mailto:[EMAIL PROTECTED]]
Sent: Saturday, December 14, 2002 2:44 PM
To: Struts Users Mailing List
Subject: Re: [OT] Overloaded setters in JavaBeans
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]>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>