Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Writing Java compatible Kotlin code

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

Writing Java compatible Kotlin code

Dive into compatibility between Kotlin and Java

Avatar for markiyan-antonyuk

markiyan-antonyuk

May 28, 2019
Tweet

More Decks by markiyan-antonyuk

Other Decks in Technology

Transcript

  1. @Metadata( mv = {1, 1, 15}, bv = {1, 0,

    3}, k = 2, //1 Class, 2 File, 3 Synthetic class, 4 Multi-file class facade, 5 Multi-file class part d1 = {"\u0000\b\n\u0000\n\u0002\u0010\u0002\n\u0000..."}, d2 = {"main", "", "app"} ) public final class ToolingKt { public static final void main() { } // $FF: synthetic method public static void main(String[] var0) { main(); } }
  2. Fields val valField: Int int getValField() var varField: String String

    getVarField() void setVarField(String) lateinit var lateinitField: String String getLateinitField() void setLateinitField(String) public String lateinitField
  3. Functions fun someFunction(): Unit void someFunction() fun anotherFunction(Int): Int int

    anotherFunction(int) Foo.kotlin: fun topLevelFunction(): Unit FooKt: static void topLevelFunction() fun String.extensionFunction(): String FooKt: static String extensionFunction(String)
  4. var nullableString: String? @Nullable String getNullableString() void setNullableString(@Nullable String) var

    nullableInt: Int? @Nullable Integer getNullableInt() void setNullableInt(@Nullable Integer) fun nullableReturnType() : String? @Nullable String nullableReturnType() Null-safety
  5. Use-site Targets @file @param @field @get @set @setparam @property @receiver

    @delegate @JvmName @JvmSynthetic @JvmOverloads @JvmStatic @JvmField @JvmDefault @JvmMultifileClass @JvmSuppressWildcards @JvmWildcard @Strictfp @Synchronized @Throws @Transient @Volatile
  6. file fun get set prop field init @JvmName + +

    + + @JvmSynthetic + + + + @JvmOverloads + + @JvmStatic + + + + @JvmField + @Synchronized + + + @Throws + + + +
  7. @JvmName Awesome.kotlin: @file:JvmName("Awesome") AwesomeKt.foo() -> Awesome.foo() @JvmName("bar") fun foo(): Unit

    foo() -> bar() @set:JvmName("updateFoo") var foo: Int setFoo(int) -> updateFoo(int)
  8. @JvmSynthetic @JvmSynthetic fun foo(): Unit // $FF: synthetic method @JvmSynthetic

    lateinit var bar: String // $FF: synthetic field @set:JvmSynthetic var baz: Int // $FF: synthetic method
  9. @JvmOverloads class Foo @JvmOverloads new Foo(int, String) constructor(required: Int, default:

    String = "") new Foo(int) fun foo(default1: Int = 0, default2: String = ""): Unit void foo(int, String) void foo(int) void foo(String) void foo()
  10. @JvmStatic class Foo { companion object { val bar1: Int

    Foo.Companion.getBar1() const val bar2: Int Foo.bar2 @JvmStatic Foo.getBar3() val bar3: Int Foo.Companion.getBar3() @JvmField val bar4: Int Foo.bar4 @JvmStatic Foo.baz() fun baz(): Unit Foo.Companion.baz() } }
  11. @JvmField val foo: Int foo @Synchronized fun bar(): Unit {

    synchronized void bar() { synchronized(lock) {} synchronized(lock) { .. } } } @Throws(IOException::class) fun baz(): Unit void baz() throws IOException
  12. data class Foo(val bar: Int, var baz: String) Foo(int, String)

    final int getBar() String getBaz() void setBaz(String) int component1() String component2() Foo copy(int, String) String toString() int hashCode() boolean equals(Object) Data class
  13. androidExtensions { experimental = true } @Parcelize data class Foo(val

    bar: Int, var baz: String) : Parcelable static class Creator implements android.os.Parcelable.Creator static android.os.Parcelable.Creator CREATOR = new Foo.Creator(); int describeContents() void writeToParcel(Parcel, int) Data class
  14. interface Foo { interface Foo { fun foo(): Int =

    1 int foo(); } static final class DefaultImpls { static int foo(Foo $this) { return 1; } } } class Bar : Foo class Bar implements Foo { int foo() { return Foo.DefaultImpls.foo(this); } } Interface
  15. package kotlin.jvm.functions interface Function0<out R> : Function<R> { operator fun

    invoke(): R } interface Function1<in P1, out R> : Function<R> { operator fun invoke(p1: P1): R } …. interface Function22<in P1, in P2,… in P22> : Function<R> @SinceKotlin("1.3") interface FunctionN<out R> : Function<R>, FunctionBase<R> { operator fun invoke(vararg args: Any?): R override val arity: Int } Lambdas
  16. val foo: () -> Unit getFoo().invoke() val bar: (Int, Int,

    Int) -> Unit getBar().invoke(int, int, int) val baz: String.(Int) -> Unit getBaz().invoke(String, int) val qux: (Int, … Int) -> Unit getQux().invoke(1, "2", new Random(), null) Lambdas
  17. sealed class Foo abstract class Foo { private Foo() {

    } // $FF: synthetic method public Foo(DefaultConstructorMarker) { this(); } } class Bar : Foo() final class Bar extends Foo { public Bar() { super((DefaultConstructorMarker)null); } } Sealed class
  18. final CoroutineContext context = GlobalScope.INSTANCE.getCoroutineContext(); CoroutineScope scope = new CoroutineScope()

    { @Override public CoroutineContext getCoroutineContext() { return context; } }; Function2<CoroutineScope, Continuation<? super Unit>, Object> coroutine = new Function2<CoroutineScope, Continuation<? super Unit>, Object>() { @Override public Object invoke(CoroutineScope coroutineScope, Continuation<? super Unit> continuation) { System.out.println(1); DelayKt.delay(1000, continuation); System.out.println(2); return Unit.INSTANCE; } }; BuildersKt.launch(scope, context, CoroutineStart.DEFAULT, coroutine);
  19. suspend fun foo(): Int = 1 Object foo(@NotNull Continuation) {

    return Boxing.boxInt(1); } GlobalScope.launch { println(1) delay(1000) println(2) }
  20. Q&A