Results 1 to 6 of 6
- 07-01-2011, 07:14 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
generate class file during runtime?
Hi, I wrote some classes that one can use to display functions, polars, parametrics, data, etc...I represent each function with a class that implements a Function interface.
using anonymous classes and eclipse, it's pretty easy to generate graphs by editing a driver class in the editor window. But now I want to create a GUI to make it more user friendly...the way I was thinking of allowing user-defined functions was to have a text box where the user types the return statement for the at method in the Function interface, and then having java compile it into a new function to load. The user would have to use java's syntax, but it seems a helluva lot easier than writing a parser. My only problem is that, from what I understand, I can't use the compiler unless the user has the JDK installed, not just the JVM, like most non-programmers do. My question is if anyone knows how to take simple java code (for example, a return statement like "return x*x" to generate a function that represents y=x^2) generated during run time and load it as if it was written before, by the programmer. I can't thank you guys enough!Java Code:public interface Function{ //returns the value of the function at this input (returns f(x)) public double at(double x); }
- 07-01-2011, 07:34 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Since Java 1.5 the JRE comes bundled with a Javascript engine (written in Java); if you're willing to use Javascript syntax instead (it's very similar to Java's syntax) you can use a Javscript engine. Here's a quick and dirty example:
kind regards,Java Code:import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class ScriptDemo { public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); try { String expression = "function add(x, y) { return x+y; }"; engine.eval(expression); engine.put("a", 41); // engine.put("b", 1); Invocable invocable= (Invocable)engine; double d= (Double)engine.eval("add(a, b)"); System.out.println(d); System.out.println("add(1, 41)= "+invocable.invokeFunction("add", 1, 41)); System.out.println("add(a, b)= "+invocable.invokeFunction("add", "a", "b")); System.out.println("add(a, b)= "+engine.eval("add(a, b)")); } catch(ScriptException se) { se.printStackTrace(); } catch (NoSuchMethodException nsme) { nsme.printStackTrace(); } } }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-01-2011, 08:02 AM #3
Another, Windows-only way is to write out a VBS script to a file, execute it, and collect the return value.
A couple of examples of this approach:
Printing excel document without opening
Find windows special folders (Java in General forum at JavaRanch)
db
- 07-01-2011, 08:18 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
After looking into the JavaScript engine, I think I'm going to go with that one, mainly because I understand the procedure better lol. Do you guys think the fact that the functions will be scripted as opposed to java-coded will reduce the speed of execution? the function is evaluated once per horizontal pixel, so 500 times on a 500x500 pane. Thanks so much!
- 07-01-2011, 08:44 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Of course a purely interpreted language is slower than compiled Java but read the API documentation for the ScriptEngine framework; my example also shows it: an Invocable object only needs to compile the script once and can be run many times; this speeds up the entire thing. 500 expression evaluations (as in your example) doesn't take much time. Another option is BeanShell, it's an interpreted version of a form of Java and it can do great stuff with ordinary compiled Java too.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-02-2011, 06:21 AM #6
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
write a program to generate a series using runtime parameters
By satheeshtech in forum New To JavaReplies: 2Last Post: 02-02-2010, 03:01 PM -
Generate a Class file
By Daniel in forum New To JavaReplies: 6Last Post: 04-20-2009, 05:37 AM -
Can we generate an array for class object?
By gaurav2211 in forum Advanced JavaReplies: 4Last Post: 03-21-2009, 04:20 AM -
Example of RunTime class
By Java Tip in forum Java TipReplies: 0Last Post: 01-04-2008, 09:32 AM -
Exception Failed to Generate Wrapper Class on WebLogic
By christina in forum New To JavaReplies: 1Last Post: 08-07-2007, 02:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks