Provide specialized stdlib function implementations depending on current JRE version #KT-8254
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
@file:JvmVersion
|
||||
package kotlin.internal
|
||||
|
||||
import java.io.Closeable
|
||||
|
||||
internal open class PlatformImplementations {
|
||||
|
||||
public open fun closeSuppressed(instance: Closeable, cause: Throwable) {
|
||||
try {
|
||||
instance.close()
|
||||
} catch (closeException: Throwable) {
|
||||
// eat the closeException as we are already throwing the original cause
|
||||
// and we don't want to mask the real exception;
|
||||
// on Java 7 we should call
|
||||
// e.addSuppressed(closeException)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
@InlineExposed
|
||||
val INSTANCE: PlatformImplementations = run {
|
||||
val version = getJavaVersion()
|
||||
try {
|
||||
if (version >= 0x10008)
|
||||
return@run Class.forName("kotlin.internal.JDK8PlatformImplementations").newInstance() as PlatformImplementations
|
||||
} catch (e: ClassNotFoundException) {}
|
||||
|
||||
try {
|
||||
if (version >= 0x10007)
|
||||
return@run Class.forName("kotlin.internal.JDK7PlatformImplementations").newInstance() as PlatformImplementations
|
||||
} catch (e: ClassNotFoundException) {}
|
||||
|
||||
PlatformImplementations()
|
||||
}
|
||||
|
||||
private fun getJavaVersion(): Int {
|
||||
val default = 0x10006
|
||||
val version = System.getProperty("java.version") ?: return default
|
||||
val firstDot = version.indexOf('.')
|
||||
if (firstDot < 0) return default
|
||||
var secondDot = version.indexOf('.', firstDot + 1)
|
||||
if (secondDot < 0) secondDot = version.length
|
||||
|
||||
val firstPart = version.substring(0, firstDot)
|
||||
val secondPart = version.substring(firstDot + 1, secondDot)
|
||||
return try {
|
||||
firstPart.toInt() * 0x10000 + secondPart.toInt()
|
||||
} catch (e: NumberFormatException) {
|
||||
default
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.io.*
|
||||
import java.nio.charset.Charset
|
||||
import java.net.URL
|
||||
import java.util.NoSuchElementException
|
||||
import kotlin.internal.PlatformImplementations
|
||||
|
||||
|
||||
/** Returns a buffered reader wrapping this Reader, or this Reader itself if it is already buffered. */
|
||||
@@ -155,19 +156,10 @@ public inline fun <T : Closeable, R> T.use(block: (T) -> R): R {
|
||||
var closed = false
|
||||
try {
|
||||
return block(this)
|
||||
} catch (e: Exception) {
|
||||
} catch (e: Throwable) {
|
||||
closed = true
|
||||
try {
|
||||
close()
|
||||
} catch (closeException: Exception) {
|
||||
// eat the closeException as we are already throwing the original cause
|
||||
// and we don't want to mask the real exception
|
||||
|
||||
// TODO on Java 7 we should call
|
||||
// e.addSuppressed(closeException)
|
||||
// to work like try-with-resources
|
||||
// http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html#suppressed-exceptions
|
||||
}
|
||||
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
|
||||
PlatformImplementations.INSTANCE.closeSuppressed(this, e)
|
||||
throw e
|
||||
} finally {
|
||||
if (!closed) {
|
||||
|
||||
Reference in New Issue
Block a user