-
1. Class c = Class.forName("myClass");
-
2. for (i=1; ....)
-
3. {
-
4. Field f = c.getField("var" + i);
-
5. f.setInt(this, i);
-
6. }
-
This is exactly what I thought of initially. The problem with this code really is that you need to know the field names, type of variables that you are setting aprior as in f.setInt(this,i)
and I have to initialize different number and type of fields.
So I thought I could create a string equivalent
List<someObjectName> myList;
myList.add(someObjectName(var1, var2, var3,....))
Let me first explain my actual problem in detail first:
I have a dynamically created query that is run on the database and we have the result set columns which can be used to initialize a known object. These objects (type not known) with constructors have to be populated dynamically. Essentially a list of these objects must be passed to some table in a jsp for display purposes. The jsp page dynamic tag libraries only accepts bean/objects.
As an awkward solution to this, we tried a genericBean object with 10 string fields in it and hence the object has a constructor with 10 fields, a maximum number of fields we might have to display in the jsp table. As different queries return different number of columns (converted to string equivalents), the remaining fields in the constructor are set as empty strings. At the jsp page, we control how many columns we want to display in the jsp from this list of objects.
Is there a solution to this problem?