Eyv*_*ind 5 groovy abstract-syntax-tree
我正在尝试学习如何使用(和扩展)groovy,我正在关注此页面中的示例.基本上,它显示了如何为groovy代码定义一个注释,使您可以挂钩到编译器进程.该示例围绕着写入和注释,这将导致在方法调用之前和之后打印行.
我的代码如下; 首先必要的进口:
package foo
import org.codehaus.groovy.transform.*
import java.lang.annotation.*
import org.codehaus.groovy.ast.*
import org.codehaus.groovy.control.*
import org.codehaus.groovy.ast.stmt.*
import org.codehaus.groovy.ast.expr.*
Run Code Online (Sandbox Code Playgroud)
然后,我们定义要使用的注释:
@Retention(RetentionPolicy.SOURCE)
@Target([ElementType.METHOD])
@GroovyASTTransformationClass(["foo.LoggingASTTransformation"])
public @interface WithLogging {
}
Run Code Online (Sandbox Code Playgroud)
那么转型本身:
@GroovyASTTransformation(phase=CompilePhase.SEMANTIC_ANALYSIS)
public class LoggingASTTransformation implements ASTTransformation {
public void visit(ASTNode[] nodes, SourceUnit sourceUnit) {
println("visiting astnodes")
List methods = sourceUnit.getAST()?.getMethods()
// find all methods annotated with @WithLogging
methods.findAll { MethodNode method ->
method.getAnnotations(new ClassNode(WithLogging))
}.each { MethodNode method ->
Statement startMessage = createPrintlnAst("Starting $method.name")
Statement endMessage = createPrintlnAst("Ending $method.name")
List existingStatements = method.getCode().getStatements()
existingStatements.add(0, startMessage)
existingStatements.add(endMessage)
}
}
private Statement createPrintlnAst(String message) {
return new ExpressionStatement(
new MethodCallExpression(
new VariableExpression("this"),
new ConstantExpression("println"),
new ArgumentListExpression(
new ConstantExpression(message)
)
)
)
}
}
Run Code Online (Sandbox Code Playgroud)
最后,我的代码应该使用这个转换:
public class Foo {
@WithLogging
def f() { println "hello from f" }
}
f = new Foo()
f.f()
Run Code Online (Sandbox Code Playgroud)
现在,这应该从f \n结束f打印开始f \n你好,但它打印的所有内容都是来自f的hello(这就是我的问题).正如你从代码中看到的那样,我还在转换本身中放置了一个访问的astnodes消息,希望看看它是否能够实现,但唉它不会(或者看起来如此).
groovy -version 版画 Groovy Version: 1.6.0 JVM: 1.6.0_11
任何人都可以尝试这个代码,看看它是否适用于他们的系统,或者给我一些关于可能出错的指示?
尝试更改假设使用转换的代码
public class Foo
{
@foo.WithLogging
def f() { println "hello from f"}
....
Run Code Online (Sandbox Code Playgroud)
我尝试了相同的示例并在这里分享了我的经验http://kartik-shah.blogspot.com/2009/03/groovy-16-ast-transformation-example_5323.html