我正在尝试在grails项目中实现AST Transformation。
运行grails clean
和grails compile
时出现以下错误。我不知道我哪里出错了,它在一个样本groovy项目中运行良好。
Compilation error: startup failed:
/home/manoj/grailsgradle/ast/grails-app/controllers/ast/AstController.groovy: 12: unable to resolve class WithLogging , unable to find class for annotation
@ line 12, column 5.
@WithLogging
我在grails项目目录的WithLogging
中创建了两个文件,即WithLoggingASTTransformation
和/src/ast/
。
WithLogging:
package ast
import org.codehaus.groovy.transform.GroovyASTTransformationClass
import java.lang.annotation.ElementType
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Target
@Retention(RetentionPolicy.SOURCE)
@Target([ElementType.METHOD])
@GroovyASTTransformationClass(["WithLoggingASTTransformation"])
public @interface WithLogging {}
WithLoggingASTTransformation:
package ast
import org.codehaus.groovy.ast.ASTNode
import org.codehaus.groovy.control.CompilePhase
import org.codehaus.groovy.control.SourceUnit
import org.codehaus.groovy.transform.ASTTransformation
import org.codehaus.groovy.transform.GroovyASTTransformation
@GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION)
class WithLoggingASTTransformation implements ASTTransformation {
@Override
void visit(ASTNode[] nodes, SourceUnit sourceUnit) {
println("******* inside ast *******")
}
}
我在/scripts
中创建了一个名为_Events.groovy
eventCompileStart = {
compileAST("/home/manoj/grailsgradle/ast", "/home/manoj/grailsgradle/ast/target/")
}
def compileAST(def srcBaseDir, def destDir) {
ant.sequential {
println "Precompiling AST Transformations ..."
println "src ${srcBaseDir} ${destDir}"
path id: "grails.compile.classpath", compileClasspath
def classpathId = "grails.compile.classpath"
mkdir dir: destDir
groovyc(destdir: destDir,
srcDir: "$srcBaseDir/src/ast",
classpathref: classpathId,
verbose: grailsSettings.verboseCompile,
stacktrace: "yes",
encoding: "UTF-8")
echo "done precompiling AST Transformations"
}
}
在/controllers/ast
中,我创建了一个控制器AstController
package ast
class AstController {
def index() {
loggedMethod()
}
@WithLogging
def loggedMethod() {
println "i am doing some important stuff!"
}
}