From 8bcb2803447ae3c64bf304ba399e79699ebdffb3 Mon Sep 17 00:00:00 2001 From: Ivan Volkov Date: Fri, 11 Nov 2022 11:08:59 +0300 Subject: [PATCH] Handle not-nullable parameters in Kotlin constructors correctly --- .../utbot/framework/assemble/AssembleModelGenerator.kt | 2 +- .../framework/modifications/ConstructorAnalyzer.kt | 7 +++++-- .../org/utbot/framework/util/KotlinIntrinsicsUtil.kt | 10 ++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 utbot-framework/src/main/kotlin/org/utbot/framework/util/KotlinIntrinsicsUtil.kt diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/assemble/AssembleModelGenerator.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/assemble/AssembleModelGenerator.kt index dc9fc6b1ad..30291d132a 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/assemble/AssembleModelGenerator.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/assemble/AssembleModelGenerator.kt @@ -241,7 +241,7 @@ class AssembleModelGenerator(private val basePackageName: String) { val modelName = nextModelName(compositeModel.classId.jClass.simpleName.decapitalize()) val constructorId = findBestConstructorOrNull(compositeModel) - ?: throw AssembleException("No default constructor to instantiate an object of the class ${compositeModel.id}") + ?: throw AssembleException("No default constructor to instantiate an object of the class ${compositeModel.classId}") val constructorInfo = constructorAnalyzer.analyze(constructorId) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/modifications/ConstructorAnalyzer.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/modifications/ConstructorAnalyzer.kt index 6284c344ea..d022c03e33 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/modifications/ConstructorAnalyzer.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/modifications/ConstructorAnalyzer.kt @@ -7,6 +7,7 @@ import org.utbot.framework.plugin.api.id import org.utbot.framework.plugin.api.util.isArray import org.utbot.framework.plugin.api.util.isRefType import org.utbot.framework.plugin.api.util.jClass +import org.utbot.framework.util.kotlinIntrinsicsClassId import soot.Scene import soot.SootMethod import soot.Type @@ -173,8 +174,8 @@ class ConstructorAnalyzer { val jimpleLocal = assn.rightOp as? JimpleLocal ?: continue val field = (assn.leftOp as? JInstanceFieldRef)?.field ?: continue - val parameterIndex = jimpleBody.locals.indexOfFirst { it.name == jimpleLocal.name } - indexedFields[parameterIndex - 1] = FieldId(field.declaringClass.id, field.name) + val parameterIndex = jimpleBody.parameterLocals.indexOfFirst { it.name == jimpleLocal.name } + indexedFields[parameterIndex] = FieldId(field.declaringClass.id, field.name) } return indexedFields @@ -226,6 +227,8 @@ class ConstructorAnalyzer { jimpleBody.units .filterIsInstance() .map { it.invokeExpr } + // These are instructions inserted by Kotlin compiler to check that arguments are not null, we should ignore them + .filterNot { it.method.declaringClass.id == kotlinIntrinsicsClassId } private fun sameParameterTypes(sootMethod: SootMethod, constructorId: ConstructorId): Boolean { val sootConstructorTypes = sootMethod.parameterTypes diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/util/KotlinIntrinsicsUtil.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/util/KotlinIntrinsicsUtil.kt new file mode 100644 index 0000000000..4fe407602b --- /dev/null +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/util/KotlinIntrinsicsUtil.kt @@ -0,0 +1,10 @@ +package org.utbot.framework.util + +import org.utbot.framework.plugin.api.BuiltinClassId + +val kotlinIntrinsicsClassId: BuiltinClassId + get() = BuiltinClassId( + simpleName = "Intrinsics", + canonicalName = "kotlin.jvm.internal.Intrinsics", + packageName = "kotlin.jvm.internal" + ) \ No newline at end of file