0

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!

vjanzaldi
  • 1
  • 1
  • How have you confirmed that the @CompileStatic annotation is not taking affect with your current code? – rhinds Jun 28 '16 at 20:41
  • Good question, how would I check? I assume no since I read that compiling scripts statically requires a setting up a CompilerConfiguration. I'm also experiencing very slow run times when running in my library (using GroovyClassLoader to compile) versus standalone in my Eclipse environment (using the maven groovy plugin to compile the groovy files). – vjanzaldi Jun 29 '16 at 01:40
  • Hmm. It's an interesting question - I have not actually looked but naively would assume that it still takes affect if compiled on the fly - much like I would assume other AST transform annotations would still work if loaded by the GroovyClassloader. Bulk of the performance impact loading dynamically rather is likely to be because of the overhead of loading the script and compiling it, rather than the effects of CompileStatic (or not being applied) – rhinds Jun 29 '16 at 11:15
  • Agreed that most of the overhead would be associated with the compiling, however I believe I have the GroovyClassLoader configured to cache the compiled class by setting the codeSource.setCachable(true). – vjanzaldi Jun 29 '16 at 12:03
  • So you are continuing to get the poor performance re-running the same script more than once? What scale are you talking about when you mention "very slow run times"? – rhinds Jun 29 '16 at 15:27
  • https://stackoverflow.com/questions/57897628/groovyclassloader-call-to-parseclass-is-successful-even-when-code-does-not-comp/57904533#57904533 – daggett Sep 12 '19 at 11:44

0 Answers0