Skip to content

Commit f43a15d

Browse files
Add assemble model heuristic
1 parent 248c196 commit f43a15d

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,10 @@ class AssembleModelGenerator(private val methodUnderTest: UtMethod<*>) {
357357
/**
358358
* Finds most appropriate constructor in class.
359359
*
360-
* We prefer constructor that allows to set more fields than others
360+
* If the [compositeModel].fields is empty, we don't care about affected fields, we would like to take an empty
361+
* constructor or an appropriate constructor with the least number of arguments.
362+
*
363+
* Otherwise, we prefer constructor that allows to set more fields than others
361364
* and use only simple assignments like "this.a = a".
362365
*
363366
* Returns null if no one appropriate constructor is found.
@@ -366,11 +369,19 @@ class AssembleModelGenerator(private val methodUnderTest: UtMethod<*>) {
366369
val classId = compositeModel.classId
367370
if (!classId.isVisible || classId.isInner) return null
368371

369-
return classId.jClass.declaredConstructors
372+
val constructorIds = classId.jClass.declaredConstructors
370373
.filter { it.isVisible }
371-
.sortedByDescending { it.parameterCount }
372374
.map { it.executableId }
373-
.firstOrNull { constructorAnalyzer.isAppropriate(it) }
375+
376+
return if (compositeModel.fields.isEmpty()) {
377+
constructorIds
378+
.sortedBy { it.parameters.size }
379+
.firstOrNull { it.parameters.isEmpty() || constructorAnalyzer.isAppropriate(it) }
380+
} else {
381+
constructorIds
382+
.sortedByDescending { it.parameters.size }
383+
.firstOrNull { constructorAnalyzer.isAppropriate(it) }
384+
}
374385
}
375386

376387
private val ClassId.isVisible : Boolean

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class ConstructorAnalyzer {
172172
for (assn in assignments) {
173173
val jimpleLocal = assn.rightOp as? JimpleLocal ?: continue
174174

175-
val field = (assn.leftOp as JInstanceFieldRef).field
175+
val field = (assn.leftOp as? JInstanceFieldRef)?.field ?: continue
176176
val parameterIndex = jimpleBody.locals.indexOfFirst { it.name == jimpleLocal.name }
177177
indexedFields[parameterIndex - 1] = FieldId(field.declaringClass.id, field.name)
178178
}

0 commit comments

Comments
 (0)