Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling and using Groovy classes from Java at runtime?

I have an app which I'd like to make extensible by letting users define classes in Groovy, eventually implementing some interfaces.

The key aspect is that it should be interpreted/compiled at runtime. I.e. I need my app to take the .groovy and compile it. Doing it during boot is ok.

Then, of course, my app should be able to instantiate that class.

I see two solutions:

1) Compile while the app runs, put the classes somewhere on classpath, and then just load the classes, pretending they were always there.

2) Some smarter way - calling a compiler API and some classloading magic to let my system classloader see them.

How would I do option 2)?
Any other ideas?

like image 749
Ondra Žižka Avatar asked Jun 03 '13 17:06

Ondra Žižka


People also ask

Can I use Java classes in Groovy?

Groovy scripts can use any Java classes. They can be compiled to Java bytecode (in . class files) that can be invoked from normal Java classes. The Groovy compiler, groovyc, compiles both Groovy scripts and Java source files, however some Java syntax (such as nested classes) is not supported yet.

Can you mix Java and Groovy?

With joint compilation, the Groovy compiler will: parse the source files. depending on the implementation, create stubs that are compatible with the Java compiler. invoke the Java compiler to compile the stubs along with Java sources – this way Java classes can find Groovy dependencies.

Does Groovy need JDK or JRE?

The answer is yes. In fact, all groovy code compiles down to Java classes that run on the JRE. All you need is JRE 1.4 or higher and the groovy-all-*.

Does Groovy need to be compiled?

Unlike Java, a Groovy source code file can be executed as an (uncompiled) script, if it contains code outside any class definition, if it is a class with a main method, or if it is a Runnable or GroovyTestCase. A Groovy script is fully parsed, compiled, and generated before executing (similar to Python and Ruby).


1 Answers

Have a look at Integrating Groovy into applications

  • Get class Loader
  • Load class
  • Instantiate class.

Beauty:-
Since .groovy compiles to .class bytecode, parsing the class would give you an instanceof Class. Now it becomes all JAVA world, only difference, once you get hold of GroovyObject after instantiatiation, you play around invoking methods on demand.

Edit: Just so it's contained here:

InputStream groovyClassIS = GroovyCompiler.class
     .getResourceAsStream("/org/jboss/loom/tools/groovy/Foo.groovy");

GroovyClassLoader gcl = new GroovyClassLoader();
Class clazz = gcl.parseClass(groovyClassIS, "SomeClassName.groovy");
Object obj = clazz.newInstance();
IFoo action = (IFoo) obj;
System.out.println( action.foo());

and

package org.jboss.loom.migrators.mail;

import org.jboss.loom.tools.groovy.IFoo;

public class Foo implements IFoo {
    public String foo(){
        return "Foooooooooo Action!";
    }
}
like image 61
dmahapatro Avatar answered Sep 23 '22 18:09

dmahapatro