Skip to content

Commit 1402e38

Browse files
committed
Rework type parameters for some class ids
Earlier type parameters were hardcoded in the class name, simple name, etc. Then they were removed from names completely, but that lead to compilation errors for Kotlin, where type parameters must always be specified. So, this commit adds type parameters, but instead of writing them into names, we add them as a separate property 'typeParameters' of BuiltinClassId.
1 parent 5030d2e commit 1402e38

File tree

3 files changed

+52
-15
lines changed

3 files changed

+52
-15
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ open class ClassId @JvmOverloads constructor(
750750
*/
751751
class BuiltinClassId(
752752
name: String,
753+
elementClassId: ClassId? = null,
753754
override val canonicalName: String,
754755
override val simpleName: String,
755756
// by default we assume that the class is not a member class
@@ -766,6 +767,7 @@ class BuiltinClassId(
766767
override val isInner: Boolean = false,
767768
override val isNested: Boolean = false,
768769
override val isSynthetic: Boolean = false,
770+
override val typeParameters: TypeParameters = TypeParameters(),
769771
override val allMethods: Sequence<MethodId> = emptySequence(),
770772
override val allConstructors: Sequence<ConstructorId> = emptySequence(),
771773
override val outerClass: Class<*>? = null,
@@ -774,7 +776,7 @@ class BuiltinClassId(
774776
-1, 0 -> ""
775777
else -> canonicalName.substring(0, index)
776778
},
777-
) : ClassId(name = name, isNullable = isNullable) {
779+
) : ClassId(name = name, isNullable = isNullable, elementClassId = elementClassId) {
778780
init {
779781
BUILTIN_CLASSES_BY_NAMES[name] = this
780782
}

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgMethodConstructor.kt

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,9 +1282,16 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
12821282
}
12831283

12841284
if (containsFailureExecution) {
1285+
val classClassId = Class::class.id
12851286
val expectedException = CgParameterDeclaration(
12861287
parameter = declareParameter(
1287-
type = Class::class.id,
1288+
type = BuiltinClassId(
1289+
name = classClassId.name,
1290+
simpleName = classClassId.simpleName,
1291+
canonicalName = classClassId.canonicalName,
1292+
packageName = classClassId.packageName,
1293+
typeParameters = TypeParameters(listOf(Throwable::class.java.id))
1294+
),
12881295
name = nameGenerator.variableName(expectedErrorVarName)
12891296
),
12901297
// exceptions are always reference type
@@ -1492,16 +1499,37 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
14921499
*/
14931500
private val argListClassId: ClassId
14941501
get() = when (testFramework) {
1495-
Junit5 -> java.util.ArrayList::class.id
1496-
TestNg -> BuiltinClassId(
1497-
name = Array<Array<Any?>?>::class.java.name,
1498-
simpleName = when (codegenLanguage) {
1499-
CodegenLanguage.JAVA -> "Object[][]"
1500-
CodegenLanguage.KOTLIN -> "Array<Array<Any?>?>"
1501-
},
1502-
canonicalName = Array<Array<Any?>?>::class.java.canonicalName,
1503-
packageName = Array<Array<Any?>?>::class.java.packageName,
1504-
)
1502+
Junit5 -> {
1503+
val arrayListId = java.util.ArrayList::class.id
1504+
BuiltinClassId(
1505+
name = arrayListId.name,
1506+
simpleName = arrayListId.simpleName,
1507+
canonicalName = arrayListId.canonicalName,
1508+
packageName = arrayListId.packageName,
1509+
elementClassId = argumentsClassId,
1510+
typeParameters = TypeParameters(listOf(argumentsClassId))
1511+
)
1512+
}
1513+
TestNg -> {
1514+
val outerArrayId = Array<Array<Any?>?>::class.id
1515+
val innerArrayId = BuiltinClassId(
1516+
name = objectArrayClassId.name,
1517+
simpleName = objectArrayClassId.simpleName,
1518+
canonicalName = objectArrayClassId.canonicalName,
1519+
packageName = objectArrayClassId.packageName,
1520+
elementClassId = objectClassId,
1521+
typeParameters = TypeParameters(listOf(objectClassId))
1522+
)
1523+
1524+
BuiltinClassId(
1525+
name = outerArrayId.name,
1526+
simpleName = outerArrayId.simpleName,
1527+
canonicalName = outerArrayId.canonicalName,
1528+
packageName = outerArrayId.packageName,
1529+
elementClassId = innerArrayId,
1530+
typeParameters = TypeParameters(listOf(innerArrayId))
1531+
)
1532+
}
15051533
Junit4 -> error("Parameterized tests are not supported for JUnit4")
15061534
}
15071535

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,7 @@ internal class CgKotlinRenderer(context: CgContext, printer: CgPrinter = CgPrint
312312
}
313313

314314
override fun renderMethodSignature(element: CgParameterizedTestDataProviderMethod) {
315-
val returnType =
316-
if (element.returnType.simpleName == "Array<Array<Any?>?>") "Array<Array<Any?>?>" else "${element.returnType}"
315+
val returnType = getKotlinClassString(element.returnType)
317316
println("fun ${element.name}(): $returnType")
318317
}
319318

@@ -469,7 +468,15 @@ internal class CgKotlinRenderer(context: CgContext, printer: CgPrinter = CgPrint
469468
else -> {
470469
// we cannot access kClass for BuiltinClassId
471470
// we cannot use simple name here because this class can be not imported
472-
if (id is BuiltinClassId) id.name else id.kClass.id.asString()
471+
val typeParameters = id.typeParameters.parameters
472+
.map { getKotlinClassString(it) }
473+
.joinToString(", ")
474+
val rawTypeName = if (id is BuiltinClassId) id.name else id.kClass.id.asString()
475+
if (typeParameters.isEmpty()) {
476+
rawTypeName
477+
} else {
478+
"$rawTypeName<$typeParameters>"
479+
}
473480
}
474481
}
475482
}

0 commit comments

Comments
 (0)