KotlinとJavaとnullのライトな話

 KotlinとJavaとnullのライトな話

2016/11/26(土)のKotlin Internal勉強会の資料です

#kotlin_internal

https://connpass.com/event/40360/

F46c97bb74758d481531990654933050?s=128

RyotaMurohoshi

November 26, 2016
Tweet

Transcript

  1. 11.

    ͜ΕΛ࢖͏ͱ!(Kotlinͷίʔυ) val str = returnNullableStringKotlin() // ͜Ε΋OK // val str

    : String? = returnNullableStringKotlin() // ͜Ε͸ίϯύΠϧΤϥʔ Type Mismatch // val str : String = returnNullableStringKotlin() // println(str.length) // ίϯύΠϧΤϥʔ println(str?.length) // ͜ΕͳΒOK
  2. 17.

    ͜ΕΛ࢖͏ͱ!(JavaͷίʔυΛKotlinͷίʔυ͔ΒݺͿ) val str = Utility.returnNullableStringJava() // ͜Ε΋OK // val str

    : String? = Utility.returnNullableStringJava() // ஫໨ʂʂʂ // ͜Ε΋OKʂʂ ʂ // val str : String = Utility.returnNullableStringJava()
  3. 19.

    ʲ࠶ܝʳ͜ΕΛ࢖͏ͱ!(Kotlinͷίʔυ) val str = returnNullableStringKotlin() // ͜Ε΋OK // val str

    : String? = returnNullableStringKotlin() // ͜Ε͸ίϯύΠϧΤϥʔ Type Mismatch // val str : String = returnNullableStringKotlin() // println(str.length) // ίϯύΠϧΤϥʔ println(str?.length) // ͜ΕͳΒOK
  4. 25.

    JavaͷίʔυΛKotlinͷίʔυ͔ΒݺΜͰ Stringʹ୅ೖ val str : String = Utility.returnNullableStringJava() println(str.length) //

    ίϯύΠϧΤϥʔʮͰ͸ʯͳ͍͚Ͳ // ͦΕඞཁͳ͍Αͬͯɺܯࠂ͕ग़Δ // println(str?.length)
  5. 31.

    JavaͷίʔυΛKotlinͷίʔυ͔ΒݺΜͰ ܕΛ໌ࣔ͠ͳ͍ͱ val str = Utility.returnNullableStringJava() // ͜Ε΋OK println(str?.length) //

    ίϯύΠϧΤϥʔͰ͸ͳ͍͚ΕͲɺ // ࣮ߦ࣌ʹʮjava.lang.NullPointerExceptionʯ println(str.length)
  6. 34.

    ʲ࠶ܝʳJavaͷίʔυΛKotlinͷίʔυ͔ΒݺΜͰ Stringʹ୅ೖ val str : String = Utility.returnNullableStringJava() println(str.length) //

    ίϯύΠϧΤϥʔʮͰ͸ʯͳ͍͚Ͳ // ͦΕඞཁͳ͍Αͬͯɺܯࠂ͕ग़Δ // println(str?.length)
  7. 41.

    ʲ࠶ܝʳJavaͷίʔυΛKotlinͷίʔυ͔ΒݺΜͰ Stringʹ୅ೖ val str : String = Utility.returnNullableStringJava() println(str.length) //

    ίϯύΠϧΤϥʔʮͰ͸ʯͳ͍͚Ͳ ͜ͷByte%CodeΛΈͯΈΔ IDEAͰɺTools)>)Kotlin)>)Show)Kotlin)Bytecode
  8. 42.

    L0 LINENUMBER 73 L0 INVOKESTATIC com/muhron/kotlin_java_interpolator/Utility.returnNullableStringJava ()Ljava/lang/String; DUP LDC "Utility.returnNullableStringJava()"

    INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkExpressionValueIsNotNull (Ljava/lang/Object;Ljava/lang/String;)V ASTORE 0 L1 LINENUMBER 74 L1 ALOAD 0 INVOKEVIRTUAL java/lang/String.length ()I ISTORE 1 NOP L2 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; ILOAD 1 INVOKEVIRTUAL java/io/PrintStream.println (I)V L3 ͜ΕΛ͞ΒʹDecompoleʂ
  9. 46.

    h"ps:/ /github.com/JetBrains/kotlin/blob/1.0.5/core/run;me.jvm/src/kotlin/jvm/internal/ Intrinsics.java#L86 package kotlin.jvm.internal; @SuppressWarnings("unused") public class Intrinsics {

    private Intrinsics() { } /*ུ*/ public static void checkExpressionValueIsNotNull(Object value, String expression) { if (value == null) { throw sanitizeStackTrace(new IllegalStateException(expression + " must not be null")); } } /*ུ*/ }
  10. 51.
  11. 55.
  12. 57.

    @NotNullͳJavaͷίʔυΛKotlinͷίʔυ͔ΒݺΜͰ // IDE͸ɺฦΓ஋ܕ͕Stringͬͯڭ͑ͯ͘ΕΔʢString!Ͱ͸ͳ͍ʣ val str = Utility.returnStringWithNotNullAnnotation() // ͜Ε΋OK //

    val str : String = Utility.returnStringWithNotNullAnnotation() // ࣮͸ɺ͜Ε΋OK //val str : String? = Utility.returnStringWithNotNullAnnotation() println(str?.length) // ܯࠂ͕ͰΔ println(str.length) // ͜Ε͸OK
  13. 59.
  14. 61.

    @NullableͳJavaͷίʔυΛKotlinͷίʔυ͔ΒݺΜͰ // IDE͸ɺฦΓ஋ܕ͕String?ͬͯڭ͑ͯ͘ΕΔʢString!Ͱ͸ͳ͍ʣ val str = Utility.returnStringWithNullableAnnotation() // ͜Ε΋OK //val

    str : String? = Utility.returnNullableStringJava() // ͜Ε͸ίϯύΠϧΤϥʔ //val str : String = Utility.returnStringWithNullableAnnotation() println(str?.length) // ͜Ε͸OK // println(str.length) // ͜Ε͸ίϯύΠϧΤϥʔ
  15. 69.

    ࣮͸ෳ਺ରԠ͍ͯ͠Δ • org.jetbrains.annota.ons.Nullable • android.support.annota.on.Nullable • com.android.annota.ons.Nullable • org.eclipse.jdt.annota.on.Nullable •

    org.checkerframework.checker.nullness.qual.Nullable h"ps:/ /github.com/JetBrains/kotlin/blob/1.0.5/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/ java/JvmAnnota?onNames.kt
  16. 70.

    ࣮͸ෳ਺ରԠ͍ͯ͠Δʢଓ͖ʣ • javax.annota*on.Nullable • javax.annota*on.CheckForNull • edu.umd.cs.findbugs.annota*ons.CheckForNull • edu.umd.cs.findbugs.annota*ons.Nullable •

    edu.umd.cs.findbugs.annota*ons.PossiblyNull h"ps:/ /github.com/JetBrains/kotlin/blob/1.0.5/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/ java/JvmAnnota?onNames.kt
  17. 73.

    h"ps:/ /github.com/JetBrains/kotlin/blob/1.0.5/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/ java/JvmAnnota?onNames.kt val NULLABLE_ANNOTATIONS = listOf( JvmAnnotationNames.JETBRAINS_NULLABLE_ANNOTATION, FqName("android.support.annotation.Nullable"), FqName("com.android.annotations.Nullable"),

    FqName("org.eclipse.jdt.annotation.Nullable"), FqName("org.checkerframework.checker.nullness.qual.Nullable"), FqName("javax.annotation.Nullable"), FqName("javax.annotation.CheckForNull"), FqName("edu.umd.cs.findbugs.annotations.CheckForNull"), FqName("edu.umd.cs.findbugs.annotations.Nullable"), FqName("edu.umd.cs.findbugs.annotations.PossiblyNull") )
  18. 74.

    typeQualifiers.ktͷதʹ.... class JavaTypeQualifiers internal constructor( val nullability: NullabilityQualifier?, val mutability:

    MutabilityQualifier?, internal val isNotNullTypeParameter: Boolean ) { companion object { val NONE = JavaTypeQualifiers(null, null, false) } } ͷnullabilityͷੜ੒ʹ࢖ΘΕ͍ͯΔ h"ps:/ /github.com/JetBrains/kotlin/blob/1.0.5/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/ java/typeEnhancement/typeQualifiers.kt#L42