Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Generate Java Code??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generate Java Code???" Watch "Generate Java Code???" New topic
Author

Generate Java Code???

Meyer Florian
Ranch Hand

Joined: Oct 20, 2003
Posts: 62
Hey there!
Does anyone know how to "generate java code" during runtime?
Like this:
String exerciseNr = "3_1_2";
and then I want to run the following:
("exerciseDialog" + exerciseNr).setVisivle(true);
Should run this: excerciseDialog3_1_2.setVisivle(true);
Hope you understad my question...
Greetz from Praiser
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
Here's what you needs to do:
1) Generate the code and write it out to a file.
2) Using Runtime.exec() invoke the compiler (javac) and compile the Java source code into a class file.
3) Using a class loader (I think you'll have to write your own, although it's not that hard), you then need to explicitly load the class during runtime.
4) Once the class is loaded you can access methods and fields of the class, as well as create instances.
Chances are, this is easier said than done. Are you sure you need to do this?
--Mark
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
In your example, you're building the name of a class. Does the class already exist? If so, you can use:

Note that you had to know the type of the class (SomeType) to do this. Since you apparently have a bunch of classes with different exerciseNr we don't know the which one we'll get at coding time. How to resolve this? The SomeType we put here is a superclass or interface that all the exercises extend or implement. Methods we call like setVisible() must be defined on SomeType and can be overridden by individual exercises.
This technique requires the class to have a no-argument constructor, usually not a big deal. If you want to use more complex constructors you'll have to dig deeper into reflection.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I don't think he's building the name of a class, but the name of a variable. He's got more than one variable named exerciseDialog3_1_1, exerciseDialog3_1_2, etc, and he wants to make one of them visible, but which one he's not sure until runtime.
Mark's answer is correct if you really need to generate code, but if all you need to do is choose which variable to operate on, then selecting among hardcoded variable names by generating code is a crazy way to do it!
Instead, why not put all your exerciseDialogs into a Collection (for example, a java.util.HashMap) and then look them up by name as needed:

[ October 21, 2003: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
norman richards
Author
Ranch Hand

Joined: Jul 21, 2003
Posts: 367
Ernest is right here. You are making a simple thing complicated. I'd recommend his approach, or perhaps use reflection if you are operating on fields. (I hope you aren't talking about local variables, because local variable names are lost at compile time and don't make it into the class file or the java runtime - aside from debugging info)
You definitely don't want code generation. If you are just deadly curious how to do dynamic code generation, I wrote an article on the topic for Java Developers Journal a couple years ago:
http://www.sys-con.com/java/article.cfm?id=1307
You don't want to do anything so complicated for this, but I thought I'd throw it out just in case someone out there has an interest in the topic.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hey, nice article, Norman!
norman richards
Author
Ranch Hand

Joined: Jul 21, 2003
Posts: 367
It was a fun article to write. It is a bit dated now, but it is still interesting. If you liked that one, you might also enjoy my "Hello World! in 70 bytes" article. It's kind of an introduction to class files and bytecode
from the completely irrelevant, but highly enjoyable, perspective of trying to produce the smallest Java "Hello World!" program...
http://www.sys-con.com/java/article.cfm?id=1533
Sean Sullivan
Ranch Hand

Joined: Sep 09, 2001
Posts: 427
Use cglib
http://cglib.sourceforge.net/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generate Java Code???