I know this has probably something to do with class loaders, however I couldn't find an example (it might be I'm google-ing for the wrong keywords.
I am trying to load a class (or a method) form a string. The string doesn't contain the name of a class, but the code for a class, e.g.
class MyClass implements IMath {
public int add(int x, int y) {
return x + y;
}
}
and then do something like this:
String s = "class MyClass implements IMath { public int add(int x, int y) { return x + y; }}";
IMath loadedClass = someThing.loadAndInitialize(string);
int result = loadedClass.add(5,6);
Now obviously, the someThing.loadAndInitialize(string)
- part is the one I don't know how to achieve. Is this even possible? Or would it be easier to run JavaScripts and somehow "give" the variables / objects (like x and y)?
Thank you for any hints.
You can use Rhino and JavaScript in JDK 7. That might be a good way to do it.
invokedynamic
is coming....If you want to stick with Java, you need something to parse the source and turn it into byte code - something like cglib.
At first you need to compile your code, for example using compiler API: ( http://www.accordess.com/wpblog/an-overview-of-java-compilation-api-jsr-199/, http://docs.oracle.com/javase/6/docs/api/javax/tools/package-summary.html). And after it load compiled class with ClassLoader ( http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/ClassLoader.html )
Use Java Compiler API. Here is a blog post that shows you how to do it.
You can use temporary files for this, as this requires input/output file, or you can create custom implementation of JavaFileObject that reads source from string. From the javadoc:
Once you have the output file (which is a compiled
.class
file), you can load it usingURLClassLoader
as follows:and then instantiate it, using:
or using a
Constructor
.You could compile it using
JavaCompiler
but I suggest you to useGroovy
for this run-time class creation. It would be much easier.