Use Intrinsics.checkNotNullExpressionValue to throw NPE in Java null checks

Similarly to previous commit, this method was unused since its
introduction before 1.0, so we're changing its semantics to throw NPE
and starting to use it with API version >= 1.4.

 #KT-22275 In Progress
This commit is contained in:
Alexander Udalov
2019-08-06 09:50:21 +02:00
parent a7c8fdcbe2
commit 480313210a
16 changed files with 93 additions and 13 deletions
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.codegen.intrinsics.HashCode;
import org.jetbrains.kotlin.codegen.intrinsics.IntrinsicMethods;
import org.jetbrains.kotlin.codegen.state.GenerationState;
import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper;
import org.jetbrains.kotlin.config.ApiVersion;
import org.jetbrains.kotlin.config.JvmTarget;
import org.jetbrains.kotlin.config.LanguageFeature;
import org.jetbrains.kotlin.config.LanguageVersionSettings;
@@ -1013,7 +1014,6 @@ public class AsmUtil {
if (runtimeAssertionInfo == null || !runtimeAssertionInfo.getNeedNotNullAssertion()) return stackValue;
return new StackValue(stackValue.type, stackValue.kotlinType) {
@Override
public void putSelector(@NotNull Type type, @Nullable KotlinType kotlinType, @NotNull InstructionAdapter v) {
Type innerType = stackValue.type;
@@ -1022,8 +1022,10 @@ public class AsmUtil {
if (innerType.getSort() == Type.OBJECT || innerType.getSort() == Type.ARRAY) {
v.dup();
v.visitLdcInsn(runtimeAssertionInfo.getMessage());
v.invokestatic(IntrinsicMethods.INTRINSICS_CLASS_NAME, "checkExpressionValueIsNotNull",
"(Ljava/lang/Object;Ljava/lang/String;)V", false);
String methodName = state.getLanguageVersionSettings().getApiVersion().compareTo(ApiVersion.KOTLIN_1_4) >= 0
? "checkNotNullExpressionValue"
: "checkExpressionValueIsNotNull";
v.invokestatic(IntrinsicMethods.INTRINSICS_CLASS_NAME, methodName, "(Ljava/lang/Object;Ljava/lang/String;)V", false);
}
StackValue.coerce(innerType, innerKotlinType, type, kotlinType, v);
}
@@ -311,7 +311,7 @@ class RedundantNullCheckMethodTransformer(private val generationState: Generatio
// ALOAD v
// DUP
// LDC *
// INVOKESTATIC checkExpressionValueIsNotNull
// INVOKESTATIC checkExpressionValueIsNotNull/checkNotNullExpressionValue
// <...> -- v is not null here (otherwise an exception was thrown)
methodNode.instructions.insert(insn, listOfSynthetics {
@@ -440,7 +440,7 @@ internal fun AbstractInsnNode.isCheckParameterIsNotNull() =
internal fun AbstractInsnNode.isCheckExpressionValueIsNotNull() =
isInsn<MethodInsnNode>(Opcodes.INVOKESTATIC) {
owner == IntrinsicMethods.INTRINSICS_CLASS_NAME &&
name == "checkExpressionValueIsNotNull" &&
(name == "checkExpressionValueIsNotNull" || name == "checkNotNullExpressionValue") &&
desc == "(Ljava/lang/Object;Ljava/lang/String;)V"
}