aspose file tools*
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
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: 42263
    
  64
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: 18643
    
    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: 19718
    
  20

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