From 9f0b52f0308c26f48fe78e77fe23483aa73bc3fe Mon Sep 17 00:00:00 2001 From: Natalia Ukhorskaya Date: Tue, 17 Nov 2015 14:04:43 +0300 Subject: [PATCH] Navigate to inline function from stackTrace #KT-8066 Fixed --- .../kotlin/generators/tests/GenerateTests.kt | 5 ++ .../idea/filters/KotlinExceptionFilter.kt | 75 ++++++++++++++++- .../inlineFunctionAnotherFile.kt | 12 +++ .../inlineFunctionFile.kt | 6 ++ .../inlineFunctionSameFile.kt | 15 ++++ .../AbstractKotlinExceptionFilterTest.kt | 80 +++++++++++++++++++ .../KotlinExceptionFilterTestGenerated.java | 49 ++++++++++++ 7 files changed, 241 insertions(+), 1 deletion(-) create mode 100644 idea/testData/debugger/exceptionFilter/inlineFunctionAnotherFile/inlineFunctionAnotherFile.kt create mode 100644 idea/testData/debugger/exceptionFilter/inlineFunctionAnotherFile/inlineFunctionFile.kt create mode 100644 idea/testData/debugger/exceptionFilter/inlineFunctionSameFile/inlineFunctionSameFile.kt create mode 100644 idea/tests/org/jetbrains/kotlin/idea/filters/AbstractKotlinExceptionFilterTest.kt create mode 100644 idea/tests/org/jetbrains/kotlin/idea/filters/KotlinExceptionFilterTestGenerated.java diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 6a14075fd30..f8210aaecbc 100644 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -73,6 +73,7 @@ import org.jetbrains.kotlin.idea.decompiler.textBuilder.AbstractCommonDecompiled import org.jetbrains.kotlin.idea.decompiler.textBuilder.AbstractJsDecompiledTextFromJsMetadataTest import org.jetbrains.kotlin.idea.decompiler.textBuilder.AbstractJvmDecompiledTextTest import org.jetbrains.kotlin.idea.editor.quickDoc.AbstractQuickDocProviderTest +import org.jetbrains.kotlin.idea.filters.AbstractKotlinExceptionFilterTest import org.jetbrains.kotlin.idea.folding.AbstractKotlinFoldingTest import org.jetbrains.kotlin.idea.hierarchy.AbstractHierarchyTest import org.jetbrains.kotlin.idea.highlighter.* @@ -679,6 +680,10 @@ fun main(args: Array) { model("debugger/positionManager", recursive = false, extension = null, testClassName = "MultiFile") } + testClass() { + model("debugger/exceptionFilter", pattern = """^([^\.]+)$""", recursive = false) + } + testClass() { model("debugger/smartStepInto") } diff --git a/idea/src/org/jetbrains/kotlin/idea/filters/KotlinExceptionFilter.kt b/idea/src/org/jetbrains/kotlin/idea/filters/KotlinExceptionFilter.kt index 7c6fe602be0..edd1e46b298 100644 --- a/idea/src/org/jetbrains/kotlin/idea/filters/KotlinExceptionFilter.kt +++ b/idea/src/org/jetbrains/kotlin/idea/filters/KotlinExceptionFilter.kt @@ -20,9 +20,24 @@ import com.intellij.execution.filters.ExceptionFilter import com.intellij.execution.filters.Filter import com.intellij.execution.filters.HyperlinkInfo import com.intellij.execution.filters.OpenFileHyperlinkInfo +import com.intellij.openapi.compiler.CompilerPaths +import com.intellij.openapi.project.Project +import com.intellij.openapi.roots.ProjectFileIndex +import com.intellij.openapi.vfs.VirtualFile import com.intellij.psi.search.GlobalSearchScope +import org.jetbrains.kotlin.codegen.inline.FileMapping +import org.jetbrains.kotlin.codegen.inline.InlineCodegenUtil +import org.jetbrains.kotlin.codegen.inline.SMAPParser +import org.jetbrains.kotlin.idea.core.refactoring.getLineCount +import org.jetbrains.kotlin.idea.core.refactoring.toPsiFile import org.jetbrains.kotlin.idea.util.DebuggerUtils +import org.jetbrains.kotlin.idea.util.ProjectRootsUtil +import org.jetbrains.kotlin.name.tail import org.jetbrains.kotlin.resolve.jvm.JvmClassName +import org.jetbrains.kotlin.utils.addToStdlib.check +import org.jetbrains.org.objectweb.asm.ClassReader +import org.jetbrains.org.objectweb.asm.ClassVisitor +import java.io.File import java.util.regex.Pattern class KotlinExceptionFilter(private val searchScope: GlobalSearchScope) : Filter { @@ -49,9 +64,67 @@ class KotlinExceptionFilter(private val searchScope: GlobalSearchScope) : Filter val virtualFile = file.virtualFile ?: return null - return OpenFileHyperlinkInfo(project, virtualFile, lineNumber) + return virtualFileForInlineCall(jvmClassName, virtualFile, lineNumber + 1, project) ?: + OpenFileHyperlinkInfo(project, virtualFile, lineNumber) } + private fun virtualFileForInlineCall(jvmName: JvmClassName, file: VirtualFile, lineNumber: Int, project: Project): OpenFileHyperlinkInfo? { + if (!ProjectRootsUtil.isInContent(project, file, true, false, false)) return null + + val linesInFile = file.toPsiFile(project)?.getLineCount() ?: return null + if (lineNumber <= linesInFile) return null + + val className = jvmName.fqNameForClassNameWithoutDollars.tail(jvmName.packageFqName).asString().replace('.', '$') + + val module = ProjectFileIndex.SERVICE.getInstance(project).getModuleForFile(file) + val outputDir = CompilerPaths.getModuleOutputDirectory(module, /*forTests = */ false) ?: return null + + val classByByDirectory = findClassFileByPath(jvmName.packageFqName.asString(), className, outputDir) ?: return null + + return readDebugInfoForInlineFun(classByByDirectory, lineNumber, project) + } + + private fun findClassFileByPath(packageName: String, className: String, outputDir: VirtualFile): File? { + val outDirFile = File(outputDir.path).check { it.exists() } ?: return null + + val parentDirectory = File(outDirFile, packageName.replace(".", File.separator)) + if (!parentDirectory.exists()) return null + + val classFile = File(parentDirectory, className + ".class") + if (classFile.exists()) { + return classFile + } + + return null + } + + private fun readDebugInfoForInlineFun(classFile: File, line: Int, project: Project): OpenFileHyperlinkInfo? { + val debugInfo = readDebugInfo(classFile) ?: return null + + val mappings = SMAPParser.parse(debugInfo) + + val mappingInfo = mappings.fileMappings.firstOrNull { + it.getIntervalIfContains(line) != null + } ?: return null + + val newJvmName = JvmClassName.byInternalName(mappingInfo.path) + val newSourceFile = DebuggerUtils.findSourceFileForClass(project, searchScope, newJvmName, mappingInfo.name) ?: return null + return OpenFileHyperlinkInfo(project, newSourceFile.virtualFile, mappingInfo.getIntervalIfContains(line)!!.map(line) - 1) + } + + private fun readDebugInfo(classFile: File): String? { + val cr = ClassReader(classFile.readBytes()); + var debugInfo: String? = null + cr.accept(object : ClassVisitor(InlineCodegenUtil.API) { + override fun visitSource(source: String?, debug: String?) { + debugInfo = debug + } + }, ClassReader.SKIP_FRAMES and ClassReader.SKIP_CODE) + return debugInfo + } + + private fun FileMapping.getIntervalIfContains(destLine: Int) = lineMappings.firstOrNull { it.contains(destLine) } + private fun patchResult(result: Filter.Result, line: String): Filter.Result { val newHyperlinkInfo = createHyperlinkInfo(line) ?: return result diff --git a/idea/testData/debugger/exceptionFilter/inlineFunctionAnotherFile/inlineFunctionAnotherFile.kt b/idea/testData/debugger/exceptionFilter/inlineFunctionAnotherFile/inlineFunctionAnotherFile.kt new file mode 100644 index 00000000000..d7e89ae72da --- /dev/null +++ b/idea/testData/debugger/exceptionFilter/inlineFunctionAnotherFile/inlineFunctionAnotherFile.kt @@ -0,0 +1,12 @@ +package inlineFunSameFile + +import inlineFunPackage.* + +fun box() { + foo { + println() + } +} + +// FILE: inlineFunctionFile.kt +// LINE: 4 \ No newline at end of file diff --git a/idea/testData/debugger/exceptionFilter/inlineFunctionAnotherFile/inlineFunctionFile.kt b/idea/testData/debugger/exceptionFilter/inlineFunctionAnotherFile/inlineFunctionFile.kt new file mode 100644 index 00000000000..eb880e7ec22 --- /dev/null +++ b/idea/testData/debugger/exceptionFilter/inlineFunctionAnotherFile/inlineFunctionFile.kt @@ -0,0 +1,6 @@ +package inlineFunPackage + +inline fun foo(f: () -> Unit) { + null!! + f() +} diff --git a/idea/testData/debugger/exceptionFilter/inlineFunctionSameFile/inlineFunctionSameFile.kt b/idea/testData/debugger/exceptionFilter/inlineFunctionSameFile/inlineFunctionSameFile.kt new file mode 100644 index 00000000000..b1dc7271504 --- /dev/null +++ b/idea/testData/debugger/exceptionFilter/inlineFunctionSameFile/inlineFunctionSameFile.kt @@ -0,0 +1,15 @@ +package inlineFunSameFile + +fun box() { + foo { + println() + } +} + +inline fun foo(f: () -> Unit) { + null!! + f() +} + +// FILE: inlineFunctionSameFile.kt +// LINE: 10 \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/filters/AbstractKotlinExceptionFilterTest.kt b/idea/tests/org/jetbrains/kotlin/idea/filters/AbstractKotlinExceptionFilterTest.kt new file mode 100644 index 00000000000..4e87e912221 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/filters/AbstractKotlinExceptionFilterTest.kt @@ -0,0 +1,80 @@ +/* + * Copyright 2010-2015 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. + */ + +package org.jetbrains.kotlin.idea.filters + +import com.intellij.execution.filters.FileHyperlinkInfo +import com.intellij.openapi.fileEditor.FileDocumentManager +import com.intellij.psi.search.GlobalSearchScope +import com.intellij.testFramework.PsiTestUtil +import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime +import org.jetbrains.kotlin.fileClasses.NoResolveFileClassesProvider +import org.jetbrains.kotlin.fileClasses.getFileClassFqName +import org.jetbrains.kotlin.idea.core.refactoring.toVirtualFile +import org.jetbrains.kotlin.idea.test.KotlinCodeInsightTestCase +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.test.InTextDirectivesUtils +import org.jetbrains.kotlin.test.MockLibraryUtil +import java.io.File +import java.lang.reflect.InvocationTargetException +import java.net.URL +import java.net.URLClassLoader + +public abstract class AbstractKotlinExceptionFilterTest: KotlinCodeInsightTestCase() { + override fun getTestDataPath() = "" + + protected fun doTest(path: String) { + val rootDir = File(path) + val mainFile = File(rootDir, rootDir.name + ".kt") + rootDir.listFiles().filter { it != mainFile }.forEach { configureByFile(it.canonicalPath) } + configureByFile(mainFile.canonicalPath) + + val fileText = file.text + + val outDir = project.baseDir.createChildDirectory(this, "out") + PsiTestUtil.setCompilerOutputPath(module, outDir.url, false) + + MockLibraryUtil.compileKotlin(path, File(outDir.path)) + + val stackTraceElement = try { + val className = NoResolveFileClassesProvider.getFileClassFqName(file as KtFile) + val clazz = URLClassLoader(arrayOf(URL(outDir.url + "/")), ForTestCompileRuntime.runtimeJarClassLoader()).loadClass(className.asString()) + clazz.getMethod("box")?.invoke(null) + throw AssertionError("class ${className.asString()} should have box() method and throw exception") + } + catch(e: InvocationTargetException) { + e.targetException.stackTrace[0] + } + + val filter = KotlinExceptionFilterFactory().create(GlobalSearchScope.allScope(project)) + val prefix = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// PREFIX: ") ?: "at" + val result = filter.applyFilter("$prefix $stackTraceElement", 0) ?: throw AssertionError("Couldn't apply filter to $stackTraceElement") + + val info = result.firstHyperlinkInfo as FileHyperlinkInfo + val descriptor = info.descriptor!! + + val expectedFileName = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// FILE: ")!! + val expectedVirtualFile = File(rootDir, expectedFileName).toVirtualFile() ?: throw AssertionError("Couldn't find file: name = $expectedFileName") + val expectedLineNumber = InTextDirectivesUtils.getPrefixedInt(fileText, "// LINE: ")!! + + // TODO compare virtual files + assertEquals(expectedFileName, descriptor.file.name) + + val document = FileDocumentManager.getInstance().getDocument(expectedVirtualFile)!! + val expectedOffset = document.getLineStartOffset(expectedLineNumber - 1) + assertEquals(expectedOffset, descriptor.offset) + } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/filters/KotlinExceptionFilterTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/filters/KotlinExceptionFilterTestGenerated.java new file mode 100644 index 00000000000..bf2871ba493 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/filters/KotlinExceptionFilterTestGenerated.java @@ -0,0 +1,49 @@ +/* + * Copyright 2010-2015 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. + */ + +package org.jetbrains.kotlin.idea.filters; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; +import org.jetbrains.kotlin.test.KotlinTestUtils; +import org.jetbrains.kotlin.test.TestMetadata; +import org.junit.runner.RunWith; + +import java.io.File; +import java.util.regex.Pattern; + +/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("idea/testData/debugger/exceptionFilter") +@TestDataPath("$PROJECT_ROOT") +@RunWith(JUnit3RunnerWithInners.class) +public class KotlinExceptionFilterTestGenerated extends AbstractKotlinExceptionFilterTest { + public void testAllFilesPresentInExceptionFilter() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/debugger/exceptionFilter"), Pattern.compile("^([^\\.]+)$"), false); + } + + @TestMetadata("inlineFunctionAnotherFile") + public void testInlineFunctionAnotherFile() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/debugger/exceptionFilter/inlineFunctionAnotherFile/"); + doTest(fileName); + } + + @TestMetadata("inlineFunctionSameFile") + public void testInlineFunctionSameFile() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/debugger/exceptionFilter/inlineFunctionSameFile/"); + doTest(fileName); + } +}