Search code examples
groovyinternals

Reusing Groovy CompilerConfiguration objects


I have some code which creates a Groovy CompilerConfiguration with a bunch of implicit imports added using an ImportCustomizer. I was wondering if it would be safe to reuse the same CompilerConfiguration object in multiple GroovyClassLoaders or GroovyShells. I don't see any code in GroovyClassLoader or GroovyShell that mutates the passed-in CompilerConfiguration, so it's probably safe.


Solution

  • As long as you're stick with ImportCustomizer, it is probably safe, but neither compilation customizers nor the compiler configurations are designed to be thread safe, so it's probably better to use distinct configuration objects. This is in general true for any class in the compilation process: it's not thread safe.

    I can easily imagine a situation where you create a CompilerConfiguration, pass it to multiple GroovyShell instances and add customizers in different threads.

    So I would say, unless you have a very good reason to share the same configuration (and I don't really see one :)), go the safe way and use distinct objects.