如何检查Groovy脚本是否存在编译错误 [英] How to check groovy script for compilation errors

查看:528
本文介绍了如何检查Groovy脚本是否存在编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用以下代码在运行时创建并运行groovyscript

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())

注意,在类声明之后,我们在脚本中添加了"jajaja".

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

在这里该怎么做才能知道我们的脚本有编译错误,并且会因MissingPropertyException或其他异常而失败.

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

当与groovyConsole尝试相同时,它会破坏脚本并显示以下错误

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

1 compilation error:

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

我们可以在运行脚本之前测试脚本是否存在编译错误吗? 对于此代码,添加try catch块对我不起作用.

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.

推荐答案

GroovyShell通过使用try-catch块可以很好地工作.

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.

这篇关于如何检查Groovy脚本是否存在编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆