[英]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 中,我喜欢使用,但不想访问自动创建的setOffset
和getOffset
:
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,您可以将@Deprecated与DeprecationLevel.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.