Java GroovyShell不解析GString中占位符的花括号
我一直在处理Groovy应用程序的AST分析和检测,我注意到在编译的Java GroovyShell不解析GString中占位符的花括号,java,parsing,groovy,compilation,groovyshell,Java,Parsing,Groovy,Compilation,Groovyshell,我一直在处理Groovy应用程序的AST分析和检测,我注意到在编译的解析阶段之后,GString占位符中的花括号被完全忽略了 比如说, log.debug "Found ${recentEvents?.size() ?: 0} events in the last $deltaSeconds seconds" 变成 log.debug("Found $(recentEvents?.size()) ? recentEvents?.size() : 0 events
解析阶段之后,GString占位符中的花括号被完全忽略了
比如说,
log.debug "Found ${recentEvents?.size() ?: 0} events in the last $deltaSeconds seconds"
变成
log.debug("Found $(recentEvents?.size()) ? recentEvents?.size() : 0 events in the last $deltaSeconds seconds")
因此,占位符中的语句会与字符串的其他部分混淆,并在检测后编译时导致错误
GroovyShell
类的设置如下:
STAnalysisAST stal = new STAnalysisAST()
CompilerConfiguration cc = new CompilerConfiguration(CompilerConfiguration.DEFAULT)
cc.addCompilationCustomizers(stal)
GroovyShell gshell = new GroovyShell(cc)
这是STAnalysisAST
课程的摘录:
@GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS)
public class STAnalysisAST extends CompilationCustomizer{
public STAnalysisAST(Logger logger){
super(CompilePhase.SEMANTIC_ANALYSIS)
nodes = new ArrayList<ICFGNode>()
}
@Override
void call(SourceUnit source, GeneratorContext context, ClassNode classNode) {
def _code = Helper.getSourceFromNode(classNode)
}
}
AstNodeToScriptVisitor
类随Groovy 2.4.6提供
请帮助一个兄弟
class Helper {
public static String getSourceFromNode(ClassNode classNode){
java.io.StringWriter writer = new java.io.StringWriter();
AstNodeToScriptVisitor visitor = new AstNodeToScriptVisitor(writer);
visitor.visitClass(classNode);
return writer.toString();
}
}