Take java 9 modules into account in coroutines debug metadata
reading #KT-28237
This commit is contained in:
@@ -0,0 +1 @@
|
||||
usage/some.module.withsome.packages.Test
|
||||
@@ -0,0 +1 @@
|
||||
OK
|
||||
@@ -0,0 +1,3 @@
|
||||
module usage {
|
||||
requires kotlin.stdlib;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "CANNOT_OVERRIDE_INVISIBLE_MEMBER")
|
||||
package some.module.withsome.packages
|
||||
|
||||
import java.lang.RuntimeException
|
||||
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn
|
||||
import kotlin.coroutines.jvm.internal.BaseContinuationImpl
|
||||
|
||||
suspend fun dummy() {}
|
||||
|
||||
class Test {
|
||||
suspend fun getStackTraceElement(): StackTraceElement {
|
||||
dummy() // to force state-machine generation
|
||||
return suspendCoroutineUninterceptedOrReturn<StackTraceElement> {
|
||||
(it as BaseContinuationImpl).getStackTraceElement()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun main() {
|
||||
println(Test().getStackTraceElement().className)
|
||||
}
|
||||
@@ -1,26 +1,17 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.jvm.compiler
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil
|
||||
import junit.framework.TestCase
|
||||
import org.jetbrains.kotlin.cli.AbstractCliTest
|
||||
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import java.io.File
|
||||
import java.util.concurrent.TimeUnit
|
||||
import java.util.jar.Manifest
|
||||
|
||||
class Java9ModulesIntegrationTest : AbstractKotlinCompilerIntegrationTest() {
|
||||
@@ -251,4 +242,21 @@ class Java9ModulesIntegrationTest : AbstractKotlinCompilerIntegrationTest() {
|
||||
)
|
||||
module("usage", additionalKotlinArguments = listOf("-no-stdlib", buildFile))
|
||||
}
|
||||
|
||||
fun testCoroutinesDebugMetadata() {
|
||||
val jar = module("usage", listOf(ForTestCompileRuntime.runtimeJarForTests()))
|
||||
|
||||
val command = listOf<String>(
|
||||
File(KotlinTestUtils.getJdk9Home(), "bin/java").path,
|
||||
"-p",
|
||||
"${ForTestCompileRuntime.runtimeJarForTests().path}:${jar.path}",
|
||||
"-m",
|
||||
"usage/some.module.withsome.packages.UsageKt"
|
||||
)
|
||||
|
||||
val process = ProcessBuilder().command(command).start()
|
||||
process.waitFor(1, TimeUnit.MINUTES)
|
||||
val got = process.inputStream.reader().readText()
|
||||
KotlinTestUtils.assertEqualsToFile(File("$testDataDirectory/stdout.txt"), got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
|
||||
package kotlin.coroutines.jvm.internal
|
||||
|
||||
import java.lang.reflect.Method
|
||||
|
||||
@Target(AnnotationTarget.CLASS)
|
||||
@SinceKotlin("1.3")
|
||||
internal annotation class DebugMetadata(
|
||||
@@ -41,7 +43,48 @@ internal fun BaseContinuationImpl.getStackTraceElementImpl(): StackTraceElement?
|
||||
checkDebugMetadataVersion(COROUTINES_DEBUG_METADATA_VERSION, debugMetadata.version)
|
||||
val label = getLabel()
|
||||
val lineNumber = if (label < 0) -1 else debugMetadata.lineNumbers[label]
|
||||
return StackTraceElement(debugMetadata.className, debugMetadata.methodName, debugMetadata.sourceFile, lineNumber)
|
||||
val moduleName = ModuleNameRetriever.getModuleName(this)
|
||||
val moduleAndClass = if (moduleName == null) debugMetadata.className else "$moduleName/${debugMetadata.className}"
|
||||
return StackTraceElement(moduleAndClass, debugMetadata.methodName, debugMetadata.sourceFile, lineNumber)
|
||||
}
|
||||
|
||||
private object ModuleNameRetriever {
|
||||
private class Cache(
|
||||
@JvmField
|
||||
val getModuleMethod: Method?,
|
||||
@JvmField
|
||||
val getDescriptorMethod: Method?,
|
||||
@JvmField
|
||||
val nameMethod: Method?
|
||||
)
|
||||
|
||||
private val notOnJava9 = Cache(null, null, null)
|
||||
|
||||
@JvmField
|
||||
var cache: Cache? = null
|
||||
|
||||
fun getModuleName(continuation: BaseContinuationImpl): String? {
|
||||
val cache = this.cache ?: buildCache(continuation)
|
||||
if (cache === notOnJava9) {
|
||||
return null
|
||||
}
|
||||
val module = cache.getModuleMethod?.invoke(continuation.javaClass) ?: return null
|
||||
val descriptor = cache.getDescriptorMethod?.invoke(module) ?: return null
|
||||
return cache.nameMethod?.invoke(descriptor) as? String
|
||||
}
|
||||
|
||||
private fun buildCache(continuation: BaseContinuationImpl): Cache {
|
||||
try {
|
||||
val getModuleMethod = Class::class.java.getDeclaredMethod("getModule")
|
||||
val methodClass = continuation.javaClass.classLoader.loadClass("java.lang.Module")
|
||||
val getDescriptorMethod = methodClass.getDeclaredMethod("getDescriptor")
|
||||
val moduleDescriptorClass = continuation.javaClass.classLoader.loadClass("java.lang.module.ModuleDescriptor")
|
||||
val nameMethod = moduleDescriptorClass.getDeclaredMethod("name")
|
||||
return Cache(getModuleMethod, getDescriptorMethod, nameMethod).also { cache = it }
|
||||
} catch (ignored: Exception) {
|
||||
return notOnJava9.also { cache = it }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun BaseContinuationImpl.getDebugMetadataAnnotation(): DebugMetadata? =
|
||||
|
||||
Reference in New Issue
Block a user