简体   繁体   English

标记一个乐趣/财产仅用于 Kotlin / Java

[英]Mark a fun / property for use in Kotlin / Java only

I'm currently writing a KOTLIN class were I like to use the possibility of DSL but be backwards compatible for JAVA developers.我目前正在写一个 KOTLIN class 我喜欢使用 DSL 的可能性,但要向后兼容 JAVA 开发人员。 When using a var the compiler automatically creates getter and setter for JAVA, but those can't be used Builder style since they are not returning this .使用var时,编译器会自动为 JAVA 创建 getter 和 setter,但不能使用 Builder 样式,因为它们不返回this

So basically what I like to do is something like this:所以基本上我喜欢做的是这样的:

class MyClass {
    // for use in KOTLIN only DSL style e.g. MyClass() { offset = 1 }
    var offset: Int? = null

    // for use in JAVA only BUILDER style e.g. new MyClass().withOffset(1)
    fun withOffset(value: Int) = apply { offset = value }
}

In Kotlin I like to use, but don't want to have access to the withOffset fun:在 Kotlin 中,我喜欢使用,但不想访问withOffset的乐趣:

val myClass = MyClass() { offset = 1 }

In JAVA I like to use, but don't want to have access to the auto created setOffset and getOffset :在 JAVA 中,我喜欢使用,但不想访问自动创建的setOffsetgetOffset

MyClass myClass = new MyClass().withOffset(1)

Renaming the getter and setter is possible via @JvmName annotation already, but is there a annotation for hiding a public property for JAVA completly and optional of course vice versa?已经可以通过@JvmName注释重命名 getter 和 setter,但是是否有注释可以完全隐藏 JAVA 的公共属性,当然反之亦然?

You cannot hide a constructor, while you can make a so called fake-constructor using operator fun invoke in which you can use a @JvmSyntheic annotation to hide it from the java.您不能隐藏构造函数,但可以使用operator fun invoke创建所谓的假构造函数,您可以在其中使用@JvmSyntheic注释将其从 java 中隐藏起来。

And to hide a function from kotlin you can use @Deprecated with DeprecationLevel.HIDDEN .要从 kotlin 中隐藏 function,您可以将@DeprecatedDeprecationLevel.HIDDEN一起使用。

Note: The @JvmField will instruct the compiler not to generate the default getter and setter for your var注意: @JvmField将指示编译器不要为您的 var 生成默认的 getter 和 setter

class MyClass {
    @JvmField
    var offset: Int? = null

    @kotlin.Deprecated(message = "JUST FOR JAVA", level = DeprecationLevel.HIDDEN)
    fun withOffset(value: Int) = apply { offset = value }

    companion object {
        @JvmSynthetic
        operator fun invoke(init: MyClass.() -> Unit) = MyClass().apply(init)
    }
}

Usage in kotlin:在 kotlin 中的用法:

MyClass() { offset = 1 }
// or
MyClass { offset = 1 }

Usage in java:在 java 中的用法:

MyClass().withOffset(1)

Resources:资源:

It's not possible to do something like this in Kotlin在 Kotlin 中不可能做这样的事情

val myClass = MyClass() { offset = 1 }

but I would suggest you do do this instead, it looks much nicer in my opinion.但我建议你改为这样做,在我看来它看起来好多了。

// It's a inline function so there's no runtime overhead.
inline fun buildMyClass(builder: MyClass.() -> Unit): MyClass {
    return MyClass().apply(builder)
}

class MyClass {

    @JvmSynthetic
    var offset: Int? = null
        private set

    fun withOffset(value: Int) = apply { offset = value }
}

So you can call it like below所以你可以像下面这样称呼它

val myClass = buildMyClass {
   withOffset(0)
}

In Java, it looks like this:在 Java 中,它看起来像这样:

final MyClass myClass = new MyClass().withOffset(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java 调用乐趣中的用户 kotlin lib - User kotlin lib in java call fun Kotlin 多平台可以使用 Java 还是只能使用 Kotlin? - Kotlin Multi platform can you use Java or only Kotlin? 使用Kotlin属性访问语法来设置Java字段是否安全 - Is it safe to use Kotlin property access syntax to set a Java field 是否可以仅在kotlin / JAVA中将切换限制为仅用于特定情况? - Is it possible to restrict switch to use particular cases only in kotlin/JAVA? Java/Kotlin 中具有返回值的属性? - Property with return value in Java/Kotlin? 在 Kotlin 中使用 Java API - Use Java API in Kotlin 如何在Java 6上将Hibernate bean属性标记为@Transient? - How to mark Hibernate bean property as @Transient on Java 6? 将java lambda传递给kotlin时,“Function0不是功能接口”错误 - “Function0 is not a functional interface” error when passing java lambda to kotlin fun onBindViewHolder(RecyclerView.ViewHolder持有人,整数位置)-无法访问持有人的“有趣”-Kotlin to Java - onBindViewHolder(RecyclerView.ViewHolder holder, int position) - can't access holder's 'fun' - Kotlin to Java 如何在 android 工作室中使用 java 在 kotlin 中编写可运行的方法,例如“有趣的 main()” - how to write runnable method like "fun main()" in kotlin with java in android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM