I am having a bit of a problem and I was hoping someone would have an idea on where I can start on coming up with a solution. I need to create a JavaBean or JavaBean properties dynamically during program execution. I am retrieving column names from a database and then selecting those values from a specific table and I need to be able to store those values into a dynamic JavaBean. The amount of variables will change from time to time and I don't want to predefine them, because I will have to keep adding values manually to the bean for every new column added. I will also need to create the getters and setters dynamically as well, since I will be accessing the values. I looked at something called Reflection API already, but that will not work as it doesn't allow us to dynamically create the properties of the bean. Any ideas?
2 Answers
You could use a library like javassist to generate classes during runtime. It will allow you to add fields and methods (such as the usual getters/setters) to a class definition.