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

查看:282
本文介绍了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.

未采取任何特定措施来公开此注释中编码的数据的架构,因为对于大多数用户而言,在运行时对此类程序进行自省需要使用这些数据,而/docs/reference/reflection.html"rel =" noreferrer> 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生成的模式解析器.目前那里没有太多支持,但是有方案可用的 您可以用来读取所需的任何其他数据.

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月):自从得到回答以来,我们已经发布了一个新的(实验性的和不稳定的)库,该库旨在用作读取和修改元数据的预期方式:

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天全站免登陆