Skip to content

Commit e3fb541

Browse files
committed
Corrected Kotlin codegen to properly handle top-level functions
1 parent b100af3 commit e3fb541

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

utbot-core/src/main/kotlin/org/utbot/common/KClassUtil.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package org.utbot.common
22

33
import java.lang.reflect.InvocationTargetException
44
import java.lang.reflect.Method
5-
import kotlin.reflect.KClass
65

76
val Class<*>.nameOfPackage: String get() = `package`?.name?:""
87

@@ -12,8 +11,5 @@ fun Method.invokeCatching(obj: Any?, args: List<Any?>) = try {
1211
Result.failure<Nothing>(e.targetException)
1312
}
1413

15-
val KClass<*>.allNestedClasses: List<KClass<*>>
16-
get() = listOf(this) + nestedClasses.flatMap { it.allNestedClasses }
17-
1814
val Class<*>.allNestedClasses: List<Class<*>>
1915
get() = listOf(this) + this.declaredClasses.flatMap { it.declaredClasses.toList() }

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/IdUtil.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import kotlin.reflect.KClass
2525
import kotlin.reflect.KFunction
2626
import kotlin.reflect.KProperty
2727
import kotlin.reflect.full.instanceParameter
28+
import kotlin.reflect.jvm.internal.impl.load.kotlin.header.KotlinClassHeader
2829
import kotlin.reflect.jvm.javaConstructor
2930
import kotlin.reflect.jvm.javaField
3031
import kotlin.reflect.jvm.javaGetter
@@ -178,6 +179,14 @@ val ClassId.isDoubleType: Boolean
178179
val ClassId.isClassType: Boolean
179180
get() = this == classClassId
180181

182+
/**
183+
* Checks if the class is a Kotlin class with kind File (see [Metadata.kind] for more details)
184+
*/
185+
val ClassId.isKotlinFile: Boolean
186+
get() = jClass.annotations.filterIsInstance<Metadata>().singleOrNull()?.let {
187+
KotlinClassHeader.Kind.getById(it.kind) == KotlinClassHeader.Kind.FILE_FACADE
188+
} ?: false
189+
181190
val voidClassId = ClassId("void")
182191
val booleanClassId = ClassId("boolean")
183192
val byteClassId = ClassId("byte")

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,10 @@ internal abstract class CgAbstractRenderer(
131131
}
132132
}
133133

134+
protected abstract val ClassId.shouldBeOmittedWhenUsedAsCaller: Boolean
135+
134136
private val MethodId.accessibleByName: Boolean
135-
get() = (context.shouldOptimizeImports && this in context.importedStaticMethods) || classId == context.generatedClass
137+
get() = (context.shouldOptimizeImports && this in context.importedStaticMethods) || classId == context.generatedClass || classId.shouldBeOmittedWhenUsedAsCaller
136138

137139
override fun visit(element: CgElement) {
138140
val error =
@@ -654,8 +656,10 @@ internal abstract class CgAbstractRenderer(
654656
}
655657

656658
override fun visit(element: CgStaticFieldAccess) {
657-
print(element.declaringClass.asString())
658-
print(".")
659+
if (!element.declaringClass.shouldBeOmittedWhenUsedAsCaller) {
660+
print(element.declaringClass.asString())
661+
print(".")
662+
}
659663
print(element.fieldName)
660664
}
661665

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ internal class CgJavaRenderer(context: CgRendererContext, printer: CgPrinter = C
6060

6161
override val langPackage: String = "java.lang"
6262

63+
override val ClassId.shouldBeOmittedWhenUsedAsCaller: Boolean
64+
get() = false
65+
6366
override fun visit(element: AbstractCgClass<*>) {
6467
for (annotation in element.annotations) {
6568
annotation.accept(this)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import org.utbot.framework.plugin.api.TypeParameters
5353
import org.utbot.framework.plugin.api.WildcardTypeParameter
5454
import org.utbot.framework.plugin.api.util.id
5555
import org.utbot.framework.plugin.api.util.isArray
56+
import org.utbot.framework.plugin.api.util.isKotlinFile
5657
import org.utbot.framework.plugin.api.util.isPrimitive
5758
import org.utbot.framework.plugin.api.util.isPrimitiveWrapper
5859
import org.utbot.framework.plugin.api.util.kClass
@@ -72,6 +73,9 @@ internal class CgKotlinRenderer(context: CgRendererContext, printer: CgPrinter =
7273

7374
override val langPackage: String = "kotlin"
7475

76+
override val ClassId.shouldBeOmittedWhenUsedAsCaller: Boolean
77+
get() = isKotlinFile
78+
7579
override fun visit(element: AbstractCgClass<*>) {
7680
for (annotation in element.annotations) {
7781
annotation.accept(this)

0 commit comments

Comments
 (0)