Skip to content

Commit 848f660

Browse files
committed
Fix Kotlin nested class code generation
1 parent 97ba522 commit 848f660

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/visitor/CgKotlinRenderer.kt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import org.utbot.framework.codegen.model.tree.CgAnonymousFunction
1313
import org.utbot.framework.codegen.model.tree.CgArrayAnnotationArgument
1414
import org.utbot.framework.codegen.model.tree.CgArrayElementAccess
1515
import org.utbot.framework.codegen.model.tree.CgArrayInitializer
16+
import org.utbot.framework.codegen.model.tree.CgAuxiliaryClass
1617
import org.utbot.framework.codegen.model.tree.CgComparison
1718
import org.utbot.framework.codegen.model.tree.CgConstructorCall
1819
import org.utbot.framework.codegen.model.tree.CgDeclaration
@@ -32,6 +33,7 @@ import org.utbot.framework.codegen.model.tree.CgNotNullAssertion
3233
import org.utbot.framework.codegen.model.tree.CgParameterDeclaration
3334
import org.utbot.framework.codegen.model.tree.CgParameterizedTestDataProviderMethod
3435
import org.utbot.framework.codegen.model.tree.CgRegularClass
36+
import org.utbot.framework.codegen.model.tree.CgSimpleRegion
3537
import org.utbot.framework.codegen.model.tree.CgSpread
3638
import org.utbot.framework.codegen.model.tree.CgStaticsRegion
3739
import org.utbot.framework.codegen.model.tree.CgSwitchCase
@@ -120,8 +122,32 @@ internal class CgKotlinRenderer(context: CgRendererContext, printer: CgPrinter =
120122
}
121123
// render static declaration regions inside a companion object
122124
println()
125+
126+
// In Kotlin, we put static declarations in a companion object of the class,
127+
// but that **does not** apply to nested classes.
128+
// They must be located in the class itself, not its companion object.
129+
// That is why here we extract all the auxiliary classes from static regions
130+
// to form a separate region specifically for them.
131+
// See the docs on CgAuxiliaryClass for details on what they represent.
132+
val auxiliaryClassesRegion = element.staticDeclarationRegions
133+
.flatMap { it.content }
134+
.filterIsInstance<CgAuxiliaryClass>()
135+
.let { classes -> CgSimpleRegion("Util classes", classes) }
136+
137+
if (auxiliaryClassesRegion.content.isNotEmpty()) {
138+
auxiliaryClassesRegion.accept(this)
139+
println()
140+
}
141+
142+
// Here we update the static regions by removing all the auxiliary classes from them.
143+
// The remaining content of regions will be rendered inside a companion object.
144+
val updatedStaticRegions = element.staticDeclarationRegions.map { region ->
145+
val updatedContent = region.content.filterNot { it is CgAuxiliaryClass }
146+
CgStaticsRegion(region.header, updatedContent)
147+
}
148+
123149
renderCompanionObject {
124-
for ((i, staticsRegion) in element.staticDeclarationRegions.withIndex()) {
150+
for ((i, staticsRegion) in updatedStaticRegions.withIndex()) {
125151
if (i != 0) println()
126152

127153
staticsRegion.accept(this)

0 commit comments

Comments
 (0)