嵌入Groovy脚本时添加一些默认import
发布时间:2020-12-14 16:52:30 所属栏目:大数据 来源:网络整理
导读:刚才给在做的项目里加了些便捷功能,把做法记下来。? 我们在Java应用里嵌入了Groovy脚本。为了方便,业务里经常用的一些类可以默认import进来,让大家少写点重复代码。幸好,这种功能用Groovy来做毫不费力。? import groovy.lang.GroovyClassLoader; 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可能会带来干扰。要注意。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |