Keep exception primary constructors for binary compatibility in JS

Resort to a workaround for KT-22053 in direct Throwable inheritors.
This commit is contained in:
Ilya Gorbunov
2017-12-29 06:34:35 +03:00
parent 3825187e93
commit e2306ecf94
2 changed files with 91 additions and 61 deletions
+82 -61
View File
@@ -16,96 +16,117 @@
package kotlin package kotlin
public open class Error : Throwable { // NOTE: Do not author your exceptions as they are written in this file, instead use this template:
/*
public open class MyException : Exception {
constructor() : super() constructor() : super()
constructor(message: String?) : super(message) constructor(message: String?) : super(message)
constructor(message: String?, cause: Throwable?): super(message, cause) constructor(message: String?, cause: Throwable?) : super(message, cause)
constructor(cause: Throwable?) : super(cause) constructor(cause: Throwable?) : super(cause)
} }
*/
public open class Exception : Throwable {
constructor() : super() // TODO: remove workarounds for KT-22053 from direct Throwable inheritors
constructor(message: String?) : super(message) // TODO: remove primary constructors, make all secondary KT-22055
constructor(message: String?, cause: Throwable?): super(message, cause)
constructor(cause: Throwable?) : super(cause) @Suppress("USELESS_ELVIS_RIGHT_IS_NULL")
public open class Error(message: String?, cause: Throwable?) : Throwable(message, cause ?: null) {
constructor() : this(null, null) {
Error::class.js.asDynamic().call(this, null, null)
}
constructor(message: String?) : this(message, null) {
Error::class.js.asDynamic().call(this, message, null)
}
constructor(cause: Throwable?) : this(undefined, cause) {
Error::class.js.asDynamic().call(this, undefined, cause)
}
} }
public open class RuntimeException : Exception { @Suppress("USELESS_ELVIS_RIGHT_IS_NULL")
constructor() : super() public open class Exception(message: String?, cause: Throwable?) : Throwable(message, cause ?: null) {
constructor(message: String?) : super(message) constructor() : this(null, null) {
constructor(message: String?, cause: Throwable?): super(message, cause) Exception::class.js.asDynamic().call(this, null, null)
constructor(cause: Throwable?) : super(cause) }
constructor(message: String?) : this(message, null) {
Exception::class.js.asDynamic().call(this, message, null)
}
constructor(cause: Throwable?) : this(undefined, cause) {
Exception::class.js.asDynamic().call(this, undefined, cause)
}
} }
public open class IllegalArgumentException : RuntimeException { public open class RuntimeException(message: String?, cause: Throwable?) : Exception(message, cause) {
constructor() : super() constructor() : this(null, null)
constructor(message: String?) : super(message) constructor(message: String?) : this(message, null)
constructor(message: String?, cause: Throwable?): super(message, cause) constructor(cause: Throwable?) : this(undefined, cause)
constructor(cause: Throwable?) : super(cause)
} }
public open class IllegalStateException : RuntimeException { public open class IllegalArgumentException(message: String?, cause: Throwable?) : RuntimeException(message, cause) {
constructor() : super() constructor() : this(null, null)
constructor(message: String?) : super(message) constructor(message: String?) : this(message, null)
constructor(message: String?, cause: Throwable?): super(message, cause) constructor(cause: Throwable?) : this(undefined, cause)
constructor(cause: Throwable?) : super(cause)
} }
public open class IndexOutOfBoundsException : RuntimeException { public open class IllegalStateException(message: String?, cause: Throwable?) : RuntimeException(message, cause) {
constructor() : super() constructor() : this(null, null)
constructor(message: String?) : super(message) constructor(message: String?) : this(message, null)
constructor(cause: Throwable?) : this(undefined, cause)
} }
public open class ConcurrentModificationException : RuntimeException { public open class IndexOutOfBoundsException(message: String?) : RuntimeException(message) {
constructor() : super() constructor() : this(null)
constructor(message: String?) : super(message)
constructor(message: String?, cause: Throwable?): super(message, cause)
constructor(cause: Throwable?) : super(cause)
} }
public open class UnsupportedOperationException : RuntimeException { public open class ConcurrentModificationException(message: String?, cause: Throwable?) : RuntimeException(message, cause) {
constructor() : super() constructor() : this(null, null)
constructor(message: String?) : super(message) constructor(message: String?) : this(message, null)
constructor(message: String?, cause: Throwable?): super(message, cause) constructor(cause: Throwable?) : this(undefined, cause)
constructor(cause: Throwable?) : super(cause)
} }
public open class NumberFormatException : IllegalArgumentException { public open class UnsupportedOperationException(message: String?, cause: Throwable?) : RuntimeException(message, cause) {
constructor() : super() constructor() : this(null, null)
constructor(message: String?) : super(message) constructor(message: String?) : this(message, null)
constructor(cause: Throwable?) : this(undefined, cause)
} }
public open class NullPointerException : RuntimeException {
constructor() : super() public open class NumberFormatException(message: String?) : IllegalArgumentException(message) {
constructor(message: String?) : super(message) constructor() : this(null)
} }
public open class ClassCastException : RuntimeException {
constructor() : super() public open class NullPointerException(message: String?) : RuntimeException(message) {
constructor(message: String?) : super(message) constructor() : this(null)
} }
public open class AssertionError : Error { public open class ClassCastException(message: String?) : RuntimeException(message) {
constructor() : super() constructor() : this(null)
constructor(message: String?) : super(message)
constructor(message: Any?) : super(message.toString(), message as? Throwable)
} }
public open class NoSuchElementException : Exception { public open class AssertionError private constructor(message: String?, cause: Throwable?) : Error(message, cause) {
constructor() : super() constructor() : this(null)
constructor(message: String?) : super(message) constructor(message: String?) : this(message, null)
constructor(message: Any?) : this(message.toString(), message as? Throwable)
} }
public open class NoWhenBranchMatchedException : RuntimeException { public open class NoSuchElementException(message: String?) : RuntimeException(message) {
constructor() : super() constructor() : this(null)
constructor(message: String?) : super(message)
constructor(message: String?, cause: Throwable?): super(message, cause)
constructor(cause: Throwable?) : super(cause)
} }
public open class UninitializedPropertyAccessException : RuntimeException {
constructor() : super() public open class NoWhenBranchMatchedException(message: String?, cause: Throwable?) : RuntimeException(message, cause) {
constructor(message: String?) : super(message) constructor() : this(null, null)
constructor(message: String?, cause: Throwable?): super(message, cause) constructor(message: String?) : this(message, null)
constructor(cause: Throwable?) : super(cause) constructor(cause: Throwable?) : this(undefined, cause)
}
public open class UninitializedPropertyAccessException(message: String?, cause: Throwable?) : RuntimeException(message, cause) {
constructor() : this(null, null)
constructor(message: String?) : this(message, null)
constructor(cause: Throwable?) : this(undefined, cause)
} }
@@ -3,6 +3,9 @@ package foo
class MyException(m: String? = null): Exception(m) class MyException(m: String? = null): Exception(m)
class MyException2(m: String? = null): Throwable(m) class MyException2(m: String? = null): Throwable(m)
// TODO: add direct inheritors of Throwable:
// - with secondary constructors
// - with cause only, in the primary constructor
fun check(e: Throwable, expectedString: String) { fun check(e: Throwable, expectedString: String) {
try { try {
@@ -16,8 +19,14 @@ fun check(e: Throwable, expectedString: String) {
fun box(): String { fun box(): String {
check(Throwable(), "Throwable: null") check(Throwable(), "Throwable: null")
check(Throwable("ccc"), "Throwable: ccc") check(Throwable("ccc"), "Throwable: ccc")
check(Throwable(Throwable("ddd")), "Throwable: Throwable: ddd")
check(Exception(), "Exception: null") check(Exception(), "Exception: null")
check(Exception("bbb"), "Exception: bbb") check(Exception("bbb"), "Exception: bbb")
check(Exception(Exception("ccc")), "Exception: Exception: ccc")
check(AssertionError(), "AssertionError: null")
check(AssertionError(null), "AssertionError: null")
check(AssertionError("bbb"), "AssertionError: bbb")
check(AssertionError(Exception("ccc")), "AssertionError: Exception: ccc")
check(MyException(), "MyException: null") check(MyException(), "MyException: null")
check(MyException("aaa"), "MyException: aaa") check(MyException("aaa"), "MyException: aaa")
check(MyException2(), "MyException2: null") check(MyException2(), "MyException2: null")