刚才给在做的项目里加了些便捷功能,把做法记下来。
我们在Java应用里嵌入了Groovy脚本。为了方便,业务里经常用的一些类可以默认import进来,让大家少写点重复代码。幸好,这种功能用Groovy来做毫不费力。
直接用ast.addImport()的话,这些添加的“默认”import实际上是被append到原本的import列表的后面了。Groovy里后import的比先import的要更优先,所以这种默认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可能会带来干扰。要注意。