【问题标题】:Nested Annotation in JCodeModelJCodeModel 中的嵌套注解
【发布时间】:2019-01-31 13:06:55
【问题描述】:

我想从 XML 文件创建一个 CompoundIndexes 注释,如下所示。

但是,我不知道如何将带参数的 CompoundIndex Annotation 放在 CompoundIndexes Annotation 中。这是我尝试过的。

JAnnotationUse indexesAnnotation = currentClass.annotate(CompoundIndexes.class);

JAnnotationArrayMember arrayMember = indexesAnnotation.paramArray("value");

 JAnnotationUse indexesParameter = currentClass.annotate(CompoundIndex.class)
.param("def", some_parameter);

arrayMember.param(indexesParameter);

但是,我最终得到以下结果。

我怎样才能只获取嵌套的compoundIndexes部分并摆脱下面的两个组件索引注释?

换句话说,如何创建一个注解并将其作为参数传递给另一个注解?

【问题讨论】:

    标签: jcodemodel


    【解决方案1】:

    看源码,param() 好像已经贬值了,建议改用annotate()。使用annotate() 可以解决您的问题:

    JAnnotationUse indexesAnnotation = currentClass.annotate(CompoundIndexes.class);
    
    JAnnotationArrayMember arrayMember = indexesAnnotation.paramArray("value");
    
    arrayMember.annotate(CompoundIndex.class)
            .param("name", "email_age")
            .param("def", "{'email.id' : 1, 'age' : 1}");
    

    给予:

    @CompoundIndexes({
        @CompoundIndex(name = "email_age", def = "{'email.id' : 1, 'age' : 1}")
    })
    

    【讨论】:

      猜你喜欢
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      • 2022-08-14
      • 2015-01-27
      • 2011-07-12
      • 2011-10-11
      • 2010-11-22
      • 2014-10-12
      相关资源
      最近更新 更多