Move more logic of 'use' into 'closeSuppressed', rename the latter to 'closeFinally', that will result in more compact inline call expansions.
This commit is contained in:
@@ -31,31 +31,33 @@ package kotlin
|
|||||||
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
|
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
public inline fun <T : AutoCloseable?, R> T.use(block: (T) -> R): R {
|
public inline fun <T : AutoCloseable?, R> T.use(block: (T) -> R): R {
|
||||||
var closed = false
|
var exception: Throwable? = null
|
||||||
try {
|
try {
|
||||||
return block(this)
|
return block(this)
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
closed = true
|
exception = e
|
||||||
this?.closeSuppressed(e)
|
|
||||||
throw e
|
throw e
|
||||||
} finally {
|
} finally {
|
||||||
if (this != null && !closed) {
|
this.closeFinally(exception)
|
||||||
close()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes this [AutoCloseable] suppressing possible exception or error thrown by [AutoCloseable.close] function.
|
* Closes this [AutoCloseable], suppressing possible exception or error thrown by [AutoCloseable.close] function when
|
||||||
|
* it's being closed due to some other [cause] exception occurred.
|
||||||
|
*
|
||||||
* The suppressed exception is added to the list of suppressed exceptions of [cause] exception.
|
* The suppressed exception is added to the list of suppressed exceptions of [cause] exception.
|
||||||
*/
|
*/
|
||||||
@SinceKotlin("1.1")
|
@SinceKotlin("1.1")
|
||||||
@PublishedApi
|
@PublishedApi
|
||||||
internal fun AutoCloseable.closeSuppressed(cause: Throwable) {
|
internal fun AutoCloseable?.closeFinally(cause: Throwable?) = when {
|
||||||
try {
|
this == null -> {}
|
||||||
close()
|
cause == null -> close()
|
||||||
} catch (closeException: Throwable) {
|
else ->
|
||||||
cause.addSuppressed(closeException)
|
try {
|
||||||
}
|
close()
|
||||||
|
} catch (closeException: Throwable) {
|
||||||
|
cause.addSuppressed(closeException)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,5 +6,5 @@ package kotlin
|
|||||||
@PublishedApi
|
@PublishedApi
|
||||||
@Deprecated("Provided for binary compatibility")
|
@Deprecated("Provided for binary compatibility")
|
||||||
@JvmName("closeSuppressed")
|
@JvmName("closeSuppressed")
|
||||||
internal fun AutoCloseable.closeSuppressedDeprecated(cause: Throwable) = closeSuppressed(cause)
|
internal fun AutoCloseable.closeSuppressedDeprecated(cause: Throwable) = closeFinally(cause)
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ internal open class PlatformImplementations {
|
|||||||
@SinceKotlin("1.1")
|
@SinceKotlin("1.1")
|
||||||
@PublishedApi
|
@PublishedApi
|
||||||
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
|
@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
|
||||||
internal fun platformCloseSuppressed(instance: Closeable, cause: Throwable) = instance.closeSuppressed(cause)
|
internal fun platformCloseSuppressed(instance: Closeable, cause: Throwable) = instance.closeFinally(cause)
|
||||||
|
|
||||||
|
|
||||||
@JvmField
|
@JvmField
|
||||||
|
|||||||
@@ -30,27 +30,32 @@ import kotlin.internal.*
|
|||||||
*/
|
*/
|
||||||
@InlineOnly
|
@InlineOnly
|
||||||
public inline fun <T : Closeable?, R> T.use(block: (T) -> R): R {
|
public inline fun <T : Closeable?, R> T.use(block: (T) -> R): R {
|
||||||
var closed = false
|
var exception: Throwable? = null
|
||||||
try {
|
try {
|
||||||
return block(this)
|
return block(this)
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
closed = true
|
exception = e
|
||||||
this?.closeSuppressed(e)
|
|
||||||
throw e
|
throw e
|
||||||
} finally {
|
} finally {
|
||||||
if (this != null && !closed) {
|
this.closeFinally(exception)
|
||||||
close()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes this [Closeable], suppressing possible exception or error thrown by [Closeable.close] function when
|
||||||
|
* it's being closed due to some other [cause] exception occurred.
|
||||||
|
*
|
||||||
|
* The suppressed exception is added to the list of suppressed exceptions of [cause] exception, when it's supported.
|
||||||
|
*/
|
||||||
@SinceKotlin("1.1")
|
@SinceKotlin("1.1")
|
||||||
@PublishedApi
|
@PublishedApi
|
||||||
internal fun Closeable.closeSuppressed(cause: Throwable) {
|
internal fun Closeable?.closeFinally(cause: Throwable?) = when {
|
||||||
try {
|
this == null -> {}
|
||||||
close()
|
cause == null -> close()
|
||||||
} catch (closeException: Throwable) {
|
else ->
|
||||||
// on Java 7 we should call
|
try {
|
||||||
IMPLEMENTATIONS.addSuppressed(cause, closeException)
|
close()
|
||||||
}
|
} catch (closeException: Throwable) {
|
||||||
|
cause.addSuppressed(closeException)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+1
-1
@@ -1758,7 +1758,7 @@ public final class kotlin/io/ByteStreamsKt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public final class kotlin/io/CloseableKt {
|
public final class kotlin/io/CloseableKt {
|
||||||
public static final fun closeSuppressed (Ljava/io/Closeable;Ljava/lang/Throwable;)V
|
public static final fun closeFinally (Ljava/io/Closeable;Ljava/lang/Throwable;)V
|
||||||
}
|
}
|
||||||
|
|
||||||
public final class kotlin/io/ConsoleKt {
|
public final class kotlin/io/ConsoleKt {
|
||||||
|
|||||||
Reference in New Issue
Block a user