diff --git a/compiler/testData/javaModules/coroutinesDebugMetadata/stdout.txt b/compiler/testData/javaModules/coroutinesDebugMetadata/stdout.txt new file mode 100644 index 00000000000..26db5b8efd5 --- /dev/null +++ b/compiler/testData/javaModules/coroutinesDebugMetadata/stdout.txt @@ -0,0 +1 @@ +usage/some.module.withsome.packages.Test diff --git a/compiler/testData/javaModules/coroutinesDebugMetadata/usage.txt b/compiler/testData/javaModules/coroutinesDebugMetadata/usage.txt new file mode 100644 index 00000000000..d86bac9de59 --- /dev/null +++ b/compiler/testData/javaModules/coroutinesDebugMetadata/usage.txt @@ -0,0 +1 @@ +OK diff --git a/compiler/testData/javaModules/coroutinesDebugMetadata/usage/module-info.java b/compiler/testData/javaModules/coroutinesDebugMetadata/usage/module-info.java new file mode 100644 index 00000000000..ecbcee60df4 --- /dev/null +++ b/compiler/testData/javaModules/coroutinesDebugMetadata/usage/module-info.java @@ -0,0 +1,3 @@ +module usage { + requires kotlin.stdlib; +} diff --git a/compiler/testData/javaModules/coroutinesDebugMetadata/usage/usage.kt b/compiler/testData/javaModules/coroutinesDebugMetadata/usage/usage.kt new file mode 100644 index 00000000000..d54c3a50471 --- /dev/null +++ b/compiler/testData/javaModules/coroutinesDebugMetadata/usage/usage.kt @@ -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 { + (it as BaseContinuationImpl).getStackTraceElement() + } + } +} + +suspend fun main() { + println(Test().getStackTraceElement().className) +} diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/Java9ModulesIntegrationTest.kt b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/Java9ModulesIntegrationTest.kt index c71b0c9e693..af7ea30935a 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/Java9ModulesIntegrationTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/Java9ModulesIntegrationTest.kt @@ -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( + 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) + } } diff --git a/libraries/stdlib/jvm/src/kotlin/coroutines/jvm/internal/DebugMetadata.kt b/libraries/stdlib/jvm/src/kotlin/coroutines/jvm/internal/DebugMetadata.kt index 8202404cb64..617127660b5 100644 --- a/libraries/stdlib/jvm/src/kotlin/coroutines/jvm/internal/DebugMetadata.kt +++ b/libraries/stdlib/jvm/src/kotlin/coroutines/jvm/internal/DebugMetadata.kt @@ -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? =