Make Closeable.use call addSuppressed

Call addSuppressed when it's provided by the supplementary artifact for jdk7, and only when targeting apiVersion > 1.1

#KT-18961 Fixed
This commit is contained in:
Ilya Gorbunov
2017-09-26 21:00:37 +03:00
parent 3ab7946c4d
commit cc150ca832
3 changed files with 14 additions and 14 deletions
@@ -60,7 +60,6 @@ class TryWithResourcesCloseableTest {
assertTrue(e.suppressed.isEmpty()) assertTrue(e.suppressed.isEmpty())
} }
@Ignore
@Test fun opFailsCloseFails() { @Test fun opFailsCloseFails() {
val e = assertFails { val e = assertFails {
Resource(faultyClose = true).use { error("op fail") } Resource(faultyClose = true).use { error("op fail") }
@@ -69,7 +68,6 @@ class TryWithResourcesCloseableTest {
assertTrue(e.suppressed.single() is IOException) assertTrue(e.suppressed.single() is IOException)
} }
@Ignore
@Test fun opFailsCloseFailsTwice() { @Test fun opFailsCloseFailsTwice() {
val e = assertFails { val e = assertFails {
Resource(faultyClose = true).use { _ -> Resource(faultyClose = true).use { _ ->
@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package kotlin.jdk7.test package test.jdk7
import org.junit.Ignore import org.junit.Ignore
import java.io.* import java.io.*
@@ -60,7 +60,6 @@ class TryWithResourcesCloseableTest {
assertTrue(e.suppressed.isEmpty()) assertTrue(e.suppressed.isEmpty())
} }
@Ignore
@Test fun opFailsCloseFails() { @Test fun opFailsCloseFails() {
val e = assertFails { val e = assertFails {
Resource(faultyClose = true).use { error("op fail") } Resource(faultyClose = true).use { error("op fail") }
@@ -69,7 +68,6 @@ class TryWithResourcesCloseableTest {
assertTrue(e.suppressed.single() is IOException) assertTrue(e.suppressed.single() is IOException)
} }
@Ignore
@Test fun opFailsCloseFailsTwice() { @Test fun opFailsCloseFailsTwice() {
val e = assertFails { val e = assertFails {
Resource(faultyClose = true).use { _ -> Resource(faultyClose = true).use { _ ->
+13 -9
View File
@@ -30,19 +30,23 @@ 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: Exception) { } catch (e: Throwable) {
closed = true exception = e
try {
this?.close()
} catch (closeException: Exception) {
}
throw e throw e
} finally { } finally {
if (!closed) { when {
this?.close() apiVersionIsAtLeast(1, 1, 0) -> this.closeFinally(exception)
this == null -> {}
exception == null -> close()
else ->
try {
close()
} catch (closeException: Throwable) {
// cause.addSuppressed(closeException) // ignored here
}
} }
} }
} }