我使用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数据类上的反射访问属性注释(即在jackson data -bind中定义的@JsonProperty )
发布于 2018-10-03 20:58:56
我刚刚找到了答案:
使用java反编译器,注解显然不在字段或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的refection作为构造函数参数时,我能够检索到注释
obj
.javaClass
.kotlin
.constructors
.first()
.parameters
.first()
.findAnnotation<JsonProperty>()https://stackoverflow.com/questions/52626548
复制相似问题