【问题标题】:How to get the default value of an annotated variable如何获取带注释的变量的默认值
【发布时间】:2019-08-14 09:36:56
【问题描述】:

我有如下注释:

@Arg
internal var stringArg1: String? = null
@Arg
internal var stringArg2: String = "default value"

我遍历所有带注释的类变量,如下所示:

for (e in annotatedElement.enclosedElements) {
    if (e.getAnnotation(Arg::class.java) != null) {
        val defaultValue = ??? 
    }
}

问题:

是否可以获取带注释的变量的默认值?在我的示例中,我想为我的两个变量检索 null"default value"

【问题讨论】:

    标签: annotations annotation-processing kotlinpoet


    【解决方案1】:

    不,这不可能。

    默认值必须存储在 Kotlin 元数据中,以便您在注释处理器中访问它们(并且它们不存储在那里)。

    你可以在Kotlin repository看到一些细节:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多