Reflection Kotlin和使用Kotlin reflect对属性的反射
我正在使用Reflection Kotlin和使用Kotlin reflect对属性的反射,reflection,kotlin,jackson,Reflection,Kotlin,Jackson,我正在使用kotlin reflect对kotlin数据类进行反射 定义是这样的 @JsonIgnoreProperties(ignoreUnknown = true) data class TopicConfiguration( @JsonProperty("max.message.bytes") var maxMessageBytes: Long? = null, @JsonProperty("compression.type") var compressionType: S
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>()
obj
.javaClass
科特林先生
.申报会员财产
.first()
.findAnnotation()
然后,无论我尝试什么,我都会得到null
如何使用Kotlin数据类上的反射访问属性注释(即jackson data bind中定义的
@JsonProperty
)我刚刚找到了一个答案:
使用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;
}
obj
.javaClass
.kotlin
.constructors
.first()
.parameters
.first()
.findAnnotation<JsonProperty>()
当我使用Kotlin的refection作为构造函数参数时,我能够检索注释
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;
}
obj
.javaClass
.kotlin
.constructors
.first()
.parameters
.first()
.findAnnotation<JsonProperty>()
obj
.javaClass
科特林先生
.建造商
.first()
.参数
.first()
.findAnnotation()