How to check groovy script for compilation errors

Vinay Prajapati picture Vinay Prajapati · Aug 20, 2015 · Viewed 8.1k times · Source

We can create and run groovyscript at runtime using code below

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;

import java.io.File;
import java.io.IOException;

        // Create a String with Groovy code.
        final StringBuilder groovyScript = new StringBuilder();
        groovyScript.append("class Sample {");
        groovyScript.append(" String sayIt() { return \"Groovy says: Cool jajaja\" }");
        groovyScript.append("}");



        GroovyClassLoader gcl = new GroovyClassLoader()

        GroovyCodeSource codeSource = new GroovyCodeSource(groovyScript.toString(), "aa", GroovyShell.DEFAULT_CODE_BASE)

        //GCL will check for enabled cache over code source and use sourceCache to cache code with name
        def scriptClass =  gcl.parseClass(codeSource)
                def classInstance = scriptClass.newInstance()

assert "Groovy says: Cool jajaja".equals(classInstance.sayIt())

Now suppose in code above, we introduced an error and now the above code is as below:

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;

import java.io.File;
import java.io.IOException;

        // Create a String with Groovy code.
        final StringBuilder groovyScript = new StringBuilder();
        groovyScript.append("class Sample {");
        groovyScript.append("jajaja");
        groovyScript.append(" String sayIt() { return \"Groovy says: Cool jajaja\" }");
        groovyScript.append("}");

        GroovyClassLoader gcl = new GroovyClassLoader()

        GroovyCodeSource codeSource = new GroovyCodeSource(groovyScript.toString(), "aa", GroovyShell.DEFAULT_CODE_BASE)

        //GCL will check for enabled cache over code source and use sourceCache to cache code with name
        def scriptClass =  gcl.parseClass(codeSource)
                def classInstance = scriptClass.newInstance()

assert "Groovy says: Cool jajaja".equals(classInstance.sayIt())

Notice, we have added "jajaja" there in script after class declaration.

What should be done here to know that our script is having compilation errors and will fail with MissingPropertyException or other exception.

When try same with groovyConsole, it breaks the script with following error

1 compilation error:

unexpected token: jajaja at line: 1, column: 15

Can we test script for any compilation errors before we run it? Adding a try catch block didn't work for me for this code.

Answer

Vinay Prajapati picture Vinay Prajapati · Aug 21, 2015

GroovyShell works well by using try-catch block.

                  GroovyCodeSource codeSource = new GroovyCodeSource(script, "aa", GroovyShell.DEFAULT_CODE_BASE)

        //GCL will check for enabled cache over code source and use sourceCache to cache code with name
        def scriptClass
        try {
            def shell = new GroovyShell()
            def data = shell.parse(codeSource.scriptText)
            data.run()
        }catch (Throwable e){
            status = false
        }
        if(!status){
            return "domain.script.compilation.errors"
        }else{
            return true
        }

Though One fallback is that whenever you have a script code which is partial and rest of the code would be added later from other script. This code will fail but that's a developer issue and not technical one.

Also, this will run the script which may cause data updates which is bad.