Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails 无法使用AST转换将方法添加到GORM对象_Grails_Groovy_Gorm_Abstract Syntax Tree - Fatal编程技术网

Grails 无法使用AST转换将方法添加到GORM对象

Grails 无法使用AST转换将方法添加到GORM对象,grails,groovy,gorm,abstract-syntax-tree,Grails,Groovy,Gorm,Abstract Syntax Tree,这是我的GORM对象 @UpdatedProperties class Cart { Date lastUpdated Date dateCreated String name } 以下是注释定义: @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @GroovyASTTransformationClass(["UpdatedPropertiesASTTransformation"]) pu

这是我的GORM对象

@UpdatedProperties
class Cart {
    Date lastUpdated
    Date dateCreated

    String name 
}
以下是注释定义:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@GroovyASTTransformationClass(["UpdatedPropertiesASTTransformation"])

public @interface UpdatedProperties {
}
这是AST的定义

@GroovyASTTransformation(phase = CompilePhase.CLASS_GENERATION)
class UpdatedPropertiesASTTransformation implements ASTTransformation{
    //...

    public void visit(ASTNode[] astNodes, SourceUnit sourceUnit) {
        astNodes.findAll { node -> node instanceof ClassNode}.each {
        classNode ->
            def testMethodBody = new AstBuilder().buildFromString (
                    """
                    println(">>*************myMethod)";
                    """
            )

            def myMethod = new MethodNode('myMethod', ACC_PUBLIC, ClassHelper.VOID_TYPE, [] as Parameter[], [] as ClassNode[], testMethodBody[0])
            classNode.addMethod(myMethod)   
        }
    }
...
}
当我尝试调用我得到的方法时:

groovy.lang.MissingMethodException:方法Cart.myMethod()的签名不适用于参数类型:()值:[]


任何提示,谢谢。感谢

类生成在向类添加方法的编译阶段太晚了,因此您需要将编译阶段更改为语义分析或规范化。(无论如何,这可能比添加方法要晚得多。)


AST字符串中还有几个问题。您的println中存在语法错误,
println(“>>****************myMethod)”应为
println(“>*******************myMethod”)
,您需要添加一个显式的
返回语句,因为您返回的是
void
(否则Groovy将在方法末尾添加一个
returnnull;
,这将与
void
返回类型冲突)

确保变压器被调用!