Skip to content

Commit 8a90b7d

Browse files
committed
Use mutating methods that affect the same fields as MUT
1 parent ee386a3 commit 8a90b7d

File tree

9 files changed

+54
-31
lines changed

9 files changed

+54
-31
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import org.junit.jupiter.api.Assertions.assertEquals
2424
import org.junit.jupiter.api.Assertions.assertTrue
2525
import org.junit.jupiter.api.BeforeEach
2626
import org.junit.jupiter.api.Test
27-
import org.utbot.common.nameOfPackage
2827
import org.utbot.framework.plugin.services.JdkInfoDefaultProvider
2928
import org.utbot.framework.util.SootUtils
29+
import soot.jimple.internal.JAssignStmt
30+
import soot.jimple.internal.JInstanceFieldRef
3031

3132
internal class UtBotFieldModificatorsTest {
3233
private lateinit var fieldsModificatorsSearcher: UtBotFieldsModificatorsSearcher
@@ -176,7 +177,9 @@ internal class UtBotFieldModificatorsTest {
176177
forceReload = false,
177178
jdkInfo = JdkInfoDefaultProvider().info
178179
)
179-
fieldsModificatorsSearcher = UtBotFieldsModificatorsSearcher()
180+
fieldsModificatorsSearcher = UtBotFieldsModificatorsSearcher(
181+
modificationsPredicate = { (it as JAssignStmt).leftOp as? JInstanceFieldRef }
182+
)
180183
}
181184

182185
private fun runUpdate(classes: Set<KClass<*>>) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ import org.utbot.framework.plugin.api.util.jClass
4848
import org.utbot.framework.util.nextModelName
4949
import java.lang.reflect.Constructor
5050
import java.util.IdentityHashMap
51+
import soot.jimple.internal.JAssignStmt
52+
import soot.jimple.internal.JInstanceFieldRef
5153

5254
/**
5355
* Creates [UtAssembleModel] from any [UtModel] or it's inner models if possible
@@ -72,7 +74,10 @@ class AssembleModelGenerator(private val basePackageName: String) {
7274
//Call chain of statements to create assemble model
7375
private var callChain = mutableListOf<UtStatementModel>()
7476

75-
private val modificatorsSearcher = UtBotFieldsModificatorsSearcher()
77+
private val modificatorsSearcher =
78+
UtBotFieldsModificatorsSearcher(
79+
modificationsPredicate = { (it as JAssignStmt).leftOp as? JInstanceFieldRef }
80+
)
7681
private val constructorAnalyzer = ConstructorAnalyzer()
7782

7883
/**

utbot-framework/src/main/kotlin/org/utbot/framework/context/spring/SpringIntegrationTestConcreteExecutionContext.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import org.utbot.instrumentation.instrumentation.execution.UtExecutionInstrument
4040
import org.utbot.instrumentation.instrumentation.spring.SpringUtExecutionInstrumentation
4141
import org.utbot.instrumentation.tryLoadingSpringContext
4242
import java.io.File
43+
import org.utbot.fuzzing.providers.ObjectValueProvider
44+
import org.utbot.fuzzing.providers.anyObjectValueProvider
4345

4446
class SpringIntegrationTestConcreteExecutionContext(
4547
private val delegateContext: ConcreteExecutionContext,
@@ -107,6 +109,8 @@ class SpringIntegrationTestConcreteExecutionContext(
107109
.withFallback(NotEmptyStringValueProvider())
108110
.withFallback(
109111
delegateContext.tryCreateValueProvider(concreteExecutor, classUnderTest, idGenerator)
112+
.except { p -> p is ObjectValueProvider }
113+
.with(anyObjectValueProvider(idGenerator, shouldMutateWithMethods = true))
110114
.with(ValidEntityValueProvider(idGenerator, onlyAcceptWhenValidIsRequired = false))
111115
.with(createGeneratedFieldValueProviders(relevantRepositories, idGenerator))
112116
.withFallback(AnyDepthNullValueProvider)

utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/JavaLanguage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fun defaultValueProviders(idGenerator: IdentityPreservingIdGenerator<Int>) = lis
4242
FloatValueProvider,
4343
StringValueProvider,
4444
NumberValueProvider,
45-
anyObjectValueProvider(idGenerator),
45+
anyObjectValueProvider(idGenerator, shouldMutateWithMethods = false),
4646
ArrayValueProvider(idGenerator),
4747
EnumValueProvider(idGenerator),
4848
ListSetValueProvider(idGenerator),

utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/providers/Objects.kt

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import java.lang.reflect.Modifier
1616
import java.lang.reflect.TypeVariable
1717
import org.utbot.modifications.AnalysisMode
1818
import org.utbot.modifications.UtBotFieldsModificatorsSearcher
19+
import soot.jimple.internal.JAssignStmt
20+
import soot.jimple.internal.JInstanceFieldRef
1921

2022
private val logger = KotlinLogging.logger {}
2123

@@ -264,11 +266,12 @@ internal fun findAccessibleModifiableFields(description: FuzzedDescription?, cla
264266
}
265267

266268
internal fun findAllAvailableMethods(
267-
description: FuzzedDescription?,
269+
description: FuzzedDescription,
268270
classId: ClassId,
269271
packageName: String?
270272
): List<MethodDescription> {
271-
val modifyingMethods = findModifyingMethodNames(classId)
273+
val methodUnderTestName = description.description.name.substringAfter(description.description.className + ".")
274+
val modifyingMethods = findModifyingMethodNames(methodUnderTestName, classId)
272275
return classId.jClass.declaredMethods.mapNotNull { method ->
273276
if (isAccessible(method, packageName)) {
274277
if (method.name !in modifyingMethods) return@mapNotNull null
@@ -278,12 +281,7 @@ internal fun findAllAvailableMethods(
278281
method
279282
.parameterTypes
280283
.map {
281-
if (description != null) {
282-
toFuzzerType(
283-
it,
284-
description.typeCache
285-
)
286-
} else FuzzedType(it.id)
284+
toFuzzerType(it, description.typeCache)
287285
}
288286

289287
MethodDescription(
@@ -327,13 +325,22 @@ internal fun isAccessible(clazz: Class<*>, packageName: String?): Boolean {
327325
(packageName != null && isNotPrivateOrProtected(clazz.modifiers) && clazz.`package`?.name == packageName)
328326
}
329327

330-
private fun findModifyingMethodNames(classId: ClassId) =
331-
UtBotFieldsModificatorsSearcher()
328+
private fun findModifyingMethodNames(methodUnderTestName: String, classId: ClassId) =
329+
UtBotFieldsModificatorsSearcher(
330+
modificationsPredicate = { (it as JAssignStmt).leftOp as? JInstanceFieldRef ?: it.rightOp as? JInstanceFieldRef }
331+
)
332332
.let { searcher ->
333333
searcher.update(setOf(classId))
334-
searcher.getModificatorToFields(AnalysisMode.Methods)
335-
.keys
336-
.mapTo(mutableSetOf()) { it.name }
334+
val modificatorsToFields = searcher.getModificatorToFields(AnalysisMode.Methods)
335+
336+
modificatorsToFields[methodUnderTestName]
337+
?.let { fieldsModifiedByMUT ->
338+
modificatorsToFields.mapNotNull {
339+
if (it.key == methodUnderTestName || it.value.intersect(fieldsModifiedByMUT).isEmpty()) null
340+
else it.key
341+
}
342+
}
343+
?: setOf()
337344
}
338345

339346
private fun isNotPrivateOrProtected(modifiers: Int): Boolean {

utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/spring/SpringBeanValueProvider.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ import org.utbot.fuzzer.FuzzedValue
1212
import org.utbot.fuzzer.IdGenerator
1313
import org.utbot.fuzzer.fuzzed
1414
import org.utbot.fuzzing.*
15-
import org.utbot.fuzzing.providers.MethodDescription
1615
import org.utbot.fuzzing.providers.SPRING_BEAN_PROP
1716
import org.utbot.fuzzing.providers.findAllAvailableMethods
1817
import org.utbot.fuzzing.providers.nullRoutine
1918
import org.utbot.fuzzing.spring.valid.EntityLifecycleState
2019
import org.utbot.fuzzing.spring.valid.EntityLifecycleStateProperty
21-
import org.utbot.fuzzing.providers.removeSetters
2220

2321
class SpringBeanValueProvider(
2422
private val idGenerator: IdGenerator<Int>,

utbot-modificators-analyzer/src/main/kotlin/org/utbot/modifications/ExecutablesAnalyzer.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,17 @@ class ExecutablesAnalyzer {
5656
/**
5757
* Finds fields modified in Jimple code of this method.
5858
*/
59-
fun findModificationsInJimple(executableId: ExecutableId): Set<FieldId> {
59+
fun findModificationsInJimple(
60+
executableId: ExecutableId,
61+
predicate: (Any) -> Any?
62+
): Set<FieldId> {
6063
val sootMethod = executablesCache[executableId] ?: error("No method ${executableId.name} in soot cache")
6164

6265
val jimpleBody = retrieveJimpleBody(sootMethod) ?: return emptySet()
6366
return jimpleBody.units
6467
.filterIsInstance<JAssignStmt>()
65-
.mapNotNull { it.leftOp as? JInstanceFieldRef }
66-
.map { it.field.fieldId }
67-
.toSet()
68+
.mapNotNull { predicate(it) as? JInstanceFieldRef }
69+
.mapTo(mutableSetOf()) { it.field.fieldId }
6870
}
6971

7072
/**

utbot-modificators-analyzer/src/main/kotlin/org/utbot/modifications/StatementsStorage.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import org.utbot.framework.plugin.api.StatementId
1919
* - cleanup all statements of deleted or modified classes
2020
* - build invocation graph (with nested calls) and find field modificators on request
2121
*/
22-
class StatementsStorage {
22+
class StatementsStorage(
23+
private val modificationsPredicate: (Any) -> Any?
24+
) {
2325
/** Statements with their detailed information */
2426
val items: MutableMap<StatementId, StatementInfo> = mutableMapOf()
2527

@@ -43,7 +45,7 @@ class StatementsStorage {
4345
items[executableId] = StatementInfo(
4446
isRoot = true,
4547
executablesAnalyzer.findDeclaringClass(executableId),
46-
executablesAnalyzer.findModificationsInJimple(executableId),
48+
executablesAnalyzer.findModificationsInJimple(executableId, modificationsPredicate),
4749
executablesAnalyzer.findInvocationsInJimple(executableId),
4850
storageDataVersion,
4951
)
@@ -155,7 +157,7 @@ class StatementsStorage {
155157

156158
val executableId = statementId as? ExecutableId ?: return
157159

158-
val modifications = executablesAnalyzer.findModificationsInJimple(executableId)
160+
val modifications = executablesAnalyzer.findModificationsInJimple(executableId, modificationsPredicate)
159161
val successors = executablesAnalyzer.findInvocationsInJimple(executableId)
160162

161163
for (successor in successors) {

utbot-modificators-analyzer/src/main/kotlin/org/utbot/modifications/UtBotFieldsModificatorsSearcher.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import org.utbot.framework.plugin.api.ClassId
44
import org.utbot.framework.plugin.api.FieldId
55
import org.utbot.framework.plugin.api.StatementId
66

7-
class UtBotFieldsModificatorsSearcher {
7+
class UtBotFieldsModificatorsSearcher(
8+
modificationsPredicate: (Any) -> Any?
9+
) {
810

9-
private var statementsStorage = StatementsStorage()
11+
private var statementsStorage = StatementsStorage(modificationsPredicate)
1012

1113
fun update(classIds: Set<ClassId>) = statementsStorage.update(classIds)
1214

@@ -22,7 +24,7 @@ class UtBotFieldsModificatorsSearcher {
2224
return findModificatorsInCacheInverted(analysisMode)
2325
}
2426

25-
fun getModificatorToFields(analysisMode: AnalysisMode): Map<StatementId, Set<FieldId>> {
27+
fun getModificatorToFields(analysisMode: AnalysisMode): Map<String, Set<FieldId>> {
2628
statementsStorage.updateCaches()
2729
return findModificatorsInCache(analysisMode)
2830
}
@@ -47,7 +49,7 @@ class UtBotFieldsModificatorsSearcher {
4749
return modifications
4850
}
4951

50-
private fun findModificatorsInCache(analysisMode: AnalysisMode): Map<StatementId, Set<FieldId>> =
52+
private fun findModificatorsInCache(analysisMode: AnalysisMode): Map<String, Set<FieldId>> =
5153
statementsStorage
5254
.items
5355
.mapNotNull {
@@ -56,7 +58,7 @@ class UtBotFieldsModificatorsSearcher {
5658
statementsStorage.find(it.key, analysisMode)
5759
.let { modifiedFields ->
5860
if (modifiedFields.isEmpty()) return@mapNotNull null
59-
else it.key to modifiedFields
61+
else it.key.name to modifiedFields
6062
}
6163
}.toMap()
6264
}

0 commit comments

Comments
 (0)