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

KotlinとJavaとnullのライトな話

RyotaMurohoshi
November 26, 2016

 KotlinとJavaとnullのライトな話

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

#kotlin_internal

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

RyotaMurohoshi

November 26, 2016
Tweet

More Decks by RyotaMurohoshi

Other Decks in Technology

Transcript

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

    : String? = returnNullableStringKotlin() // ͜Ε͸ίϯύΠϧΤϥʔ Type Mismatch // val str : String = returnNullableStringKotlin() // println(str.length) // ίϯύΠϧΤϥʔ println(str?.length) // ͜ΕͳΒOK
  2. ͜ΕΛ࢖͏ͱ!(JavaͷίʔυΛKotlinͷίʔυ͔ΒݺͿ) val str = Utility.returnNullableStringJava() // ͜Ε΋OK // val str

    : String? = Utility.returnNullableStringJava() // ஫໨ʂʂʂ // ͜Ε΋OKʂʂ ʂ // val str : String = Utility.returnNullableStringJava()
  3. ʲ࠶ܝʳ͜ΕΛ࢖͏ͱ!(Kotlinͷίʔυ) val str = returnNullableStringKotlin() // ͜Ε΋OK // val str

    : String? = returnNullableStringKotlin() // ͜Ε͸ίϯύΠϧΤϥʔ Type Mismatch // val str : String = returnNullableStringKotlin() // println(str.length) // ίϯύΠϧΤϥʔ println(str?.length) // ͜ΕͳΒOK
  4. JavaͷίʔυΛKotlinͷίʔυ͔ΒݺΜͰ Stringʹ୅ೖ val str : String = Utility.returnNullableStringJava() println(str.length) //

    ίϯύΠϧΤϥʔʮͰ͸ʯͳ͍͚Ͳ // ͦΕඞཁͳ͍Αͬͯɺܯࠂ͕ग़Δ // println(str?.length)
  5. JavaͷίʔυΛKotlinͷίʔυ͔ΒݺΜͰ ܕΛ໌ࣔ͠ͳ͍ͱ val str = Utility.returnNullableStringJava() // ͜Ε΋OK println(str?.length) //

    ίϯύΠϧΤϥʔͰ͸ͳ͍͚ΕͲɺ // ࣮ߦ࣌ʹʮjava.lang.NullPointerExceptionʯ println(str.length)
  6. ʲ࠶ܝʳJavaͷίʔυΛKotlinͷίʔυ͔ΒݺΜͰ Stringʹ୅ೖ val str : String = Utility.returnNullableStringJava() println(str.length) //

    ίϯύΠϧΤϥʔʮͰ͸ʯͳ͍͚Ͳ // ͦΕඞཁͳ͍Αͬͯɺܯࠂ͕ग़Δ // println(str?.length)
  7. ʲ࠶ܝʳJavaͷίʔυΛKotlinͷίʔυ͔ΒݺΜͰ Stringʹ୅ೖ val str : String = Utility.returnNullableStringJava() println(str.length) //

    ίϯύΠϧΤϥʔʮͰ͸ʯͳ͍͚Ͳ ͜ͷByte%CodeΛΈͯΈΔ IDEAͰɺTools)>)Kotlin)>)Show)Kotlin)Bytecode
  8. 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. 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. @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
  11. @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) // ͜Ε͸ίϯύΠϧΤϥʔ
  12. ࣮͸ෳ਺ରԠ͍ͯ͠Δ • 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
  13. ࣮͸ෳ਺ରԠ͍ͯ͠Δʢଓ͖ʣ • 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
  14. 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") )
  15. 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