Navigate to inline function from stackTrace

#KT-8066 Fixed
This commit is contained in:
Natalia Ukhorskaya
2015-11-17 14:04:43 +03:00
parent 93ccc08e3d
commit 9f0b52f030
7 changed files with 241 additions and 1 deletions
@@ -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<String>) {
model("debugger/positionManager", recursive = false, extension = null, testClassName = "MultiFile")
}
testClass<AbstractKotlinExceptionFilterTest>() {
model("debugger/exceptionFilter", pattern = """^([^\.]+)$""", recursive = false)
}
testClass<AbstractSmartStepIntoTest>() {
model("debugger/smartStepInto")
}
@@ -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
@@ -0,0 +1,12 @@
package inlineFunSameFile
import inlineFunPackage.*
fun box() {
foo {
println()
}
}
// FILE: inlineFunctionFile.kt
// LINE: 4
@@ -0,0 +1,6 @@
package inlineFunPackage
inline fun foo(f: () -> Unit) {
null!!
f()
}
@@ -0,0 +1,15 @@
package inlineFunSameFile
fun box() {
foo {
println()
}
}
inline fun foo(f: () -> Unit) {
null!!
f()
}
// FILE: inlineFunctionSameFile.kt
// LINE: 10
@@ -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)
}
}
@@ -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);
}
}