Skip to content

Commit ba08cb7

Browse files
EgorkaKulikovAbdullinAM
authored andcommittedOct 17, 2022
Modificators processing in AssembleModelGenerator and UtBotFieldModificatorsSearcher corrected (UnitTestBot#1029)
* Improve modificators analysis in UtBotFieldModificatorsSearcher and AssembleModelGenerator * Apply review comments * Fixed test * One more correction for package names

35 files changed

+104
-107
lines changed
 
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.arrays;
1+
package org.utbot.examples.assemble;
22

33
/**
44
* A class with array of objects that are arrays of complex fields themselves.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.arrays;
1+
package org.utbot.examples.assemble;
22

33
/**
44
* A class with a two-dimensional array field.

‎utbot-framework-test/src/main/java/org/utbot/examples/assemble/arrays/AssignedArray.java renamed to ‎utbot-framework-test/src/main/java/org/utbot/examples/assemble/AssignedArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.arrays;
1+
package org.utbot.examples.assemble;
22

33
/**
44
* A class with an array with a default value.

‎utbot-framework-test/src/main/java/org/utbot/examples/assemble/arrays/ComplexArray.java renamed to ‎utbot-framework-test/src/main/java/org/utbot/examples/assemble/ComplexArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.arrays;
1+
package org.utbot.examples.assemble;
22

33
import org.utbot.examples.assemble.PrimitiveFields;
44

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.constructors;
1+
package org.utbot.examples.assemble;
22

33
/**
44
* A class without default constructor and with complex one.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.constructors;
1+
package org.utbot.examples.assemble;
22

33
/**
44
* A class without default constructor and with complex one,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.constructors;
1+
package org.utbot.examples.assemble;
22

33
public class ConstructorModifyingStatic {
44

‎utbot-framework-test/src/main/java/org/utbot/examples/assemble/defaults/DefaultField.java renamed to ‎utbot-framework-test/src/main/java/org/utbot/examples/assemble/DefaultField.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.defaults;
1+
package org.utbot.examples.assemble;
22

33
/**
44
* A class with a field with default value that is not a default value of type.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package org.utbot.examples.assemble.defaults;
1+
package org.utbot.examples.assemble;
22

33
public class DefaultFieldModifiedInConstructor {
4-
int z;
4+
public int z;
55

66
@SuppressWarnings("Unused")
77
DefaultFieldModifiedInConstructor(int z_) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.defaults;
1+
package org.utbot.examples.assemble;
22

33
/**
44
* A class with a field with default value that is not a default value of type.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.defaults;
1+
package org.utbot.examples.assemble;
22

33
/**
44
* A class with a field with setter default value that is not a default value of type.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.utbot.examples.assemble;
2+
3+
/**
4+
* Need to be located at the same package as [AssembleTestUtils]
5+
* because requires a setter for package-private field.
6+
*/
7+
public class DefaultPackagePrivateField {
8+
int z = 10;
9+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.constructors;
1+
package org.utbot.examples.assemble;
22

33
/**
44
* A class with a primitive constructor that inherits a complex constructor.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.constructors;
1+
package org.utbot.examples.assemble;
22

33
/**
44
* A class with a primitive constructor that inherits another primitive constructor.

‎utbot-framework-test/src/main/java/org/utbot/examples/assemble/arrays/PrimitiveArray.java renamed to ‎utbot-framework-test/src/main/java/org/utbot/examples/assemble/PrimitiveArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.arrays;
1+
package org.utbot.examples.assemble;
22

33
/**
44
* A class with an array with elements of primitive type.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.constructors;
1+
package org.utbot.examples.assemble;
22

33
/**
44
* A class without default constructor and with primitive one.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.constructors;
1+
package org.utbot.examples.assemble;
22

33
/**
44
* A class without default constructor and with another one with default field
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.constructors;
1+
package org.utbot.examples.assemble;
22

33
/**
44
* A class with private constructor.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.constructors;
1+
package org.utbot.examples.assemble;
22

33
/**
44
* A class with a constructor that seems to be complex

‎utbot-framework-test/src/main/java/org/utbot/examples/assemble/statics/StaticField.java renamed to ‎utbot-framework-test/src/main/java/org/utbot/examples/assemble/StaticField.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.statics;
1+
package org.utbot.examples.assemble;
22

33
/**
44
* A class with primitive constructor and static field

‎utbot-framework-test/src/main/java/org/utbot/examples/assemble/arrays/MethodUnderTest.java renamed to ‎utbot-framework-test/src/main/java/org/utbot/examples/assemble/another/MethodUnderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.utbot.examples.assemble.arrays;
1+
package org.utbot.examples.assemble.another;
22

33
/**
44
* A test class with fake method under test.

‎utbot-framework-test/src/main/java/org/utbot/examples/assemble/defaults/DefaultPackagePrivateField.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎utbot-framework-test/src/main/java/org/utbot/examples/manual/examples/ArrayOfComplexArraysExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.utbot.examples.manual.examples;
22

3-
import org.utbot.examples.assemble.arrays.ArrayOfComplexArrays;
3+
import org.utbot.examples.assemble.ArrayOfComplexArrays;
44

55
public class ArrayOfComplexArraysExample {
66
public int getValue(ArrayOfComplexArrays a) {

‎utbot-framework-test/src/main/java/org/utbot/examples/manual/examples/ArrayOfPrimitiveArraysExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.utbot.examples.manual.examples;
22

3-
import org.utbot.examples.assemble.arrays.ArrayOfPrimitiveArrays;
3+
import org.utbot.examples.assemble.ArrayOfPrimitiveArrays;
44

55
public class ArrayOfPrimitiveArraysExample {
66
int assign10(ArrayOfPrimitiveArrays a) {

‎utbot-framework-test/src/main/java/org/utbot/examples/manual/examples/AssignedArrayExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.utbot.examples.manual.examples;
22

3-
import org.utbot.examples.assemble.arrays.AssignedArray;
3+
import org.utbot.examples.assemble.AssignedArray;
44

55
public class AssignedArrayExample {
66
public void foo(AssignedArray aa) {

‎utbot-framework-test/src/test/java/org/utbot/examples/manual/UtBotJavaApiTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import org.utbot.common.PathUtil;
1010
import org.utbot.examples.assemble.DirectAccess;
1111
import org.utbot.examples.assemble.PrimitiveFields;
12-
import org.utbot.examples.assemble.arrays.ArrayOfComplexArrays;
13-
import org.utbot.examples.assemble.arrays.ArrayOfPrimitiveArrays;
14-
import org.utbot.examples.assemble.arrays.AssignedArray;
15-
import org.utbot.examples.assemble.arrays.ComplexArray;
12+
import org.utbot.examples.assemble.ArrayOfComplexArrays;
13+
import org.utbot.examples.assemble.ArrayOfPrimitiveArrays;
14+
import org.utbot.examples.assemble.AssignedArray;
15+
import org.utbot.examples.assemble.ComplexArray;
1616
import org.utbot.examples.manual.examples.*;
1717
import org.utbot.examples.manual.examples.customer.B;
1818
import org.utbot.examples.manual.examples.customer.C;

‎utbot-framework-test/src/test/kotlin/org/utbot/framework/assemble/AssembleModelGeneratorTests.kt

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,27 @@ import org.utbot.examples.assemble.ListItem
1616
import org.utbot.examples.assemble.NoModifier
1717
import org.utbot.examples.assemble.PackagePrivateFields
1818
import org.utbot.examples.assemble.PrimitiveFields
19-
import org.utbot.examples.assemble.arrays.ArrayOfComplexArrays
20-
import org.utbot.examples.assemble.arrays.ArrayOfPrimitiveArrays
21-
import org.utbot.examples.assemble.arrays.AssignedArray
22-
import org.utbot.examples.assemble.arrays.ComplexArray
23-
import org.utbot.examples.assemble.arrays.MethodUnderTest
24-
import org.utbot.examples.assemble.arrays.PrimitiveArray
25-
import org.utbot.examples.assemble.constructors.ComplexConstructor
26-
import org.utbot.examples.assemble.constructors.ComplexConstructorWithSetter
27-
import org.utbot.examples.assemble.constructors.ConstructorModifyingStatic
28-
import org.utbot.examples.assemble.constructors.InheritComplexConstructor
29-
import org.utbot.examples.assemble.constructors.InheritPrimitiveConstructor
30-
import org.utbot.examples.assemble.constructors.PrimitiveConstructor
31-
import org.utbot.examples.assemble.constructors.PrimitiveConstructorWithDefaultField
32-
import org.utbot.examples.assemble.constructors.PrivateConstructor
33-
import org.utbot.examples.assemble.constructors.PseudoComplexConstructor
34-
import org.utbot.examples.assemble.defaults.DefaultField
35-
import org.utbot.examples.assemble.defaults.DefaultFieldModifiedInConstructor
36-
import org.utbot.examples.assemble.defaults.DefaultFieldWithDirectAccessor
37-
import org.utbot.examples.assemble.defaults.DefaultFieldWithSetter
38-
import org.utbot.examples.assemble.defaults.DefaultPackagePrivateField
39-
import org.utbot.examples.assemble.statics.StaticField
19+
import org.utbot.examples.assemble.ArrayOfComplexArrays
20+
import org.utbot.examples.assemble.ArrayOfPrimitiveArrays
21+
import org.utbot.examples.assemble.AssignedArray
22+
import org.utbot.examples.assemble.ComplexArray
23+
import org.utbot.examples.assemble.another.MethodUnderTest
24+
import org.utbot.examples.assemble.PrimitiveArray
25+
import org.utbot.examples.assemble.ComplexConstructor
26+
import org.utbot.examples.assemble.ComplexConstructorWithSetter
27+
import org.utbot.examples.assemble.ConstructorModifyingStatic
28+
import org.utbot.examples.assemble.InheritComplexConstructor
29+
import org.utbot.examples.assemble.InheritPrimitiveConstructor
30+
import org.utbot.examples.assemble.PrimitiveConstructor
31+
import org.utbot.examples.assemble.PrimitiveConstructorWithDefaultField
32+
import org.utbot.examples.assemble.PrivateConstructor
33+
import org.utbot.examples.assemble.PseudoComplexConstructor
34+
import org.utbot.examples.assemble.DefaultField
35+
import org.utbot.examples.assemble.DefaultFieldModifiedInConstructor
36+
import org.utbot.examples.assemble.DefaultFieldWithDirectAccessor
37+
import org.utbot.examples.assemble.DefaultFieldWithSetter
38+
import org.utbot.examples.assemble.DefaultPackagePrivateField
39+
import org.utbot.examples.assemble.StaticField
4040
import org.utbot.framework.plugin.api.ClassId
4141
import org.utbot.framework.plugin.api.ExecutableId
4242
import org.utbot.framework.plugin.api.FieldId
@@ -58,7 +58,6 @@ import org.utbot.framework.util.SootUtils
5858
import org.utbot.framework.util.instanceCounter
5959
import org.utbot.framework.util.modelIdCounter
6060
import kotlin.reflect.full.functions
61-
import org.utbot.examples.assemble.*
6261
import org.utbot.framework.codegen.model.constructor.util.arrayTypeOf
6362

6463
/**
@@ -150,8 +149,7 @@ class AssembleModelGeneratorTests {
150149
fields(testClassId, "a" to 5, "b" to 3)
151150
)
152151

153-
val methodFromAnotherPackage =
154-
MethodUnderTest::class.functions.first()
152+
val methodFromAnotherPackage = MethodUnderTest::class.functions.first()
155153

156154
createModelAndAssert(compositeModel, null, methodFromAnotherPackage.executableId)
157155
}
@@ -413,7 +411,7 @@ class AssembleModelGeneratorTests {
413411
val baseClassId = PrimitiveFields::class.id
414412

415413
val thisFields = fields(inheritedFieldClassId, "i" to 5, "d" to 3.0)
416-
val baseFields = fields(baseClassId, "a" to 2, "b" to 4)
414+
val baseFields = fields(baseClassId, "b" to 4)
417415

418416
val compositeModel = UtCompositeModel(
419417
modelIdCounter.incrementAndGet(),
@@ -425,7 +423,6 @@ class AssembleModelGeneratorTests {
425423
val v1 = statementsChain.addExpectedVariableDecl<InheritedField>()
426424
statementsChain.add("$v1." + ("i" `=` 5))
427425
statementsChain.add("$v1." + ("d" `=` 3.0))
428-
statementsChain.add("$v1." + addExpectedSetter("a", 2))
429426
statementsChain.add("$v1." + ("b" `=` 4))
430427

431428
val expectedRepresentation = printExpectedModel(inheritedFieldClassId.simpleName, v1, statementsChain)
@@ -1448,9 +1445,9 @@ class AssembleModelGeneratorTests {
14481445
private fun createModelsAndAssert(
14491446
models: List<UtModel>,
14501447
expectedModelRepresentations: List<String?>,
1451-
assembleTestUtils: ExecutableId = AssembleTestUtils::class.id.allMethods.first(),
1448+
assembleTestDummyMethod: ExecutableId = AssembleTestUtils::class.id.allMethods.first(),
14521449
) {
1453-
val modelsMap = AssembleModelGenerator(assembleTestUtils.classId.packageName).createAssembleModels(models)
1450+
val modelsMap = AssembleModelGenerator(assembleTestDummyMethod.classId.packageName).createAssembleModels(models)
14541451
//we sort values to fix order of models somehow (IdentityHashMap does not guarantee the order)
14551452
val assembleModels = modelsMap.values
14561453
.filterIsInstance<UtAssembleModel>()

‎utbot-framework-test/src/test/kotlin/org/utbot/framework/modificators/UtBotFieldModificatorsTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ internal class UtBotFieldModificatorsTest {
192192

193193
//We use sorting here to make comparing with sorted in advance expected collections easier
194194
private fun runFieldModificatorsSearch(analysisMode: AnalysisMode) =
195-
fieldsModificatorsSearcher.findModificators(analysisMode, PrimitiveModifications::class.java.packageName)
195+
fieldsModificatorsSearcher.findModificators(analysisMode)
196196
.map { (key, value) ->
197197
val modificatorNames = value.filterNot { it.name.startsWith("direct_set_") }.map { it.name }
198198
key.name to modificatorNames.toSortedSet()

‎utbot-framework/src/main/kotlin/org/utbot/external/api/UtModelFactory.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.utbot.framework.plugin.api.UtArrayModel
88
import org.utbot.framework.plugin.api.UtClassRefModel
99
import org.utbot.framework.plugin.api.UtCompositeModel
1010
import org.utbot.framework.plugin.api.UtModel
11+
import org.utbot.framework.plugin.api.util.executableId
1112
import org.utbot.framework.plugin.api.util.id
1213
import java.lang.reflect.Field
1314
import java.lang.reflect.Method

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@ import org.utbot.framework.modifications.UtBotFieldsModificatorsSearcher
1313
import org.utbot.framework.plugin.api.*
1414
import org.utbot.framework.plugin.api.util.defaultValueModel
1515
import org.utbot.framework.plugin.api.util.executableId
16+
import org.utbot.framework.plugin.api.util.isSubtypeOf
1617
import org.utbot.framework.plugin.api.util.jClass
1718
import org.utbot.framework.util.nextModelName
1819
import java.lang.reflect.Constructor
1920
import java.util.IdentityHashMap
2021

2122
/**
2223
* Creates [UtAssembleModel] from any [UtModel] or it's inner models if possible
23-
* during generation test for [methodUnderTest].
24+
* during generation test for [basePackageName].
2425
*
2526
* Needs utContext be set and Soot be initialized.
2627
*
2728
* Note: Caches class related information, can be reused if classes don't change.
2829
*/
29-
class AssembleModelGenerator(private val methodPackageName: String) {
30+
class AssembleModelGenerator(private val basePackageName: String) {
3031

3132
//Instantiated models are stored to avoid cyclic references during reference graph analysis
3233
private val instantiatedModels: IdentityHashMap<UtModel, UtReferenceModel> =
@@ -148,7 +149,7 @@ class AssembleModelGenerator(private val methodPackageName: String) {
148149
private fun assembleModel(utModel: UtModel): UtModel {
149150
val collectedCallChain = callChain.toMutableList()
150151

151-
// we cannot create an assemble model for an anonymous class instance
152+
// We cannot create an assemble model for an anonymous class instance
152153
if (utModel.classId.isAnonymous) {
153154
return utModel
154155
}
@@ -234,7 +235,7 @@ class AssembleModelGenerator(private val methodPackageName: String) {
234235
if (fieldId.isFinal) {
235236
throw AssembleException("Final field $fieldId can't be set in an object of the class $classId")
236237
}
237-
if (!fieldId.type.isAccessibleFrom(methodPackageName)) {
238+
if (!fieldId.type.isAccessibleFrom(basePackageName)) {
238239
throw AssembleException(
239240
"Field $fieldId can't be set in an object of the class $classId because its type is inaccessible"
240241
)
@@ -243,7 +244,8 @@ class AssembleModelGenerator(private val methodPackageName: String) {
243244
if (fieldId in constructorInfo.affectedFields ||
244245
(fieldId !in constructorInfo.setFields && !fieldModel.hasDefaultValue())
245246
) {
246-
val modifierCall = modifierCall(this, fieldId, assembleModel(fieldModel))
247+
val assembledModel = assembleModel(fieldModel)
248+
val modifierCall = modifierCall(this, fieldId, assembledModel)
247249
callChain.add(modifierCall)
248250
}
249251
}
@@ -375,10 +377,10 @@ class AssembleModelGenerator(private val methodPackageName: String) {
375377
}
376378

377379
private val ClassId.isVisible : Boolean
378-
get() = this.isPublic || !this.isPrivate && this.packageName.startsWith(methodPackageName)
380+
get() = this.isPublic || !this.isPrivate && this.packageName == basePackageName
379381

380382
private val Constructor<*>.isVisible : Boolean
381-
get() = this.isPublic || !this.isPrivate && this.declaringClass.packageName.startsWith(methodPackageName)
383+
get() = this.isPublic || !this.isPrivate && this.declaringClass.packageName == basePackageName
382384

383385
/**
384386
* Creates setter or direct setter call to set a field.
@@ -392,7 +394,7 @@ class AssembleModelGenerator(private val methodPackageName: String) {
392394
): UtStatementModel {
393395
val declaringClassId = fieldId.declaringClass
394396

395-
val modifiers = getOrFindSettersAndDirectAccessors(declaringClassId)
397+
val modifiers = getOrFindSettersAndDirectAccessors(instance.classId)
396398
val modifier = modifiers[fieldId]
397399
?: throw AssembleException("No setter for field ${fieldId.name} of class ${declaringClassId.name}")
398400

@@ -417,9 +419,7 @@ class AssembleModelGenerator(private val methodPackageName: String) {
417419
* Finds setters and direct accessors for fields of particular class.
418420
*/
419421
private fun findSettersAndDirectAccessors(classId: ClassId): Map<FieldId, StatementId> {
420-
val allModificatorsOfClass = modificatorsSearcher
421-
.findModificators(SettersAndDirectAccessors, methodPackageName)
422-
.map { it.key to it.value.filter { st -> st.classId == classId } }
422+
val allModificatorsOfClass = modificatorsSearcher.findModificators(SettersAndDirectAccessors)
423423

424424
return allModificatorsOfClass
425425
.mapNotNull { (fieldId, possibleModificators) ->
@@ -435,9 +435,12 @@ class AssembleModelGenerator(private val methodPackageName: String) {
435435
*/
436436
private fun chooseModificator(
437437
fieldId: FieldId,
438-
settersAndDirectAccessors: List<StatementId>
438+
settersAndDirectAccessors: Set<StatementId>,
439439
): StatementId? {
440-
val directAccessors = settersAndDirectAccessors.filterIsInstance<DirectFieldAccessId>()
440+
val directAccessors = settersAndDirectAccessors
441+
.filterIsInstance<DirectFieldAccessId>()
442+
.filter {it.fieldId.isAccessibleFrom(basePackageName) }
443+
441444
if (directAccessors.any()) {
442445
return directAccessors.singleOrNull()
443446
?: throw AssembleException(
@@ -446,7 +449,9 @@ class AssembleModelGenerator(private val methodPackageName: String) {
446449
}
447450

448451
if (settersAndDirectAccessors.any()) {
449-
return settersAndDirectAccessors.singleOrNull()
452+
return settersAndDirectAccessors
453+
.filterIsInstance<ExecutableId>()
454+
.singleOrNull { it.isAccessibleFrom(basePackageName) }
450455
?: throw AssembleException(
451456
"Field $fieldId has more than one setter: ${settersAndDirectAccessors.joinToString(" ")}"
452457
)

‎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/codegen/model/util/FieldIdUtil.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ import org.utbot.framework.plugin.api.util.voidClassId
1313
*
1414
* @param context context in which code is generated (it is needed because the method needs to know package and language)
1515
*/
16-
// TODO: change parameter from packageName: String to context: CgContext in ClassId.isAccessibleFrom and ExecutableId.isAccessibleFrom ?
17-
private fun FieldId.isAccessibleFrom(context: CgContext): Boolean {
18-
val packageName = context.testClassPackageName
16+
fun FieldId.isAccessibleFrom(packageName: String): Boolean {
1917
val isClassAccessible = declaringClass.isAccessibleFrom(packageName)
2018
val isAccessibleByVisibility = isPublic || (declaringClass.packageName == packageName && (isPackagePrivate || isProtected))
2119
val isAccessibleFromPackageByModifiers = isAccessibleByVisibility && !isSynthetic
@@ -36,7 +34,7 @@ internal infix fun FieldId.canBeReadFrom(context: CgContext): Boolean {
3634
return true
3735
}
3836

39-
return isAccessibleFrom(context)
37+
return isAccessibleFrom(context.testClassPackageName)
4038
}
4139

4240
private fun FieldId.canBeSetViaSetterFrom(context: CgContext): Boolean =
@@ -49,12 +47,12 @@ internal fun FieldId.canBeSetFrom(context: CgContext): Boolean {
4947
if (context.codegenLanguage == CodegenLanguage.KOTLIN) {
5048
// Kotlin will allow direct write access if both getter and setter is defined
5149
// !isAccessibleFrom(context) is important here because above rule applies to final fields only if they are not accessible in Java terms
52-
if (!isAccessibleFrom(context) && !isStatic && canBeReadViaGetterFrom(context) && canBeSetViaSetterFrom(context)) {
50+
if (!isAccessibleFrom(context.testClassPackageName) && !isStatic && canBeReadViaGetterFrom(context) && canBeSetViaSetterFrom(context)) {
5351
return true
5452
}
5553
}
5654

57-
return isAccessibleFrom(context) && !isFinal
55+
return isAccessibleFrom(context.testClassPackageName) && !isFinal
5856
}
5957

6058
/**

‎utbot-framework/src/main/kotlin/org/utbot/framework/concrete/UtExecutionInstrumentation.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.utbot.framework.UtSettings
88
import org.utbot.framework.assemble.AssembleModelGenerator
99
import org.utbot.framework.plugin.api.Coverage
1010
import org.utbot.framework.plugin.api.EnvironmentModels
11+
import org.utbot.framework.plugin.api.ExecutableId
1112
import org.utbot.framework.plugin.api.FieldId
1213
import org.utbot.framework.plugin.api.Instruction
1314
import org.utbot.framework.plugin.api.MissingState
@@ -98,9 +99,7 @@ class UtConcreteExecutionResult(
9899
*
99100
* @return [UtConcreteExecutionResult] with converted models.
100101
*/
101-
fun convertToAssemble(
102-
packageName: String
103-
): UtConcreteExecutionResult {
102+
fun convertToAssemble(packageName: String): UtConcreteExecutionResult {
104103
val allModels = collectAllModels()
105104

106105
val modelsToAssembleModels = AssembleModelGenerator(packageName).createAssembleModels(allModels)

‎utbot-framework/src/main/kotlin/org/utbot/framework/modifications/DirectAccessorsAnalyzer.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,24 @@ class DirectAccessorsAnalyzer {
1717
*/
1818
fun collectDirectAccesses(classIds: Set<ClassId>): Set<DirectFieldAccessId> =
1919
classIds
20-
.flatMap { classId -> collectFieldsInPackage(classId) }
20+
.flatMap { classId -> collectFields(classId) }
2121
.map { fieldId -> DirectFieldAccessId(fieldId.declaringClass, directSetterName(fieldId), fieldId) }
2222
.toSet()
2323

2424
/**
25-
* Collect all fields with different non-private modifiers from class [classId].
25+
* Collect all fields with different non-private modifiers
26+
* from class [classId] or it's base classes.
2627
*/
27-
private fun collectFieldsInPackage(classId: ClassId): Set<FieldId> {
28-
val clazz = classId.jClass
28+
private fun collectFields(classId: ClassId): Set<FieldId> {
29+
var clazz = classId.jClass
2930

3031
val fieldIds = mutableSetOf<Field>()
31-
fieldIds += clazz.fields
3232
fieldIds += clazz.declaredFields.filterNot { Modifier.isPrivate(it.modifiers) }
33+
while (clazz.superclass != null) {
34+
clazz = clazz.superclass
35+
fieldIds += clazz.declaredFields.filterNot { Modifier.isPrivate(it.modifiers) }
36+
}
37+
3338

3439
return fieldIds.map { it.fieldId }.toSet()
3540
}

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,8 @@ 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.
2019
*/
21-
fun findModificators(analysisMode: AnalysisMode, packageName: String? = null): Map<FieldId, Set<StatementId>> {
22-
val modificators = findModificators(analysisMode)
23-
if (packageName == null) {
24-
return modificators
25-
}
26-
27-
val filteredModifications = mutableMapOf<FieldId, Set<StatementId>>()
28-
for ((fieldId, statements) in modificators) {
29-
val filteredStmts = statements.filter { it.classId.packageName.startsWith(packageName) }.toSet()
30-
filteredModifications[fieldId] = filteredStmts
31-
}
32-
33-
return filteredModifications
34-
}
35-
36-
private fun findModificators(analysisMode: AnalysisMode): Map<FieldId, Set<StatementId>> {
20+
fun findModificators(analysisMode: AnalysisMode): Map<FieldId, Set<StatementId>> {
3721
statementsStorage.updateCaches()
3822
return findModificatorsInCache(analysisMode)
3923
}

0 commit comments

Comments
 (0)
Please sign in to comment.