@@ -13,6 +13,7 @@ import org.utbot.framework.codegen.model.tree.CgAnonymousFunction
13
13
import org.utbot.framework.codegen.model.tree.CgArrayAnnotationArgument
14
14
import org.utbot.framework.codegen.model.tree.CgArrayElementAccess
15
15
import org.utbot.framework.codegen.model.tree.CgArrayInitializer
16
+ import org.utbot.framework.codegen.model.tree.CgAuxiliaryClass
16
17
import org.utbot.framework.codegen.model.tree.CgComparison
17
18
import org.utbot.framework.codegen.model.tree.CgConstructorCall
18
19
import org.utbot.framework.codegen.model.tree.CgDeclaration
@@ -32,6 +33,7 @@ import org.utbot.framework.codegen.model.tree.CgNotNullAssertion
32
33
import org.utbot.framework.codegen.model.tree.CgParameterDeclaration
33
34
import org.utbot.framework.codegen.model.tree.CgParameterizedTestDataProviderMethod
34
35
import org.utbot.framework.codegen.model.tree.CgRegularClass
36
+ import org.utbot.framework.codegen.model.tree.CgSimpleRegion
35
37
import org.utbot.framework.codegen.model.tree.CgSpread
36
38
import org.utbot.framework.codegen.model.tree.CgStaticsRegion
37
39
import org.utbot.framework.codegen.model.tree.CgSwitchCase
@@ -120,8 +122,32 @@ internal class CgKotlinRenderer(context: CgRendererContext, printer: CgPrinter =
120
122
}
121
123
// render static declaration regions inside a companion object
122
124
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
+
123
149
renderCompanionObject {
124
- for ((i, staticsRegion) in element.staticDeclarationRegions .withIndex()) {
150
+ for ((i, staticsRegion) in updatedStaticRegions .withIndex()) {
125
151
if (i != 0 ) println ()
126
152
127
153
staticsRegion.accept(this )
0 commit comments