Generate not-null assertions after method calls
If a method comes from Java and is annotated as returning NotNull, after calling it we should check if it actually returned something other than null. Introduce checkReturnedValueIsNotNull() in jet/runtime/Intrinsics which does exactly that. CallableMethod's invoke() and invokeDefault() are now private, use asserted versions instead
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import jet.runtime.typeinfo.KotlinSignature;
|
||||
|
||||
public class A {
|
||||
@KotlinSignature("fun foo() : String")
|
||||
public String foo() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@KotlinSignature("fun staticFoo() : String")
|
||||
public static String staticFoo() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@KotlinSignature("fun plus(a: A) : A")
|
||||
public A plus(A a) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@KotlinSignature("fun inc() : A")
|
||||
public A inc() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@KotlinSignature("fun get(o: Any) : Any")
|
||||
public Object get(Object o) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
class AssertionChecker(val illegalStateExpected: Boolean) {
|
||||
fun invoke(name: String, f: () -> Unit) {
|
||||
try {
|
||||
f()
|
||||
} catch (e: IllegalStateException) {
|
||||
if (!illegalStateExpected) throw AssertionError("Unexpected IllegalStateException on calling $name")
|
||||
return
|
||||
}
|
||||
if (illegalStateExpected) throw AssertionError("IllegalStateException expected on calling $name")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
trait Tr {
|
||||
fun foo(): String
|
||||
}
|
||||
|
||||
class Derived : A(), Tr {
|
||||
override fun foo() = super<A>.foo()
|
||||
}
|
||||
|
||||
class Delegated : Tr by Derived() {
|
||||
}
|
||||
|
||||
|
||||
fun checkAssertions(val illegalStateExpected: Boolean) {
|
||||
val check = AssertionChecker(illegalStateExpected)
|
||||
|
||||
// simple call
|
||||
check("foo") { A().foo() }
|
||||
|
||||
// simple static call
|
||||
check("staticFoo") { A.staticFoo() }
|
||||
|
||||
// supercall
|
||||
check("foo") { Derived().foo() }
|
||||
|
||||
// delegated call
|
||||
check("foo") { Delegated().foo() }
|
||||
|
||||
// collection element
|
||||
check("get") { A()[""] }
|
||||
|
||||
// binary expression
|
||||
check("plus") { A() + A() }
|
||||
|
||||
// postfix expression
|
||||
check("inc") { var a = A(); a++ }
|
||||
|
||||
// prefix expression
|
||||
check("inc") { var a = A(); ++a }
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun box(): String {
|
||||
checkAssertions(true)
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun box(): String {
|
||||
checkAssertions(false)
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user