This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes Creating a Class on-the-fly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Creating a Class on-the-fly" Watch "Creating a Class on-the-fly" New topic
Author

Creating a Class on-the-fly

Dinesh Sundrani
Ranch Hand

Joined: Mar 21, 2006
Posts: 78
Hello,

I have a text file called class_content_decider.config and I would like to develop a simple Java Application (TestDynamicClass.java) that would read this 1-liner config file and create a DYNAMIC class (object on the fly).

Say for example - class_content_decider.config
firstanme, lastname, age, dob, company, salary

And the dynamic Class/Object should look like

Class temp1_21
{
String firstanme;
String lastname;
String age;
String dob;
String company;
String salary;
}

Would Reflection help in such a (specific) case?

Please suggest!

Regards,
DS - [SCJP, SCWCD, SCDJWS]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41165
    
  45
Reflection works on existing classes, but a library like Javassist can easily create classes on the fly.


Ping & DNS - my free Android networking tools app
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

However you would certainly need to use reflection to create and access instances of that class after you had created it.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Unless it implements an interface.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

A simple Map<String,String> looks easier to accomplish.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating a Class on-the-fly
 
Similar Threads
code to read the xml and also overide the value of certain attributes in xml at runtime itself
Using Named Queries
setter/getter in bean - overengineering?
Does this code will be valid for Composition?
Reading from a file