Java changes kotlin class contents to null

Kotlin has brilliant compile-time null checks using split up to "nullable?" and "notnullable". It has a KAnnotator to help determine whether objects coming from Java are invalid or not. But what if some part of the non-null class changes?

We believe that we have a cat, which undoubtedly has a head that can become real. When we ask the cat to laugh, she pats her head:

package org.cat

class CatHead(){
    fun mew(){
        println("Mew")
    }
}

class Cat(){
    var head = CatHead()
    fun mew(){
         head.mew()
    }
}

fun main(args:Array<String>){
   val cat = Cat()
   cat.mew()
}

Now let's add to this beautiful picture a maniac cat JAVA (CatManiac.java), which cuts off the heads of cats when it receives:

import org.cat.*;

public class CatManiac {
    public static void cutCatHead(Cat cat){
         cat.setHead(null);
    }
}

So, if we pass the cat to the maniac, he definitely cuts his head. And how does a cat say mew without a head?

fun main(args:Array<String>){
   val cat = Cat()
   CatManiac.cutCatHead(cat)
   cat.mew()
}

kotlin - null, , cat.mew():

Exception in thread "main" java.lang.IllegalArgumentException". 

, , Java . - / ?

P.S. - KAnnotator ?

+3
1

Java, Kotlin.

, , IntelliJ . Community Edition, .

, Kotlin Java: , . . Kotlin , null.

+3

All Articles