I want to know if there is any way to convert a String
to Java compilable code.
I have a comparative expression saved in a database field. I want to re
If you are using Java 6, you could try the Java Compiler API. At its core is the JavaCompiler class. You should be able to construct the source code for your Comparator
object in memory.
Warning: I have not actually tried the code below as the JavaCompiler object is not available on my platform, for some odd reason...
Warning: Compiling arbitrary Java code can be hazardous to your health.
Consider yourself warned...
String comparableClassName = ...; // the class name of the objects you wish to compare
String comparatorClassName = ...; // something random to avoid class name conflicts
String source = "public class " + comparatorClassName + " implements Comparable<" + comparableClassName + "> {" +
" public int compare(" + comparableClassName + " a, " + comparableClassName + " b) {" +
" return " + expression + ";" +
" }" +
"}";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
/*
* Please refer to the JavaCompiler JavaDoc page for examples of the following objects (most of which can remain null)
*/
Writer out = null;
JavaFileManager fileManager = null;
DiagnosticListener<? super JavaFileObject> diagnosticListener = null;
Iterable<String> options = null;
Iterable<String> classes = null;
Iterable<? extends JavaFileObject> compilationUnits = new ArrayList<? extends JavaFileObject>();
compilationUnits.add(
new SimpleJavaFileObject() {
// See the JavaDoc page for more details on loading the source String
}
);
compiler.getTask(out, fileManager, diagnosticListener, options, classes, compilationUnits).call();
Comparator comparator = (Comparator) Class.forName(comparableClassName).newInstance();
After this, you just have to store the appropriate Java expression in your database field, referencing a
and b
.
A simple way to get code snippets to executable byte code is with the Javassist library.
You can possibly adapt the techniques described in http://www.ibm.com/developerworks/java/library/j-dyn0610/ to fit your needs.
StringEscapeUtils.escapeJava from Commons Lang may help if you want to generate a piece of compilable code and dump a string there.
You shouldn't. Really!
Are you inventing another enterprise rules engine?. You might want to read these links.
Consider the fact that the only people that is skilled enough to write code and then insert it into a database, probably are having an editor and a compiler anyway...
The compiler will catch all those pesky syntax errors and you can even test the code! Remember that editors and compilers, and even computer languages were invented to help the programmer to comfortably write comprehensible code with a reasonable effort.
While I'm at it: read about the complicators gloves too!
Use Groovy!
Binding binding = new Binding();
GroovyShell shell = new GroovyShell(binding);
Object value = shell.evaluate("for (x=0; x<5; x++){println "Hello"}; return x");
Groovy might also be an option for you.
It integrates cleanly with the Bean Scripting Framework, can be embedded directly quite easily and might be ok, syntax-wise, for you.