Skip to content

Commit de865a5

Browse files
committed
Fix unnecessary reflection calls and infinite loop in self-reference initializations
1 parent e4cf410 commit de865a5

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ class AssembleModelGenerator(private val basePackageName: String) {
226226
assembleModel
227227
}
228228

229+
private val modelsInAnalysis = mutableListOf<UtCompositeModel>()
230+
229231
/**
230232
* Assembles internal structure of [UtCompositeModel] if possible and handles assembling exceptions.
231233
*/
@@ -243,7 +245,14 @@ class AssembleModelGenerator(private val basePackageName: String) {
243245
val constructorId = findBestConstructorOrNull(compositeModel)
244246
?: throw AssembleException("No default constructor to instantiate an object of the class ${compositeModel.classId}")
245247

246-
val constructorInfo = constructorAnalyzer.analyze(constructorId)
248+
// we do not analyze a constructor which is currently in the analysis
249+
// thus, we do not encounter an infinite loop in self or cross-reference situations
250+
val shouldAnalyzeConstructor = compositeModel !in modelsInAnalysis
251+
modelsInAnalysis.add(compositeModel)
252+
253+
val constructorInfo =
254+
if (shouldAnalyzeConstructor) constructorAnalyzer.analyze(constructorId)
255+
else ConstructorAssembleInfo(constructorId)
247256

248257
val instantiationCall = constructorCall(compositeModel, constructorInfo)
249258
return UtAssembleModel(
@@ -284,6 +293,8 @@ class AssembleModelGenerator(private val basePackageName: String) {
284293
} catch (e: AssembleException) {
285294
instantiatedModels.remove(compositeModel)
286295
throw e
296+
} finally {
297+
modelsInAnalysis.remove(compositeModel)
287298
}
288299
}
289300

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ import soot.jimple.internal.JimpleLocal
3333
* */
3434
data class ConstructorAssembleInfo(
3535
val constructorId: ConstructorId,
36-
val params: Map<Int, FieldId>,
37-
val setFields: Set<FieldId>,
38-
val affectedFields: Set<FieldId>
36+
val params: Map<Int, FieldId> = mapOf(),
37+
val setFields: Set<FieldId> = setOf(),
38+
val affectedFields: Set<FieldId> = setOf()
3939
)
4040

4141
/**
@@ -109,18 +109,11 @@ class ConstructorAnalyzer {
109109
return jimpleLocal.name.first() != '$'
110110
}
111111

112-
private val visitedConstructors = mutableSetOf<SootMethod>()
113-
114112
private fun analyze(
115113
sootConstructor: SootMethod,
116114
setFields: MutableSet<FieldId>,
117115
affectedFields: MutableSet<FieldId>,
118116
): Map<Int, FieldId> {
119-
if (sootConstructor in visitedConstructors) {
120-
return emptyMap()
121-
}
122-
visitedConstructors.add(sootConstructor)
123-
124117
val jimpleBody = retrieveJimpleBody(sootConstructor) ?: return emptyMap()
125118
analyzeAssignments(jimpleBody, setFields, affectedFields)
126119

0 commit comments

Comments
 (0)