Refactor exception suppression to provide Throwable.addSuppressed extension in kotlin-stdlib.
Place Closeable and AutoCloseable extensions to separate files (CloseableKt and AutoCloseableKt). #KT-15477 Fixed
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.
|
||||
*/
|
||||
|
||||
@file:JvmName("AutoCloseableKt")
|
||||
package kotlin
|
||||
|
||||
/**
|
||||
* Executes the given [block] function on this resource and then closes it down correctly whether an exception
|
||||
* is thrown or not.
|
||||
*
|
||||
* In case if the resource is being closed due to an exception occurred in [block], and the closing also fails with an exception,
|
||||
* the latter is added to the [suppressed][java.lang.Throwable.addSuppressed] exceptions of the former.
|
||||
*
|
||||
* @param block a function to process this [AutoCloseable] resource.
|
||||
* @return the result of [block] function invoked on this resource.
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T : AutoCloseable?, R> T.use(block: (T) -> R): R {
|
||||
var closed = false
|
||||
try {
|
||||
return block(this)
|
||||
} catch (e: Throwable) {
|
||||
closed = true
|
||||
this?.closeSuppressed(e)
|
||||
throw e
|
||||
} finally {
|
||||
if (this != null && !closed) {
|
||||
close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes this [AutoCloseable] suppressing possible exception or error thrown by [AutoCloseable.close] function.
|
||||
* The suppressed exception is added to the list of suppressed exceptions of [cause] exception.
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
@PublishedApi
|
||||
internal fun AutoCloseable.closeSuppressed(cause: Throwable) {
|
||||
try {
|
||||
close()
|
||||
} catch (closeException: Throwable) {
|
||||
cause.addSuppressed(closeException)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +1,10 @@
|
||||
@file:JvmName("StandardJRE7Kt")
|
||||
package kotlin
|
||||
|
||||
/**
|
||||
* Executes the given [block] function on this resource and then closes it down correctly whether an exception
|
||||
* is thrown or not.
|
||||
*
|
||||
* In case if the resource is being closed due to an exception occurred in [block], and the closing also fails with an exception,
|
||||
* the latter is added to the [suppressed][java.lang.Throwable.addSuppressed] exceptions of the former.
|
||||
*
|
||||
* @param block a function to process this [AutoCloseable] resource.
|
||||
* @return the result of [block] function invoked on this resource.
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T : AutoCloseable?, R> T.use(block: (T) -> R): R {
|
||||
var closed = false
|
||||
try {
|
||||
return block(this)
|
||||
} catch (e: Throwable) {
|
||||
closed = true
|
||||
this?.closeSuppressed(e)
|
||||
throw e
|
||||
} finally {
|
||||
if (this != null && !closed) {
|
||||
close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes this [AutoCloseable] suppressing possible exception or error thrown by [AutoCloseable.close] function.
|
||||
* The suppressed exception is added to the list of suppressed exceptions of [cause] exception.
|
||||
*/
|
||||
// TODO: Drop before 1.1
|
||||
@SinceKotlin("1.1")
|
||||
@PublishedApi
|
||||
internal fun AutoCloseable.closeSuppressed(cause: Throwable) {
|
||||
try {
|
||||
close()
|
||||
} catch (closeException: Throwable) {
|
||||
cause.addSuppressed(closeException)
|
||||
}
|
||||
}
|
||||
@Deprecated("Provided for binary compatibility")
|
||||
@JvmName("closeSuppressed")
|
||||
internal fun AutoCloseable.closeSuppressedDeprecated(cause: Throwable) = closeSuppressed(cause)
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package kotlin.internal
|
||||
|
||||
import java.io.Closeable
|
||||
|
||||
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "CANNOT_OVERRIDE_INVISIBLE_MEMBER")
|
||||
internal open class JRE7PlatformImplementations : PlatformImplementations() {
|
||||
override fun closeSuppressed(instance: Closeable, cause: Throwable) = instance.closeSuppressed(cause)
|
||||
|
||||
override fun addSuppressed(cause: Throwable, exception: Throwable) = cause.addSuppressed(exception)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user