Is there a java equivalent of the python eval function?
This would be a function which takes an arbitrary string and attempts to execute it in the current context.>
"Yes" and "no". Yes in that it's possible. No in that it's not standard and has a number of limitations.
See BeanShell which allows execution of limited Java from a process that is, well, Java. I have never tried to use it as a library and can not vouch for its use as such.
BeanShell is a small, free, embeddable Java source interpreter with object scripting language features, written in Java. BeanShell dynamically executes standard Java syntax and extends it with common scripting conveniences such as loose types, commands, and method closures like those in Perl and JavaScript.
It is however, far more restrictive/limited than say eval in Python or another dynamic language. (Consider JRuby, Jython, Groovy and Clojure as some dynamic counterparts that run on the JVM). The local Java variable names in the surrounding code are all compiled away and thus not accessible, for instance.
I would recommend rethinking the approach, if possible ;-)
Happy coding.