Grails AST Transformation“无法找到类”错误

时间:2016-03-07 13:51:33

标签: grails groovy abstract-syntax-tree

我正在尝试在grails项目中实现AST Transformation。

运行grails cleangrails 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!"
    }
}

0 个答案:

没有答案