Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reflection Kotlin和使用Kotlin reflect对属性的反射_Reflection_Kotlin_Jackson - Fatal编程技术网

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()