Skip to content

Fix unnecessary reflection calls in the generated code #1491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ class AssembleModelGenerator(private val basePackageName: String) {
assembleModel
}

private val modelsInAnalysis = mutableListOf<UtCompositeModel>()

/**
* Assembles internal structure of [UtCompositeModel] if possible and handles assembling exceptions.
*/
Expand All @@ -243,7 +245,14 @@ class AssembleModelGenerator(private val basePackageName: String) {
val constructorId = findBestConstructorOrNull(compositeModel)
?: throw AssembleException("No default constructor to instantiate an object of the class ${compositeModel.classId}")

val constructorInfo = constructorAnalyzer.analyze(constructorId)
// we do not analyze a constructor which is currently in the analysis
// thus, we do not encounter an infinite loop in self or cross-reference situations
val shouldAnalyzeConstructor = compositeModel !in modelsInAnalysis
modelsInAnalysis.add(compositeModel)

val constructorInfo =
if (shouldAnalyzeConstructor) constructorAnalyzer.analyze(constructorId)
else ConstructorAssembleInfo(constructorId)

val instantiationCall = constructorCall(compositeModel, constructorInfo)
return UtAssembleModel(
Expand Down Expand Up @@ -284,6 +293,8 @@ class AssembleModelGenerator(private val basePackageName: String) {
} catch (e: AssembleException) {
instantiatedModels.remove(compositeModel)
throw e
} finally {
modelsInAnalysis.remove(compositeModel)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import soot.jimple.internal.JimpleLocal
* */
data class ConstructorAssembleInfo(
val constructorId: ConstructorId,
val params: Map<Int, FieldId>,
val setFields: Set<FieldId>,
val affectedFields: Set<FieldId>
val params: Map<Int, FieldId> = mapOf(),
val setFields: Set<FieldId> = setOf(),
val affectedFields: Set<FieldId> = setOf()
)

/**
Expand Down Expand Up @@ -109,18 +109,11 @@ class ConstructorAnalyzer {
return jimpleLocal.name.first() != '$'
}

private val visitedConstructors = mutableSetOf<SootMethod>()

private fun analyze(
sootConstructor: SootMethod,
setFields: MutableSet<FieldId>,
affectedFields: MutableSet<FieldId>,
): Map<Int, FieldId> {
if (sootConstructor in visitedConstructors) {
return emptyMap()
}
visitedConstructors.add(sootConstructor)

val jimpleBody = retrieveJimpleBody(sootConstructor) ?: return emptyMap()
analyzeAssignments(jimpleBody, setFields, affectedFields)

Expand Down