Skip to content

Commit 30499ad

Browse files
authored
Handle not-nullable parameters in Kotlin constructors correctly #1336 (#1352)
1 parent 72b8c13 commit 30499ad

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/assemble/AssembleModelGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class AssembleModelGenerator(private val basePackageName: String) {
241241
val modelName = nextModelName(compositeModel.classId.jClass.simpleName.decapitalize())
242242

243243
val constructorId = findBestConstructorOrNull(compositeModel)
244-
?: throw AssembleException("No default constructor to instantiate an object of the class ${compositeModel.id}")
244+
?: throw AssembleException("No default constructor to instantiate an object of the class ${compositeModel.classId}")
245245

246246
val constructorInfo = constructorAnalyzer.analyze(constructorId)
247247

utbot-framework/src/main/kotlin/org/utbot/framework/modifications/ConstructorAnalyzer.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.utbot.framework.plugin.api.id
77
import org.utbot.framework.plugin.api.util.isArray
88
import org.utbot.framework.plugin.api.util.isRefType
99
import org.utbot.framework.plugin.api.util.jClass
10+
import org.utbot.framework.util.kotlinIntrinsicsClassId
1011
import soot.Scene
1112
import soot.SootMethod
1213
import soot.Type
@@ -173,8 +174,8 @@ class ConstructorAnalyzer {
173174
val jimpleLocal = assn.rightOp as? JimpleLocal ?: continue
174175

175176
val field = (assn.leftOp as? JInstanceFieldRef)?.field ?: continue
176-
val parameterIndex = jimpleBody.locals.indexOfFirst { it.name == jimpleLocal.name }
177-
indexedFields[parameterIndex - 1] = FieldId(field.declaringClass.id, field.name)
177+
val parameterIndex = jimpleBody.parameterLocals.indexOfFirst { it.name == jimpleLocal.name }
178+
indexedFields[parameterIndex] = FieldId(field.declaringClass.id, field.name)
178179
}
179180

180181
return indexedFields
@@ -226,6 +227,8 @@ class ConstructorAnalyzer {
226227
jimpleBody.units
227228
.filterIsInstance<JInvokeStmt>()
228229
.map { it.invokeExpr }
230+
// These are instructions inserted by Kotlin compiler to check that arguments are not null, we should ignore them
231+
.filterNot { it.method.declaringClass.id == kotlinIntrinsicsClassId }
229232

230233
private fun sameParameterTypes(sootMethod: SootMethod, constructorId: ConstructorId): Boolean {
231234
val sootConstructorTypes = sootMethod.parameterTypes
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.utbot.framework.util
2+
3+
import org.utbot.framework.plugin.api.BuiltinClassId
4+
5+
val kotlinIntrinsicsClassId: BuiltinClassId
6+
get() = BuiltinClassId(
7+
simpleName = "Intrinsics",
8+
canonicalName = "kotlin.jvm.internal.Intrinsics",
9+
packageName = "kotlin.jvm.internal"
10+
)

0 commit comments

Comments
 (0)