Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generate Java Code???

 
Meyer Florian
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
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
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 ]
 
norman richards
Author
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, nice article, Norman!
 
norman richards
Author
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use cglib
http://cglib.sourceforge.net/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic