嵌入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可能会带来干扰。要注意。 1 楼 lwwin 2011-01-23 嗯,用这个的话,应该比用啥BAT方便的多了=3=! 2 楼 RednaxelaFX 2011-01-23 lwwin 写道嗯,用这个的话,应该比用啥BAT方便的多了=3=!
这跟BAT完全,是完全没有任何关系… 3 楼 chenchao051 2011-02-25 看了你的JVM分享,写的非常不错,南大软院的同学,鼓励一下。继续加油啊。
我也正准备把Groovy引入项目,现在的生产力实在是低下。 4 楼 RednaxelaFX 2011-02-25 chenchao051 写道看了你的JVM分享,写的非常不错,南大软院的同学,鼓励一下。继续加油啊。
我也正准备把Groovy引入项目,现在的生产力实在是低下。
哈哈,感谢^_^ 共同努力~
请问是哪一年级的同学?我是04