When Groovy has grown up to version 2, it has gained a lot of brand new features and improvements in areas of productivity and security. Groovy now provides the ability to statically type check and statically compile your Groovy code for robustness and performance. This is very useful when you don't need Groovy's dynamic features, or simply want to speed up performance of your scripts.
Let's say, you're invoking Groovy scripts via GroovyShell within your application. You may want your scripts to be statically compiled in order to prevent any missing properties or methods passing to runtime, or may just want to speed up your system. For such purpose, you can make your own extension for groovy script files, and tell GroovyShell that you want it to statically compile all scripts with that extension.
Take a look at these lines of code:
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9 10 11 12 13
The following lines of code configure GroovyShell's behavior for files with *.sgroovy extension, so that these scripts will be compiled statically. This means that when you run your scripts with *.sgroovy extension using this modified GroovyShell – any undeclared variable, method or metaClass extending, etc. will result in MultipleCompilationException at compilation time!
Sometimes you may want your executed scripts to have some predefined methods, properties, classes or annotations. This can be done by creating an abstract class inherited from Script class. Assume, you want your script to return null instead of throwing MissingProperty/MissingMethod exception in situations when property or method doesn't exist. All you need is to set your custom Script class as a script base superclass in CompilerConfiguration and then apply it to GroovyShell.
Here's an example of how you can achieve this:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7
Besides these features, you can do even more: customize imports, secure AST (allow/disallow closures creations, imports, package definition, definition of methods, etc.), and more.
Check out Groovy Official Documentation to stay in touch!