Configure GrrovyClassLoader to honor @CompileStatic annotation

251 Views Asked by At

I have a custom Java libary that implements business rules implemented in Groovy and provided via configuration file. I use the GroovyClassLoader to compile the business rules and return the desired class as follows:

public class GroovyScriptClassLoader {
    private GroovyClassLoader classLoader = null;

    public GroovyScriptClassLoader() {
        this.classLoader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader());
    }

    public Rule getRule(final String ruleName, final String ruleSource) {
        Class<?> ruleClazz = parseScript(ruleName, ruleSource);
    }

    private Class<?> parseScript(final String ruleName, final String ruleSource) {
        GroovyCodeSource codeSource = new GroovyCodeSource(ruleSource, ruleName, GroovyShell.DEFAULT_CODE_BASE);
        codeSource.setCachable(true);
        return classLoader.parseClass(codeSource);
    }
}

My business rules embedded in my configuration files are annotated at the class level with @CompileStatic annotation.

How can I get my GroovyClassLoader configured to do a static compile?

I would guess it involves creating the GroovyClassLoader with a CompilerConfiguration (i.e. GroovyClassLoader(ClassLoader loader, CompilerConfiguration config), but I'm having difficulty finding the specific information to configure CompilerConfiguration to accomplish this.

Can anyone provide an example on how to configure GroovyClassLoader to compile statically?

Thanks!

0

There are 0 best solutions below