aspose file tools*
The moose likes Java in General and the fly likes creating simple java objects dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "creating simple java objects dynamically" Watch "creating simple java objects dynamically" New topic
Author

creating simple java objects dynamically

Jochen Szostek
Ranch Hand

Joined: Mar 23, 2008
Posts: 42

Hi all,

Is it possible to generate an object dynamically within java code (at runtime)?

Something like:

somekindofjavaobject user;

user.addVar(Public String userName);
user.addVar(Public String password);
user.userName("Jochen");
user.password("xxx");
user.createClass();

Thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Not like that, no.

After the declaration of the object you have a null reference, so the app would crash with a NullPointerException.

That is, if the compiler would let Public instead of public or a method call with three tokens past its Argus-like eye in the first place.

You might manage to get a method using the Class.forName method.

Myclass myObject = Class.forName("mypackage.MyClass").getInstance();
myObject.setName("Jochen");

etc.

I think that might require MyClass have a no-arguments constructor.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Well actually... yes, it is quite possible. Check out, for example, ASM. This library lets you build a class file from code; you can then load that class and use it right in the application.


[Jess in Action][AskingGoodQuestions]
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 988
    
    1
You might consider using a Hashtable (from the java.util package) or similar object.

Jochen Szostek
Ranch Hand

Joined: Mar 23, 2008
Posts: 42

Thanks a lot for the fast replies!!

Gonna try the reflections and ASM approach first thing tomorrow.

The hashtable solution isn't really going to be useful I guess because want to use the (in runtime) generated object in hibernate so I can store it in my DB.
(hope this will be possible, though I guess I also need to adjust my hibernate mapping settings in real time somehow too)

Greetings
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
If you get the reflection to work, tell us, then I can see where I was mistaken.

Sorry
Jochen Szostek
Ranch Hand

Joined: Mar 23, 2008
Posts: 42

Well, if I get it right then "Class.forName()" can be used for dynamic class loading. But then (for what I'm trying to do) I'd have to create and compile my dynamic class somehow during runtime...and I don't really see how I can accomplish that...

I could use ASM to generate the bytecode for the class somehow but I browsed a bit through the docs on their site and it doesn't seem that easy to me. I think I will need quite some time to get it.


To tell you the whole story: The kind of system I would like to create is one where you can generate a form (with some fields) which then would automatically be mapped to a java object with the same name as the form name and the fields and their values as class variables. Thereafter I would like to pass it to hibernate to store it in my DB. (so then I would need to generate hibernate's mapping files too along the way)

I've already been working on such a of system in PHP and managed to get it more or less, but now I'm trying to create the same thing in Java.

Maybe I could use the struts ModelDriven interface for generating objects from html forms as someone advised me in this thread:
http://www.coderanch.com/t/58230/Struts/implement-pojo-web-form-mapping

But I'm not sure if this workflow would be suited for the kind of system I'm trying to build. I'm still exploring all my possibilities...so any comments are welcome.

Thanks!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
I use Javassist for the same purpose, and can recommend it. It's easy to get started with, and has very helpful tutorial to get up and running.

As an aside, it's possible to drive the Java compiler from inside Java code, and quite easy. But using one of the above-mentioned libraries is probably easier.


Ping & DNS - updated with new look and Ping home screen widget
 
 
subject: creating simple java objects dynamically
 
Similar Threads
dynamic java
Creating xml structure from java bean
calling a metohd name usingn string
Dynamic type casting for Java Objects
finding duplicate characters in a String