0

If the string is:

String common_mpCon = "20+5*Math.ceil(2/7)"; //(equals 200 btw)

I'm trying to make the String do all the operations inside while it gets converted into a short. Is it possible? Using Short.parseShort(common_mpCon), I get an exception thrown, with googling I found no useful answer, so I finally decided to ask stackoverflow :D

Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
  • 5
    Maybe this helps: http://stackoverflow.com/questions/2605032/using-eval-in-java – Bart van Heukelom Jul 02 '11 at 22:16
  • @Bart I think that it is a valid case, however he would have to know that he will be only executing Javascript. The other option would be to actually parse out the operations and use reflection to execute static Java classes. – Suroot Jul 02 '11 at 22:22
  • 1
    If you wanted to get really odd, you could try adding more code into the string to make it into a class with a static method containing the original string; then use the JavaCompiler to generate the class; finally using Java classloader to load the new class and execute it. – Suroot Jul 02 '11 at 22:30
  • Any ideas on why Java doesn't have eval? Just curious. – James P. Jul 02 '11 at 22:59
  • 3
    @James: This would need a compiler in any JRE. JVMs generally don't know about the Java language, they only care about bytecode. – Paŭlo Ebermann Jul 02 '11 at 23:23

2 Answers2

2

If you're using Java 6, you might consider using the javax.tools.javacompiler classes. It's a lot of work, and you'd want to be very cautious about much freedom users would have to execute arbitrary code in your JVM instance.

That said, there's a great example available at http://www.java2s.com/Code/Java/JDK-6/CompilingfromMemory.htm. It's not my code and I don't want to plagiarize, so I'll explain the technique and let you reference the link for the full example.

First, build a string containing the code you wish to call. As in actual source code, the newlines are optional.

String common_mpCon = "20+5*Math.ceil(2/7)";
String code;
code = "public class MyShortEvaluator {\n";
code += "    public static Short calculate() {\n";
code += "        return (" + common_mpCon + ");\n";
code += "    }\n";
code += "}\n";

Create a JavaFileObject which returns the value of code from its getCharContent method. Add this to an collection of JavaFileObjects, pass the result to a JavaCompiler instance's getTask() method to get a CompilationTask, and call the resulting task's call() method.

If all goes well, you can use Class.forName("MyShortEvaluator").getDeclaredMethod to get a reference to the defined class's new static method.

Of course other reflection techniques are available as well. You could define the Calculate() method in an interface, have MyShortEvaluator implement that interface, and then use newInstance to get a reference to an actual object.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
GargantuChet
  • 5,691
  • 1
  • 30
  • 41
1

java.lang.String won't do these operations for you. Believe the JVM when it tells you.

If you interpret it as JavaScript, perhaps you can pass it to the eval() method and get what you want. Try Rhino.

duffymo
  • 305,152
  • 44
  • 369
  • 561