kotlin.MetaData 中保存的数据格式是否记录在任何地方? [英] Is the format of the data held in kotlin.MetaData documented anywhere?

查看:20
本文介绍了kotlin.MetaData 中保存的数据格式是否记录在任何地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道添加到每个 Kotlin 类的 MetaData 注释中保存了哪些数据.

I'm interested to know what data is held in the MetaData annotation added to each Kotlin class.

但大多数字段没有提供更多细节

But most fields give no more detail than

自定义格式的元数据.不同种类的格式可能不同(甚至不存在)."

https://github.com/JetBrains/kotlin/blob/master/core/runtime.jvm/src/kotlin/Metadata.kt

是否有任何参考资料可以解释如何解释这些数据?

Is there are reference somewhere that explains how to interpret this data?

推荐答案

kotlin.Metadata 包含有关 Kotlin 符号的信息,例如它们的名称、签名、类型之间的关系等.其中一些信息已经存在于类文件的 JVM 签名中,但很多不是,因为有很多 Kotlin 特定的东西 JVM 类文件无法正确表示:类型可为空性、可变/只读集合接口、声明站点差异, 和其他.

kotlin.Metadata contains information about Kotlin symbols, such as their names, signatures, relations between types, etc. Some of this information is already present in the JVM signatures in the class files, but a lot is not, since there's quite a few Kotlin-specific things which JVM class files cannot represent properly: type nullability, mutable/read-only collection interfaces, declaration-site variance, and others.

没有采取任何具体措施来公开此注释中编码的数据的模式,因为对于大多数用户而言,在运行时内省程序需要此类数据,并且 Kotlin 反射库为此提供了一个很好的 API.

No specific actions were taken to make the schema of the data encoded in this annotation public, because for most users such data is needed to introspect a program at runtime, and the Kotlin reflection library provides a nice API for that.

如果您需要检查未通过反射 API 公开的特定于 Kotlin 的内容,或者您​​只是好奇该注解中还存储了哪些内容,您可以查看 kotlinx.reflect.lite.它是一个轻量级的库,其核心是 protobuf 生成的模式解析器.目前没有太多支持,但有 schemas可用的您可以使用它来读取您需要的任何其他数据.

If you need to inspect Kotlin-specific stuff which is not exposed via the reflection API, or you're just generally curious what else is stored in that annotation, you can take a look at the implementation of kotlinx.reflect.lite. It's a light-weight library, the core of which is the protobuf-generated schema parser. There's not much supported there at the moment, but there are schemas available which you can use to read any other data you need.

UPD(2018 年 8 月):自从回答这个问题后,我们发布了一个新的(实验性的和不稳定的)库,该库旨在成为读取和修改元数据的预期方式:https://discuss.kotlinlang.org/t/annoucing-kotlinx-metadata-jvm-library-for-reading-modifying-metadata-of-kotlin-jvm-class-files/7980

UPD (August 2018): since this was answered, we've published a new (experimental and unstable) library, which is designed to be the intended way for reading and modifying the metadata: https://discuss.kotlinlang.org/t/announcing-kotlinx-metadata-jvm-library-for-reading-modifying-metadata-of-kotlin-jvm-class-files/7980

这篇关于kotlin.MetaData 中保存的数据格式是否记录在任何地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆