This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
(Moderators: fry, REAS)
   dynamically generated code
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: dynamically generated code  (Read 603 times)

dynamically generated code
« on: Feb 23rd, 2005, 7:45pm »

Is it possible from within a Processing program
to generate (Java) code on the fly and then have this
code execute? Something like this would be easy in
a language like Lisp, but with Java, I cannot locate
a good discussion. The concept of "reflective code"
is similar but not exactly the above.

Re: dynamically generated code
« Reply #1 on: Feb 23rd, 2005, 8:14pm »

[ i probably should leave this question for the java-pros (tom where are you?) ... ]
in a way that's what processing is already doing: it is itself a java-application which is compiling java-code (generated by the preprocessor from the .pde file) via jikes into .class files. so yes, it's possible.
if you don't want to use jikes (which comes with you processing) installation, then you might want to look up "com.sun.tools.javac" at sun. here is a tutorial to get you started:
but remember you sure have to have some kind of write access to a disk to write the .classes to ... so i guess building an applet might be difficult.
btw. sounds interesting, what are you planning to do?

Re: dynamically generated code
« Reply #2 on: Feb 23rd, 2005, 9:31pm »

Thanks for the suggestion regarding the URL.
The simplest use of the technique for us
 would be to allow people to
sketch and then generate code from the sketch (as
done in a GUI built on top of simulation code). This
would allow us to use Processing's graphics to  
construct more complete GUIs, with ability to draw
icons, arrows, etc (and then have code generated).
Jikes would be fine - I've never really used it.
presumably, there is a Java function that does an
"eval" (whatever Processing uses, for example,
to achieve this effect).

Re: dynamically generated code
« Reply #3 on: Feb 23rd, 2005, 10:41pm »

i don't really get what you are trying to do ...
anyway, as i wrote: processing has a preprocessor (no eval, rather string replace) that runs thru the .pde text and changes some parts to make it valid java. that java-text is then saved to a secret place ( ) from where it is compiled with the jikes that comes with the install.

Re: dynamically generated code
« Reply #4 on: Feb 24th, 2005, 12:20am »

If you're referring to actually creating a program from within a program you could possibly try and make your program into a programmer.. ie.. use the BufferedWriter class to write a string to a .java file and then make a system call from within your program:
System("javac replicant.java");
Process p = Runtime.getRuntime().exec("java replicant");
Never tried doing this from within a Processing applet myself.. its not your stock programming technique to compile and run java from java..  
another option would be to try and dynamically create objects instead(polymorphism).. and that way your new 'child'program can still access variables of the 'parent'.
Just a thought..
« Last Edit: Feb 24th, 2005, 12:21am by moving_ninja »  


Re: dynamically generated code
« Reply #5 on: Feb 24th, 2005, 9:08am »

i guess metaphorz is looking for "java interpreters", e.g. beanshell and a respectable list of other projects
not as simple as javascript.eval() but much more powerful...

Ariel Malka | www.chronotext.org
Pages: 1 

« Previous topic | Next topic »