[KAPT] Improve stub formatting; check raw stubs in tests
Merge-request: KT-MR-14244 Merged-by: Pavel Mikhailovskii <Pavel.Mikhailovskii@jetbrains.com>
This commit is contained in:
committed by
Space Team
parent
c5e6197690
commit
9688c3e761
@@ -158,6 +158,7 @@ private class StubGenerator(
|
||||
}
|
||||
|
||||
private fun Printer.printImports(file: KtFile) {
|
||||
var hasImports = false
|
||||
if (unresolvedSimpleNames.isEmpty()) return
|
||||
|
||||
val importedShortNames = mutableSetOf<String>()
|
||||
@@ -176,7 +177,6 @@ private class StubGenerator(
|
||||
|
||||
if (!acceptableByName) continue
|
||||
|
||||
|
||||
val importedReference = importDirective.importedReference
|
||||
?.getCalleeExpressionIfAny()
|
||||
?.references
|
||||
@@ -197,7 +197,9 @@ private class StubGenerator(
|
||||
importedShortNames.add(importedFqName.shortName().asString()) -> printWithNoIndent(importedFqName)
|
||||
}
|
||||
printlnWithNoIndent(";")
|
||||
hasImports = true
|
||||
}
|
||||
if (hasImports) printlnWithNoIndent()
|
||||
}
|
||||
|
||||
private inner class ClassGenerator(private val psiClass: PsiClass) {
|
||||
@@ -216,7 +218,6 @@ private class StubGenerator(
|
||||
else -> "class"
|
||||
}
|
||||
calculateMetadata(psiClass)?.let { printMetadata(it) }
|
||||
printIndent()
|
||||
printModifiers(psiClass)
|
||||
printWithNoIndent(classWord, " ", simpleName)
|
||||
printTypeParams(psiClass.typeParameters)
|
||||
@@ -245,7 +246,7 @@ private class StubGenerator(
|
||||
printType(type)
|
||||
}
|
||||
}
|
||||
printlnWithNoIndent("{")
|
||||
printlnWithNoIndent(" {")
|
||||
pushIndent()
|
||||
|
||||
if (psiClass.isEnum) {
|
||||
@@ -270,8 +271,9 @@ private class StubGenerator(
|
||||
.onEach { lineMappings.registerField(psiClass, it) }
|
||||
.associateWith { MemberData(it.name, it.signature, lineMappings.getPosition(psiClass, it)) }
|
||||
|
||||
fieldsPositions.keys.sortedWith(MembersPositionComparator(classPosition, fieldsPositions)).forEach {
|
||||
printField(it)
|
||||
fieldsPositions.keys.sortedWith(MembersPositionComparator(classPosition, fieldsPositions)).forEachIndexed { index, field ->
|
||||
if (index > 0) printlnWithNoIndent()
|
||||
printField(field)
|
||||
}
|
||||
|
||||
val methodsPositions = psiClass.methods
|
||||
@@ -283,16 +285,18 @@ private class StubGenerator(
|
||||
.onEach { lineMappings.registerMethod(psiClass, it) }
|
||||
.associateWith { MemberData(it.name, it.signature, lineMappings.getPosition(psiClass, it)) }
|
||||
|
||||
if (fieldsPositions.isNotEmpty() && methodsPositions.isNotEmpty()) printlnWithNoIndent()
|
||||
if (methodsPositions.isNotEmpty()) printlnWithNoIndent()
|
||||
methodsPositions.keys.sortedWith(MembersPositionComparator(classPosition, methodsPositions))
|
||||
.forEach {
|
||||
lineMappings.registerSignature(javacSignature(it), it)
|
||||
printMethod(it)
|
||||
.forEachIndexed { index, method ->
|
||||
lineMappings.registerSignature(javacSignature(method), method)
|
||||
if (index > 0) printlnWithNoIndent()
|
||||
printMethod(method)
|
||||
}
|
||||
|
||||
if (psiClass.innerClasses.isNotEmpty() && (fieldsPositions.isNotEmpty() || methodsPositions.isNotEmpty())) println()
|
||||
psiClass.innerClasses.forEach {
|
||||
with(ClassGenerator(it)) { printClass() }
|
||||
psiClass.innerClasses.forEachIndexed { i, innerClass ->
|
||||
if (i > 0) printWithNoIndent()
|
||||
with(ClassGenerator(innerClass)) { printClass() }
|
||||
}
|
||||
popIndent()
|
||||
println("}")
|
||||
@@ -305,7 +309,7 @@ private class StubGenerator(
|
||||
comment.split("\n").forEach {
|
||||
println(" * ", it)
|
||||
}
|
||||
println("*/")
|
||||
println(" */")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -322,7 +326,6 @@ private class StubGenerator(
|
||||
printWithNoIndent(" = ", defaultValue(field.type))
|
||||
}
|
||||
printlnWithNoIndent(";")
|
||||
printlnWithNoIndent()
|
||||
}
|
||||
|
||||
private fun Printer.printMethod(method: PsiMethod) {
|
||||
@@ -384,8 +387,6 @@ private class StubGenerator(
|
||||
popIndent()
|
||||
println("}")
|
||||
}
|
||||
|
||||
printlnWithNoIndent()
|
||||
}
|
||||
|
||||
private fun javacSignature(method: PsiMethod) = printToString {
|
||||
@@ -434,7 +435,12 @@ private class StubGenerator(
|
||||
}
|
||||
|
||||
private fun Printer.printTypeSignature(type: PsiType, annotated: Boolean) {
|
||||
printWithNoIndent((if (type is PsiClassType && isErroneous(type)) type.rawType() else type).getCanonicalText(annotated).replace('$', '.'))
|
||||
printWithNoIndent(
|
||||
(if (type is PsiClassType && isErroneous(type)) type.rawType() else type)
|
||||
.getCanonicalText(annotated)
|
||||
.replace('$', '.')
|
||||
.replace(",", ", ")
|
||||
)
|
||||
}
|
||||
|
||||
private fun Printer.printTypeParams(typeParameters: Array<PsiTypeParameter>) {
|
||||
@@ -571,7 +577,7 @@ private class StubGenerator(
|
||||
}
|
||||
printWithNoIndent("}")
|
||||
}
|
||||
is PsiAnnotation -> printToString { printAnnotation(v, false) }
|
||||
is PsiAnnotation -> printToString { printAnnotation(v, false) }.trim()
|
||||
else -> v.text
|
||||
}
|
||||
} ?: return
|
||||
|
||||
@@ -20,13 +20,11 @@ import org.jetbrains.kotlin.kapt3.test.KaptTestDirectives.EXPECTED_ERROR
|
||||
import org.jetbrains.kotlin.kapt3.test.handlers.ClassFileToSourceKaptStubHandler
|
||||
import org.jetbrains.kotlin.kapt3.test.handlers.removeMetadataAnnotationContents
|
||||
import org.jetbrains.kotlin.kapt3.test.messageCollectorProvider
|
||||
import org.jetbrains.kotlin.kapt3.util.prettyPrint
|
||||
import org.jetbrains.kotlin.test.Assertions
|
||||
import org.jetbrains.kotlin.test.model.AnalysisHandler
|
||||
import org.jetbrains.kotlin.test.model.TestArtifactKind
|
||||
import org.jetbrains.kotlin.test.model.TestModule
|
||||
import org.jetbrains.kotlin.test.services.TestServices
|
||||
import org.jetbrains.kotlin.test.services.getRealJavaFiles
|
||||
import org.jetbrains.kotlin.test.services.sourceFileProvider
|
||||
import org.jetbrains.kotlin.test.util.trimTrailingWhitespacesAndAddNewlineAtEOF
|
||||
import org.jetbrains.kotlin.test.utils.withExtension
|
||||
@@ -46,13 +44,13 @@ internal class Kapt4Handler(testServices: TestServices) : AnalysisHandler<Kapt4C
|
||||
val validate = KaptTestDirectives.NO_VALIDATION !in module.directives
|
||||
|
||||
val (kaptContext) = info
|
||||
val convertedFiles = getJavaFiles(info, module)
|
||||
val convertedFiles = getJavaFiles(info)
|
||||
kaptContext.javaLog.interceptorData.files = convertedFiles.associateBy { it.sourceFile }
|
||||
if (validate) kaptContext.compiler.enterTrees(convertedFiles)
|
||||
|
||||
val actualRaw = convertedFiles
|
||||
.sortedBy { it.sourceFile.name }
|
||||
.joinToString(ClassFileToSourceKaptStubHandler.FILE_SEPARATOR) { it.prettyPrint(kaptContext.context) }
|
||||
.joinToString(ClassFileToSourceKaptStubHandler.FILE_SEPARATOR) { (it.sourceFile as KaptJavaFileObject).file!!.readText() }
|
||||
|
||||
val actual = StringUtil.convertLineSeparators(actualRaw.trim { it <= ' ' })
|
||||
.trimTrailingWhitespacesAndAddNewlineAtEOF()
|
||||
@@ -111,8 +109,7 @@ internal class Kapt4Handler(testServices: TestServices) : AnalysisHandler<Kapt4C
|
||||
}
|
||||
|
||||
private fun getJavaFiles(
|
||||
info: Kapt4ContextBinaryArtifact,
|
||||
module: TestModule
|
||||
info: Kapt4ContextBinaryArtifact
|
||||
): List<JCTree.JCCompilationUnit> {
|
||||
val (kaptContext, kaptStubs) = info
|
||||
val convertedFiles = kaptStubs.mapIndexed { index, stub ->
|
||||
@@ -121,16 +118,13 @@ internal class Kapt4Handler(testServices: TestServices) : AnalysisHandler<Kapt4C
|
||||
sourceFile
|
||||
}
|
||||
|
||||
val javaFiles = testServices.sourceFileProvider.getRealJavaFiles(module)
|
||||
val allJavaFiles = javaFiles + convertedFiles
|
||||
|
||||
// A workaround needed for Javac to parse files correctly even if errors were already reported
|
||||
// If nerrors > 0, "parseFiles()" returns the empty list
|
||||
val oldErrorCount = kaptContext.compiler.log.nerrors
|
||||
kaptContext.compiler.log.nerrors = 0
|
||||
|
||||
try {
|
||||
val parsedJavaFiles = kaptContext.parseJavaFiles(allJavaFiles)
|
||||
val parsedJavaFiles = kaptContext.parseJavaFiles(convertedFiles)
|
||||
|
||||
for (tree in parsedJavaFiles) {
|
||||
val actualFile = File(tree.sourceFile.toUri())
|
||||
|
||||
Reference in New Issue
Block a user