This content has been marked as final. Show 8 replies
How to create a constructor dynamicallyYou can't. The class already has its constructors. Or not.
I want to create a constructor dynamicallyWhy?
If it's a bean object it must have (a) a no-args constructor and (b) a set method for every writable property.
I suggest you have misunderstood something somewhere.
thanks for your reply.
One way I have implemented a solution for this issue is creating a 'TransferEntity' that contains the generic values (in an array usually). Then you can wrapper this class with a "friendlier" one that has Get/Set methods. This allows you to always get the values you need (because the TransferEntity is inside) if they are variable. If not variable, you can simply use the Get/Set (which would reach inside the TransferEntity). Depending on how you're using the bean, you can simply put all the Get/Set methods inside and either return null, throw a NoSuchFieldException, etc.
Hope this helps...
Edited by: dfortae on Apr 29, 2010 2:44 AM
Presumably neither the original question nor the last suggested solution was based on the idea that someone wants to avoid typing or testing.
Why a constructor, specifically? Why not plain old OR/M to properties?
Following @georgemc's suggestion I would just create a Map<String, Object> for each row. You can obtain the column names from the meta data. This would allow you to create a dataset from any random query. (I wouldn't use a Properties class unless you want to assume String values)
Something I have done in the past is used the MetaData to generate a class on the fly using ASM (caching the classes generated). However, unless you have serious efficiency issues with using a Map, I would suggest its not worth it.
Cheapside-Poultry wrote:I never suggested anything of the sort
Following @georgemc's suggestion I would just create a Map<String, Object> for each row.
Neither did anybody else.