Eval

There is no standard Java class or method that will do what you want. Your options include:

  • Select and use some third-party expression evaluation library. For example JEL or any of the half dozen libraries listed here.

  • Wrap the expression in the Java source code for a class with an eval method, send that to the Java compiler, and then load the resulting compiled class.

  • Use some scripting language that can be called from Java as an expression evaluator. Possibilities include Javascript, BeanShell, and so on.

  • Write your own expression evaluator from scratch.

The first approach is probably simplest. The second and third approaches are a potential security risk if you get the expression to be evaluated from an untrusted user. (Think code injection.)


 

http://stackoverflow.com/questions/2605032/using-eval-in-java

 

http://stackoverflow.com/questions/6559885/converting-a-string-operation-to-short-java?lq=1
http://www.java2s.com/Code/Java/JDK-6/CompilingfromMemory.html

http://www.crazyit.org/thread-3908-1-1.html
http://crazyit.org/thread-1064-1-1.html
 

 

 

你可能感兴趣的:(eval)