我正在使用kotlin-reflect
来反射 Kotlin 数据类
定义是这样的
@JsonIgnoreProperties(ignoreUnknown = true)
data class TopicConfiguration(
@JsonProperty("max.message.bytes") var maxMessageBytes: Long? = null,
@JsonProperty("compression.type") var compressionType: String? = null,
@JsonProperty("retention.ms") var retentionMs: Long? = null
)
我想使用反射来获得@JsonProperty
,但是当我尝试时
obj
.javaClass
.kotlin
.declaredMemberProperties
.first()
.findAnnotation<JsonProperty>()
然后无论我尝试什么,我都会得到null
。
如何使用对 Kotlin 数据类的反射访问属性注释(即杰克逊数据绑定中定义的@JsonProperty
(
我刚刚找到了一个答案:
使用 java-decompiler,很明显注释不在字段或 getter 上,而是在构造函数参数上。
public TopicConfiguration(@Nullable @JsonProperty("max.message.bytes") Long maxMessageBytes, @Nullable @JsonProperty("compression.type") String compressionType, @Nullable @JsonProperty("retention.ms") Long retentionMs)
{
this.maxMessageBytes = maxMessageBytes;this.compressionType = compressionType;this.retentionMs = retentionMs;
}
当我使用 Kotlin 的构造函数参数时,我能够检索注释
obj
.javaClass
.kotlin
.constructors
.first()
.parameters
.first()
.findAnnotation<JsonProperty>()