嵌入Groovy脚本时添加一些默认import

刚才给在做的项目里加了些便捷功能,把做法记下来。 
我们在Java应用里嵌入了Groovy脚本。为了方便,业务里经常用的一些类可以认import进来,让大家少写点重复代码。幸好,这种功能用Groovy来做毫不费力。 
import groovy.lang.GroovyClassLoader;  
  
import java.security.CodeSource;  
  
import org.codehaus.groovy.ast.ClassHelper;  
import org.codehaus.groovy.ast.ModuleNode;  
import org.codehaus.groovy.control.CompilationFailedException;  
import org.codehaus.groovy.control.compilationunit;  
import org.codehaus.groovy.control.CompilerConfiguration;  
import org.codehaus.groovy.control.Phases;  
import org.codehaus.groovy.control.sourceUnit;  
import org.codehaus.groovy.control.compilationunit.sourceUnitOperation;  
  
public class CustomGroovyClassLoader extends GroovyClassLoader {  
    private final String[] DEFAULT_IMPORT_CLASSES = {  
        "rednaxelafx.sample.Foo","rednaxelafx.sample.Bar"  
    };  
      
    /** 
     * Validate classes. 
     * 
     * Because the specified default import classes might not be present in 
     * the during runtime,they have to be validated first,otherwise Groovy 
     * won't compile the code. 
     *  
     * @param className the name of the class to validate 
     * @return true iff the class exists and is valid 
     */  
    private boolean isClassValid(String className) {  
        try {  
            loadClass(className,false);  
            return true;  
        } catch (ClassNotFoundException e) {  
            return false;  
        }  
    }  
      
    private static String getClassSimpleName(final String className) {  
        return className.substring(className.lastIndexOf('.') + 1);  
    }  
      
    private static boolean alreadyImported(final String alias,final ModuleNode ast) {  
        return ast.getImport(alias) != null;  
    }  
      
    /** 
     * add default imports 
     */  
    @Override  
    protected compilationunit createcompilationunit(  
            CompilerConfiguration config,CodeSource source) {  
        compilationunit compunit = super.createcompilationunit(config,source);  
        compunit.addPhaSEOperation(new SourceUnitOperation() {  
            public void call(SourceUnit source) throws CompilationFailedException {  
                ModuleNode ast = source.getAST();  
                for (String className : DEFAULT_IMPORT_CLASSES) {  
                    String simpleClassName = getClassSimpleName(className);  
                    if (isClassValid(className) && !alreadyImported(simpleClassName,ast)) {  
                        ast.addImport(simpleClassName,ClassHelper.make(className));  
                    }  
                }  
            }  
        },Phases.CONVERSION);  
        return compunit;  
    }  
}


直接用ast.addImport()的话,这些添加的“认”import实际上是被append到原本的import列表的后面了。Groovy里后import的比先import的要更优先,所以这种认import可能会带来干扰。要注意。

相关文章

背景:    8月29日,凌晨4点左右,某服务告警,其中一个...
https://support.smartbear.comeadyapi/docs/soapui/steps/g...
有几个选项可用于执行自定义JMeter脚本并扩展基线JMeter功能...
Scala和Java为静态语言,Groovy为动态语言Scala:函数式编程,...
出处:https://www.jianshu.com/p/ce6f8a1f66f4一、一些内部...
在运行groovy的junit方法时,报了这个错误:java.lang.Excep...