commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Niall Pemberton <niall.pember...@gmail.com>
Subject Re: [beanutils] beanutils library cannot invoke the correct accessor
Date Thu, 10 Jun 2010 14:14:31 GMT
On Sun, Jun 6, 2010 at 3:02 AM, Dimitris Tsitses <4.biz.etc@gmail.com> wrote:
> Hi all, I'm trying to do something really simple but I can't seem to figure it out, I
hope someone can help.
>
> I have a simple bean:
>
>
> public class MyBean {
>        private ClassTypeA myProperty;
>
>        public void setMyProperty(ClassTypeA anObj) {
>                this.myProperty = anObj;
>        }
>        public void setMyProperty(ClassTypeB anObj) {
>                this.myProperty = Converter.convertBtoA(anObj);
>        }
>
> }

The way to do this in BeanUtils is to get rid of your overloaded
setter for ClassTypeB and register a converter with Beanutils.
Something like...

    ClassTypeAConverter converter = new ClassTypeAConverter();
    ConvertUtils.register(converter,  ClassTypeA.class);

That way when you use BeanUtils.setProperty() it will call the
converter to convert the ClassTypeB to ClassTypeA.

Niall

> If I invoke the setter by passing an instance of ClassTypeA, it works without a problem:
> BeanUtils.setProperty(myBeanInst, "myProperty", instanceOfClassTypeA);
>
>
> However if I invoke the setter by passing an instance of ClassTypeB, I get an exception
and the setter is never actually called:
> BeanUtils.setProperty(myBeanInst, "myProperty", instanceOfClassTypeB);
> java.lang.IllegalArgumentException: Cannot invoke.. la la la - argument type mismatch
-
>
> I can't believe it is not possible to do that, I'm sure I'm just missing something. Any
help will be greatly appreciated.
>
> Many thanks
> Dimitris

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message