commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Carman <ja...@carmanconsulting.com>
Subject Re: [beanutils] beanutils library cannot invoke the correct accessor
Date Mon, 07 Jun 2010 21:37:09 GMT
Create a helper method of your own somewhere?

On Mon, Jun 7, 2010 at 5:28 PM, Dimitris Tsitses <4.biz.etc@gmail.com> wrote:
> Sorry, it appears that I didn't explain myself properly. What I mean is that BeanUtils.setProperty()
expects to receive the property name (i.e., "customer"), whereas MethodUtils.invokeMethod()
expects to receive the method name (i.e., "setCustomer"). So I'm wondering what is the best
way to resolve a property name into a setter method name. At the moment I use the following:
>
> PropertyDescriptor aPropDescriptor  = PropertyUtils.getPropertyDescriptor(myBeanInstance,
propName);
> String setterMethodName = PropertyUtils.getWriteMethod( aPropDescriptor).getName();
>
> So that gives me the setter method name which I can pass to MethodUtils.invokeMethod().
However, PropertyUtils.getPropertyDescriptor() returns null for the bean example below. That's
because a setter that accepts an argument of type ClassTypeA is not defined in that bean.
I suppose that's not a big deal, I was just wondering though if there is an alternative way
to resolve a property name to the setter method name of that property, merely on a string
processing basis instead of reflection. Of course doing the string processing manually is
2 lines of code, but was curious as to whether there is a standard utility method in beanutils
library.
>
> Thanks
> Dimitris
>
>
> public class MyBean {
>       private ClassTypeA myProperty;
>
>       public void setMyProperty(ClassTypeB anObj) {
>               this.myProperty = Converter.convertBtoA(anObj);
>       }
>
> }
>
> On 2010-06-07, at 2:05 PM, James Carman wrote:
>
>> On Mon, Jun 7, 2010 at 1:05 PM, Dimitris Tsitses <4.biz.etc@gmail.com> wrote:
>>> Hi James,
>>>
>>> actually replacing:
>>> BeanUtils.setProperty(myBeanInst, myProperty, instanceOfClassTypeX);
>>>
>>> with:
>>> MethodUtils.invokeMethod(myBeanInst, myPropertySetter, instanceOfClassTypeX);
>>>
>>> works like a dream, thanks for pointing out MethodUtils. I'm currently converting
'myProperty' to 'myPropertySetter' manually (i.e., "propertyName" -> "setPropertyName").
Is there a convenience method that I could use for the conversion?
>>>
>>
>> Any modern IDE should have a rename refactoring feature.  I'd use that
>> to rename properties/methods.  That also will change the places where
>> it is used.  Very helpful.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

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


Mime
View raw message