views:

55

answers:

1

Hi,

I'm using the Apache BeanUtils setProperty method to import data from an Excel file in a generic manner. I provide the parser a list of "colum name" and "property name" pairs and then the parser sets the properties with setProperty( ..., "property name", ...) for the Excel fields that match with "column name". It's working nice but now I'm having a problem, I have a property that is setted with an addXXX() method and I cannot change that class. How can I set this property without changing this class or the parser? Is it possible? Can I make something like a wrapper with DynaClass, PropertyUtils or MethodUtils so when setting that property it calls the addXX method?

Thanks!

A: 

You can write a decorator bean class that will wrap around your class and provide a setXXX() method delegating to addXXX() of the wrapped bean. You can even do that via dynamic proxy with help of cglib or any other bytecode instrumentation library.

If you'd rather go the DynaClass way, take a look at WrapDynaClass as a starting point. You'll have to extend it to provide custom PropertyDescriptor for your method. I wouldn't do this, though, for two reasons - first, addXXX() semantics is likely quite different from setXXX(); and secondly, the whole DynaClass/DynaBean internal API is a bit messy whereas writing your own decorator class is very straightforward.

ChssPly76
Yes, why using WrapDynaClass when a simple decorator solves the problem. Thanks!
fmaste
I made the docerator but now I'm getting a java.lang.InstantiationException when creating the instance like I did before.Somebody knows why?
fmaste
`InstantiationException` is thrown when class is either abstract or an interface or it does not have a public (or otherwise accessible to caller) no-argument constructor. Your problem most likely is the latter.
ChssPly76
My problem was that the class was an inner class and it needs to be static!
fmaste