Use Intrinsics.checkNotNullParameter to throw NPE in parameter null checks

Similarly to previous commits, this method was unused, so we're changing
its semantics in API version >= 1.4.

 #KT-22275 In Progress
This commit is contained in:
Alexander Udalov
2019-08-06 10:56:44 +02:00
parent 480313210a
commit 2baddb029c
23 changed files with 173 additions and 16 deletions
@@ -48,8 +48,8 @@ class A : MutableMap<Any, Any> {
}
override fun getOrDefault(key: Any, defaultValue: Any): Any {
// this condition can not be true because of checkParameterIsNotNull checks in the begin of every method, but it's left here
// to emphasize that we expect these parameters are not null
// this condition can not be true because of checkParameterIsNotNull/checkNotNullParameter checks in the begin of every method,
// but it's left here to emphasize that we expect these parameters are not null
if (key == null || defaultValue == null) {
throw IllegalArgumentException("fail")
}
@@ -0,0 +1,25 @@
// !API_VERSION: LATEST
// TARGET_BACKEND: JVM
// FILE: A.java
public class A {
public static void test() {
new B().foo(null);
}
}
// FILE: test.kt
class B {
fun foo(s: String) {}
}
fun box(): String {
try {
A.test()
return "Fail: NPE should have been thrown"
} catch (e: Throwable) {
if (e::class != NullPointerException::class) return "Fail: exception class should be NPE: ${e::class}"
return "OK"
}
}
@@ -0,0 +1,16 @@
// !API_VERSION: LATEST
// FILE: 1.kt
package test
public inline fun <R> doRun(block: () -> R): R {
return block()
}
// FILE: 2.kt
import test.*
fun box(): String {
return doRun { "OK" }
}
@@ -13,4 +13,5 @@ fun AsNonNullReference.nonNullReferenceExtension(b1: AsNonNullReference) {} // 2
fun asNullablePrimitive(c: AsNonNullPrimitive?) {}
fun asNullableReference(c: AsNonNullReference?) {}
// 8 checkParameterIsNotNull
// 8 checkParameterIsNotNull
// 0 checkNotNullParameter
@@ -5,4 +5,5 @@ inline class AsAny(val a: Any?)
fun asNotNullAny(a: AsAny) {}
fun AsAny.asNotNullAnyExtension(b: AsAny): AsAny = this
// 0 checkParameterIsNotNull
// 0 checkParameterIsNotNull
// 0 checkNotNullParameter
+4
View File
@@ -32,12 +32,16 @@ class TestAccessor {
// @TestMethod.class:
// 0 INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull \(Ljava/lang/Object;Ljava/lang/String;\)V
// 0 INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkNotNullParameter \(Ljava/lang/Object;Ljava/lang/String;\)V
// @TestMethodOverloads.class:
// 0 INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull \(Ljava/lang/Object;Ljava/lang/String;\)V
// 0 INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkNotNullParameter \(Ljava/lang/Object;Ljava/lang/String;\)V
// @TestProperty.class:
// 0 INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull \(Ljava/lang/Object;Ljava/lang/String;\)V
// 0 INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkNotNullParameter \(Ljava/lang/Object;Ljava/lang/String;\)V
// @TestAccessor.class:
// 0 INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull \(Ljava/lang/Object;Ljava/lang/String;\)V
// 0 INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkNotNullParameter \(Ljava/lang/Object;Ljava/lang/String;\)V
@@ -13,3 +13,4 @@ fun bar(a: ArrayList<String>) {
// 1 checkExpressionValueIsNotNull
// 0 checkNotNullExpressionValue
// 1 checkParameterIsNotNull
// 0 checkNotNullParameter
@@ -1,3 +1,4 @@
fun <T : Any> foo(t: T) = t
// 1 checkParameterIsNotNull
// 0 checkNotNullParameter
@@ -0,0 +1,6 @@
// !API_VERSION: LATEST
fun <T : Any> foo(t: T) = t
// 0 checkParameterIsNotNull
// 1 checkNotNullParameter
@@ -37,3 +37,4 @@ internal fun bar(a: A<String, Int>, b: B<String>, c: C) {
// 3 checkExpressionValueIsNotNull
// 0 checkNotNullExpressionValue
// 3 checkParameterIsNotNull
// 0 checkNotNullParameter
@@ -0,0 +1,8 @@
// !API_VERSION: LATEST
fun test(s: String) = s?.length
// 0 IFNULL
// 0 IFNONNULL
// 0 intValue
// 0 valueOf