Skip to content

Commit 122d408

Browse files
committed
Improve modificators analysis in UtBotFieldModificatorsSearcher
1 parent 0d2c65b commit 122d408

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgCallableAccessManager.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ internal class CgCallableAccessManagerImpl(val context: CgContext) : CgCallableA
129129
)
130130
}
131131
// we can access the field only via reflection
132+
133+
// NOTE that current implementation works only if field access is located
134+
// in the right part of the assignment. However, obtaining this construction
135+
// as an "l-value" seems to be an error in assemble models or somewhere else.
132136
is ReflectionOnly -> fieldId.accessWithReflection(this)
133137
}
134138
}

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,37 @@ class UtBotFieldsModificatorsSearcher {
1616
* Finds field modificators.
1717
*
1818
* @param analysisMode represents which type of modificators (e.g. setters) are considered.
19-
* @param packageName describes a location of package-private methods that need to be considered.
19+
* @param basePackageName describes a location of package-private methods that need to be considered.
2020
*/
21-
fun findModificators(analysisMode: AnalysisMode, packageName: String? = null): Map<FieldId, Set<StatementId>> {
21+
fun findModificators(analysisMode: AnalysisMode, basePackageName: String? = null): Map<FieldId, Set<StatementId>> {
2222
val modificators = findModificators(analysisMode)
23-
if (packageName == null) {
23+
24+
if (basePackageName == null) {
2425
return modificators
2526
}
2627

2728
val filteredModifications = mutableMapOf<FieldId, Set<StatementId>>()
2829
for ((fieldId, statements) in modificators) {
29-
val filteredStmts = statements.filter { it.classId.packageName.startsWith(packageName) }.toSet()
30-
filteredModifications[fieldId] = filteredStmts
30+
val filteredStmts = statements.filter { stmt -> fieldId.isAccessibleBy(stmt, basePackageName!!) }
31+
filteredModifications[fieldId] = filteredStmts.toSet()
3132
}
3233

3334
return filteredModifications
3435
}
3536

37+
private fun FieldId.isAccessibleBy(statementId: StatementId, basePackageName: String): Boolean {
38+
val classPackageName = statementId.classId.packageName
39+
if (basePackageName == "") {
40+
return classPackageName == ""
41+
} else {
42+
if (this.isPublic) return true
43+
if (this.isProtected) return classPackageName.startsWith(basePackageName)
44+
if (this.isPackagePrivate) return classPackageName == basePackageName
45+
//for private fields no filtering based on package names is required
46+
return true
47+
}
48+
}
49+
3650
private fun findModificators(analysisMode: AnalysisMode): Map<FieldId, Set<StatementId>> {
3751
statementsStorage.updateCaches()
3852
return findModificatorsInCache(analysisMode)

0 commit comments

Comments
 (0)