Minor. Code clean

This commit is contained in:
Mikhael Bogdanov
2018-08-24 17:13:24 +02:00
parent 4687381179
commit 367ab5470c
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.codegen package org.jetbrains.kotlin.codegen
import com.google.common.collect.Lists
import com.intellij.openapi.util.text.StringUtil import com.intellij.openapi.util.text.StringUtil
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.test.KotlinTestUtils import org.jetbrains.kotlin.test.KotlinTestUtils
@@ -44,7 +43,7 @@ abstract class AbstractLineNumberTest : CodegenTestCase() {
if (isCustomTest) { if (isCustomTest) {
val actualLineNumbers = extractActualLineNumbersFromBytecode(classFileFactory, false) val actualLineNumbers = extractActualLineNumbersFromBytecode(classFileFactory, false)
val text = psiFile.text val text = psiFile.text
val newFileText = text.substring(0, text.indexOf("// ")) + getActualLineNumbersAsString(actualLineNumbers) val newFileText = text.substringBefore("// ") + getActualLineNumbersAsString(actualLineNumbers)
KotlinTestUtils.assertEqualsToFile(wholeFile, newFileText) KotlinTestUtils.assertEqualsToFile(wholeFile, newFileText)
} else { } else {
val expectedLineNumbers = extractSelectedLineNumbersFromSource(psiFile) val expectedLineNumbers = extractSelectedLineNumbersFromSource(psiFile)
@@ -54,7 +53,7 @@ abstract class AbstractLineNumberTest : CodegenTestCase() {
} }
} catch (e: Throwable) { } catch (e: Throwable) {
println(classFileFactory.createText()) println(classFileFactory.createText())
throw rethrow(e) throw e
} }
} }
@@ -62,37 +61,24 @@ abstract class AbstractLineNumberTest : CodegenTestCase() {
private const val LINE_NUMBER_FUN = "lineNumber" private const val LINE_NUMBER_FUN = "lineNumber"
private val TEST_LINE_NUMBER_PATTERN = Pattern.compile("^.*test.$LINE_NUMBER_FUN\\(\\).*$") private val TEST_LINE_NUMBER_PATTERN = Pattern.compile("^.*test.$LINE_NUMBER_FUN\\(\\).*$")
private fun createLineNumberDeclaration(): CodegenTestCase.TestFile { private fun createLineNumberDeclaration() =
return CodegenTestCase.TestFile( CodegenTestCase.TestFile(
"$LINE_NUMBER_FUN.kt", "$LINE_NUMBER_FUN.kt",
"package test;\n\npublic fun $LINE_NUMBER_FUN(): Int = 0\n" "package test;\n\npublic fun $LINE_NUMBER_FUN(): Int = 0\n"
) )
}
private fun getActualLineNumbersAsString(lines: List<String>): String { private fun getActualLineNumbersAsString(lines: List<String>) =
return lines.joinToString(" ", "// ", "", -1, "...") { lineNumber -> lineNumber } lines.joinToString(" ", "// ")
}
private fun extractActualLineNumbersFromBytecode(factory: ClassFileFactory, testFunInvoke: Boolean): List<String> { private fun extractActualLineNumbersFromBytecode(factory: ClassFileFactory, testFunInvoke: Boolean) =
val actualLineNumbers = Lists.newArrayList<String>() factory.getClassFiles().flatMap { outputFile ->
for (outputFile in factory.getClassFiles()) {
val cr = ClassReader(outputFile.asByteArray()) val cr = ClassReader(outputFile.asByteArray())
try { if (testFunInvoke) readTestFunLineNumbers(cr) else readAllLineNumbers(cr)
val lineNumbers = if (testFunInvoke) readTestFunLineNumbers(cr) else readAllLineNumbers(cr)
actualLineNumbers.addAll(lineNumbers)
} catch (e: Throwable) {
println(factory.createText())
throw rethrow(e)
}
} }
return actualLineNumbers
}
private fun extractSelectedLineNumbersFromSource(file: KtFile): List<String> { private fun extractSelectedLineNumbersFromSource(file: KtFile): List<String> {
val fileContent = file.text val fileContent = file.text
val lineNumbers = Lists.newArrayList<String>() val lineNumbers = arrayListOf<String>()
val lines = StringUtil.convertLineSeparators(fileContent).split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() val lines = StringUtil.convertLineSeparators(fileContent).split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
for (i in lines.indices) { for (i in lines.indices) {
@@ -106,7 +92,7 @@ abstract class AbstractLineNumberTest : CodegenTestCase() {
} }
private fun readTestFunLineNumbers(cr: ClassReader): List<String> { private fun readTestFunLineNumbers(cr: ClassReader): List<String> {
val labels = Lists.newArrayList<Label>() val labels = arrayListOf<Label>()
val labels2LineNumbers = HashMap<Label, String>() val labels2LineNumbers = HashMap<Label, String>()
val visitor = object : ClassVisitor(Opcodes.ASM5) { val visitor = object : ClassVisitor(Opcodes.ASM5) {
@@ -114,16 +100,15 @@ abstract class AbstractLineNumberTest : CodegenTestCase() {
access: Int, access: Int,
name: String, name: String,
desc: String, desc: String,
signature: String, signature: String?,
exceptions: Array<String> exceptions: Array<String>?
): MethodVisitor { ): MethodVisitor {
return object : MethodVisitor(Opcodes.ASM5) { return object : MethodVisitor(Opcodes.ASM5) {
private var lastLabel: Label? = null private var lastLabel: Label? = null
override fun visitMethodInsn(opcode: Int, owner: String, name: String, desc: String, itf: Boolean) { override fun visitMethodInsn(opcode: Int, owner: String, name: String, desc: String, itf: Boolean) {
if (LINE_NUMBER_FUN == name) { if (LINE_NUMBER_FUN == name) {
assert(lastLabel != null) { "A function call with no preceding label" } labels.add(lastLabel ?: error("A function call with no preceding label"))
labels.add(lastLabel)
} }
lastLabel = null lastLabel = null
} }
@@ -141,13 +126,9 @@ abstract class AbstractLineNumberTest : CodegenTestCase() {
cr.accept(visitor, ClassReader.SKIP_FRAMES) cr.accept(visitor, ClassReader.SKIP_FRAMES)
val lineNumbers = Lists.newArrayList<String>() return labels.map { label ->
for (label in labels) { labels2LineNumbers[label] ?: error("No line number found for a label")
val lineNumber = labels2LineNumbers[label] ?: error("No line number found for a label")
lineNumbers.add(lineNumber)
} }
return lineNumbers
} }
private fun readAllLineNumbers(reader: ClassReader): List<String> { private fun readAllLineNumbers(reader: ClassReader): List<String> {
@@ -159,8 +140,8 @@ abstract class AbstractLineNumberTest : CodegenTestCase() {
access: Int, access: Int,
name: String, name: String,
desc: String, desc: String,
signature: String, signature: String?,
exceptions: Array<String> exceptions: Array<String>?
): MethodVisitor { ): MethodVisitor {
return object : MethodVisitor(Opcodes.ASM5) { return object : MethodVisitor(Opcodes.ASM5) {
override fun visitLineNumber(line: Int, label: Label) { override fun visitLineNumber(line: Int, label: Label) {