Create a generic class that will create and populate any class in the app

Hi All,

I am new to this, and I was wondering if some one could help me out. If my question is too obvious then please let me know and still guide me to a resource where I can get help.Here is what I want.

In my application at run time, I read a file, this file will determine the values for the properties of a class.

I read this file, get the class name and then read all the values that I need to set in that class. I am able to get an instance of any specifc class using Class.forName("package.ClassName");

What I am getting at is I have a generic class, to which I pass the String class name and the values[], and in this generic class, it will create an instance and populate fields using setters.

I want to do this for many classes not writing a single loading class for each class.

If I do not make any sense, even then please let me know so that I could try explaining further..:-)

A help is greatly appreciated

[969 byte] By [Khalid.Alia] at [2007-11-27 1:48:48]
# 1
No, your question is not obvious at all.Your description of your requirements is perfectly clear, and certainly you could do that. I just don't see a question in there.
DrClapa at 2007-7-12 1:13:18 > top of Java-index,Core,Core APIs...
# 2

Hi DrClap,

Thanks allot (I have to say thank you to you for your help over the years, I can remember at least 6 yrs..*_*)

Ok here is my question.

To create a class to which I provide classname and the values that I want to set for fields in that class.

Then this class uses some magic and updates all the fields according to new values provided, and returns an instance of that class so that I can use it later on.

Khalid.Alia at 2007-7-12 1:13:18 > top of Java-index,Core,Core APIs...
# 3
No, I still don't see a question there.Surely you aren't asking how to do that? You posted the question in the Reflection forum so that suggests you already know you need to use reflection.
DrClapa at 2007-7-12 1:13:18 > top of Java-index,Core,Core APIs...
# 4
take a look at how, for instance, the Spring framework achieves exactly this
georgemca at 2007-7-12 1:13:18 > top of Java-index,Core,Core APIs...
# 5
Thanks to both of you, I figured it out. I just was confused because of the simplicity of the methods such as Class.forName("className").newInstance();and getMethods() and invoke(instance,params).It is very simple as it turned out to be.Thanks anyways.
Khalid.Alia at 2007-7-12 1:13:18 > top of Java-index,Core,Core APIs...