• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating simple java objects dynamically

 
Jochen Szostek
Ranch Hand
Posts: 42
Android Google Web Toolkit Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49364
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ryan McGuire
Ranch Hand
Posts: 1068
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might consider using a Hashtable (from the java.util package) or similar object.

 
Jochen Szostek
Ranch Hand
Posts: 42
Android Google Web Toolkit Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49364
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you get the reflection to work, tell us, then I can see where I was mistaken.

Sorry
 
Jochen Szostek
Ranch Hand
Posts: 42
Android Google Web Toolkit Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic