什么是@JvmSynthetic在Kotlin中的用途? [英] What's the intended use of @JvmSynthetic in Kotlin?

查看:889
本文介绍了什么是@JvmSynthetic在Kotlin中的用途?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 <$ kotlin-stdlib中的c $ c> @JvmSynthetic 注释,我想知道它是什么,但不幸的是,它没有记录。

I have come across the @JvmSynthetic annotation in kotlin-stdlib, and I'm wondering what it is for, but, unfortunately, it is undocumented.

据我所知,将它应用于程序元素会将合成修饰符添加到相应的字节码元素。因此,元素从Java变得不可见:

As far as I understand, applying it to a program element will add the synthetic modifier to the corresponding bytecode elements. As a consequence, the element becomes invisible from Java:

class MyClass {
    @JvmSynthetic
    fun f() { }
}

Java代码中的某处:

Somewhere in Java code:

MyClass c = new MyClass();
c.f() // Error: cannot resolve method f()

但是相同的元素在Kotlin代码中仍然可见:

But the same elements are still visible in Kotlin code:

val c = MyClass()
c.f() // OK

隐藏来自非Kotlin来源的声明是否有效使用 @JvmSynthetic ?它是预期用途吗?其他适当的用例是什么?

Is hiding declarations from non-Kotlin sources a valid use of @JvmSynthetic? Is it the intended use? What are the other appropriate use cases?

由于 @JvmSynthetic 隐藏Java中的函数,因此无法在Java中覆盖它们要么(当涉及 abstract 成员时,调用会导致 AbstractMethodError )。鉴于此,我可以使用 @JvmSynthetic 禁止在Java源代码中覆盖Kotlin类的成员吗?

Since @JvmSynthetic hides functions from Java, they cannot be overridden in Java either (and when it comes to an abstract member, the calls then result into AbstractMethodError). Given that, can I use @JvmSynthetic to prohibit overriding members of a Kotlin class in Java sources?

推荐答案

首先,为了回答实际 的合成方法,让我们来看看 Java语言规范

First, to answer what synthetic methods actually are, let's have a look at the Java language specification:


11. 如果Java编译器发出的构造与源代码中显式或隐式声明的构造不对应,则必须将其标记为 synthetic ,除非发出construct是一个类初始化方法(JVMS§2.9)。

11. A construct emitted by a Java compiler must be marked as synthetic if it does not correspond to a construct declared explicitly or implicitly in source code, unless the emitted construct is a class initialization method (JVMS §2.9).

@JvmSynthetic 注释正是这样做:阻止从源代码访问。该方法仍然会出现在反射中,然后标记为合成。

The @JvmSynthetic annotation does exactly that: prevent access from source code. The method will still appear in reflection and is then marked as synthetic.

更确切地说,来自 Kotlin文档(强调我的):

More precisely, from the Kotlin documentation (emphasis mine):


@JvmSynthetic



设置 ACC_SYNTHETIC Java字节码中带注释的目标上的标记。

@JvmSynthetic

Sets ACC_SYNTHETIC flag on the annotated target in the Java bytecode.

编译时Java源代码无法访问合成目标,但Kotlin源仍可访问。将目标标记为合成是一种二进制兼容的更改,已编译的Java代码将能够访问此类目标。

Synthetic targets become inaccessible for Java sources at compile time while still being accessible for Kotlin sources. Marking target as synthetic is a binary compatible change, already compiled Java code will be able to access such target.

此注释适用于稀有当API设计人员需要从Java API中隐藏Kotlin特定目标同时将其保留为Kotlin API的一部分时,所以生成的API对于两者都是惯用的。

This annotation is intended for rare cases when API designer needs to hide Kotlin-specific target from Java API while keeping it a part of Kotlin API so the resulting API is idiomatic for both.

如上所述最后一段, @JvmSynthetic 是一个API设计工具,它允许库编写者避免自动生成Java等价物。可能最流行的用例是仅Kotlin的功能,例如运算符重载, componentN()方法或属性,这些方法或属性可能有更多惯用的方式在Java中公开。

As described in the last paragraph, @JvmSynthetic is a tool for API design, which lets a library writer avoid automatic generation of Java equivalents. Probably the most popular use cases are Kotlin-only features, such as operator overloading, componentN() methods or properties, which may have a more idiomatic way to be exposed in Java.

值得注意的是,这个注释的目标是属性设置器/ getter,函数和字段 - 基本上所有用Java翻译成方法的东西。

It is noteworthy that the target of this annotations are property setters/getters, functions and fields -- basically everything that translates in Java to a method.

@Target([
    AnnotationTarget.FUNCTION,
    AnnotationTarget.PROPERTY_GETTER,
    AnnotationTarget.PROPERTY_SETTER,
    AnnotationTarget.FIELD])
annotation actual class JvmSynthetic

这篇关于什么是@JvmSynthetic在Kotlin中的用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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