Groovy AST-使用枚举添加类注释

Groovy AST-使用枚举添加类注释,groovy,annotations,abstract-syntax-tree,Groovy,Annotations,Abstract Syntax Tree,我试图在编译时使用AST将@JsonTypeInfo注释添加到我的类中 要添加的注释应显示为(以类为例): 其中JsonTypeInfo.Id定义为: public enum Id { NONE(null), CLASS("@class"), MINIMAL_CLASS("@c"), NAME("@type"), CUSTOM(null) ; } public enum As { PROPERTY, WRAPPER_OBJECT,

我试图在编译时使用AST将
@JsonTypeInfo
注释添加到我的类中

要添加的注释应显示为(以类为例):

其中
JsonTypeInfo.Id
定义为:

public enum Id {
    NONE(null),
    CLASS("@class"),
    MINIMAL_CLASS("@c"),
    NAME("@type"),
    CUSTOM(null)
    ;
}
public enum As {
    PROPERTY,
    WRAPPER_OBJECT,
    WRAPPER_ARRAY,
    EXTERNAL_PROPERTY
    ;
}
JsonTypeInfo.As
的定义如下:

public enum Id {
    NONE(null),
    CLASS("@class"),
    MINIMAL_CLASS("@c"),
    NAME("@type"),
    CUSTOM(null)
    ;
}
public enum As {
    PROPERTY,
    WRAPPER_OBJECT,
    WRAPPER_ARRAY,
    EXTERNAL_PROPERTY
    ;
}
都在
JsonTypeInfo
类中

要添加注释,我有一个函数
setJson()
,如下所示:

public static void setJson(ClassNode cn)
{
    AnnotationNode an = new AnnotationNode( new ClassNode(com.fasterxml.jackson.annotation.JsonTypeInfo.class));

    an.addMember("use", new ConstantExpression(JsonTypeInfo.Id.CLASS));
    an.addMember("include", new ConstantExpression(JsonTypeInfo.As.PROPERTY));
    an.addMember("property", new ConstantExpression("className"));

    cn.addAnnotation(an);
}
但是,似乎只有
属性
成员的设置没有问题。当我跑完剩下的时候,我会出现如下错误

"Expected enum value for attribute use in @com.fasterxml.jackson.annotation.JsonTypeInfo"
如何在AST转换期间正确传递枚举值?尝试直接传入值(即使用
CLASS
1
)无效


从这里查看其他
Expression
类:,我想可能
FieldExpression
可以完成这项工作,但我一直无法让它工作。

在AST浏览器中查找用
JsonTypeInfo
注释的类(如上面的示例注释),您会得到:

use: org.codehaus.groovy.ast.expr.PropertyExpression@7f78be49 [
         object: org.codehaus.groovy.ast.expr.ClassExpression@5014ec00[
                     type: com.fasterxml.jackson.annotation.JsonTypeInfo$Id
         ]
         property: ConstantExpression[CLASS]
     ]
这让我相信:

an.addMember("use", new ConstantExpression(JsonTypeInfo.Id.CLASS));
应该是:

an.addMember("use", new PropertyExpression(
                      new ClassExpression( JsonTypeInfo.Id ),
                      new ConstantExpression( JsonTypeInfo.Id.CLASS ) ) )

但是我还没有测试它,可能是胡说八道:-/

不得不修改
ClassExpression
以获取
ClassNode
,但就是这样<代码>一个.addMember(“使用”,新属性表达式(新类表达式(新类节点(com.fasterxml.jackson.annotation.JsonTypeInfo.Id.class)),新常量表达式(JsonTypeInfo.Id.class))