Create custom exception for lateinit

This commit is contained in:
Yan Zhulanow
2015-09-04 17:13:55 +03:00
parent fc3bf3cca4
commit 4ca127ecb3
8 changed files with 38 additions and 7 deletions
@@ -1042,7 +1042,8 @@ public abstract class StackValue {
v.dup();
Label ok = new Label();
v.ifnonnull(ok);
v.invokestatic(IntrinsicMethods.INTRINSICS_CLASS_NAME, "throwNpe", "()V", false);
v.visitLdcInsn(descriptor.getName().asString());
v.invokestatic(IntrinsicMethods.INTRINSICS_CLASS_NAME, "throwUninitializedPropertyAccessException", "(Ljava/lang/String;)V", false);
v.mark(ok);
}
@@ -3,7 +3,7 @@ public class A {
fun getMyStr(): String {
try {
val a = str
} catch (e: NullPointerException) {
} catch (e: RuntimeException) {
return "OK"
}
@@ -4,7 +4,7 @@ class A {
public fun getMyStr(): String {
try {
val a = str
} catch (e: NullPointerException) {
} catch (e: RuntimeException) {
return "OK"
}
@@ -6,7 +6,7 @@ fun box(): String {
val a = A()
try {
a.str
} catch (e: NullPointerException) {
} catch (e: RuntimeException) {
return "OK"
}
return "FAIL"
@@ -8,7 +8,7 @@ class A : Intf {
fun getMyStr(): String {
try {
val a = str
} catch (e: NullPointerException) {
} catch (e: RuntimeException) {
return "OK"
}
return "FAIL"
@@ -13,9 +13,15 @@ public class A {
if (!Modifier.isProtected(clazz.getDeclaredField("protectedField").modifiers)) cond += "NOT_PROTECTED"
if (!Modifier.isPublic(clazz.getDeclaredField("publicField").modifiers)) cond += "NOT_PUBLIC"
if (Modifier.isPrivate(clazz.getDeclaredField("privateField").modifiers)) cond += "FINAL"
if (Modifier.isFinal(clazz.getDeclaredField("privateField").modifiers)) cond += "FINAL"
return if (cond.isEmpty()) "OK" else cond.joinToString()
try {
val a = privateField
} catch (e: UninitializedPropertyAccessException) {
return if (cond.isEmpty()) "OK" else cond.joinToString()
}
return "EXCEPTION WAS NOT CATCHED"
}
}
@@ -0,0 +1,19 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin
public class UninitializedPropertyAccessException(name: String): RuntimeException("lateinit property $name has not been initialized")
@@ -17,6 +17,7 @@
package kotlin.jvm.internal;
import kotlin.KotlinNullPointerException;
import kotlin.UninitializedPropertyAccessException;
import java.util.Arrays;
import java.util.List;
@@ -34,6 +35,10 @@ public class Intrinsics {
throw sanitizeStackTrace(new KotlinNullPointerException());
}
public static void throwUninitializedPropertyAccessException(String propertyName) {
throw sanitizeStackTrace(new UninitializedPropertyAccessException(propertyName));
}
public static void checkExpressionValueIsNotNull(Object value, String message) {
if (value == null) {
IllegalStateException exception = new IllegalStateException(message + " must not be null");