Skip to content

Commit b70ee87

Browse files
committed
Used real variable type after field access with reflection if possible
1 parent d9b7076 commit b70ee87

File tree

4 files changed

+63
-12
lines changed

4 files changed

+63
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.utbot.examples.codegen.modifiers
2+
3+
import org.junit.jupiter.api.Test
4+
import org.utbot.common.withAccessibility
5+
import org.utbot.framework.plugin.api.FieldId
6+
import org.utbot.framework.plugin.api.util.id
7+
import org.utbot.framework.plugin.api.util.jField
8+
import org.utbot.testcheckers.eq
9+
import org.utbot.tests.infrastructure.UtValueTestCaseChecker
10+
11+
class ClassWithPrivateMutableFieldOfPrivateTypeTest : UtValueTestCaseChecker(
12+
testClass = ClassWithPrivateMutableFieldOfPrivateType::class
13+
) {
14+
@Test
15+
fun testChangePrivateMutableFieldWithPrivateType() {
16+
checkAllMutationsWithThis(
17+
ClassWithPrivateMutableFieldOfPrivateType::changePrivateMutableFieldWithPrivateType,
18+
eq(1),
19+
{ thisBefore, _, thisAfter, _, r ->
20+
val privateMutableField = FieldId(
21+
ClassWithPrivateMutableFieldOfPrivateType::class.id,
22+
"privateMutableField"
23+
).jField
24+
25+
val (privateFieldBeforeValue, privateFieldAfterValue) = privateMutableField.withAccessibility {
26+
privateMutableField.get(thisBefore) to privateMutableField.get(thisAfter)
27+
}
28+
29+
privateFieldBeforeValue == null && privateFieldAfterValue != null && r == 0
30+
}
31+
)
32+
}
33+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ internal class CgFieldStateManagerImpl(val context: CgContext)
227227
if (index > path.lastIndex) return@generateSequence null
228228
val passedPath = FieldPath(path.subList(0, index + 1))
229229
val name = if (index == path.lastIndex) customName else getFieldVariableName(prev, passedPath)
230-
val expression = when (val newElement = path[index++]) {
230+
231+
val newElement = path[index++]
232+
val expression = when (newElement) {
231233
is FieldAccess -> {
232234
val fieldId = newElement.field
233235
utilsClassId[getFieldValue](prev, fieldId.declaringClass.name, fieldId.name)
@@ -236,7 +238,7 @@ internal class CgFieldStateManagerImpl(val context: CgContext)
236238
Array::class.id[getArrayElement](prev, newElement.index)
237239
}
238240
}
239-
newVar(objectClassId, name) { expression }
241+
newVar(newElement.type, name) { expression }
240242
}.last()
241243
}
242244

utbot-framework/src/main/kotlin/org/utbot/tests/infrastructure/UtValueTestCaseChecker.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,10 +2150,10 @@ abstract class UtValueTestCaseChecker(
21502150
)
21512151

21522152
// checks mutations in this, parameters and statics
2153-
protected inline fun <reified T> checkAllMutationsWithThis(
2154-
method: KFunction1<T, *>,
2153+
protected inline fun <reified T, reified R> checkAllMutationsWithThis(
2154+
method: KFunction1<T, R>,
21552155
branches: ExecutionsNumberMatcher,
2156-
vararg matchers: (T, StaticsType, T, StaticsType) -> Boolean,
2156+
vararg matchers: (T, StaticsType, T, StaticsType, R) -> Boolean,
21572157
coverage: CoverageMatcher = Full,
21582158
mockStrategy: MockStrategyApi = NO_MOCKS,
21592159
additionalDependencies: Array<Class<*>> = emptyArray(),
@@ -2169,10 +2169,10 @@ abstract class UtValueTestCaseChecker(
21692169
summaryDisplayNameChecks = summaryDisplayNameChecks
21702170
)
21712171

2172-
protected inline fun <reified T, reified T1> checkAllMutationsWithThis(
2172+
protected inline fun <reified T, reified T1, reified R> checkAllMutationsWithThis(
21732173
method: KFunction2<T, T1, *>,
21742174
branches: ExecutionsNumberMatcher,
2175-
vararg matchers: (T, T1, StaticsType, T, T1, StaticsType) -> Boolean,
2175+
vararg matchers: (T, T1, StaticsType, T, T1, StaticsType, R) -> Boolean,
21762176
coverage: CoverageMatcher = Full,
21772177
mockStrategy: MockStrategyApi = NO_MOCKS,
21782178
additionalDependencies: Array<Class<*>> = emptyArray(),
@@ -2188,10 +2188,10 @@ abstract class UtValueTestCaseChecker(
21882188
summaryDisplayNameChecks = summaryDisplayNameChecks
21892189
)
21902190

2191-
protected inline fun <reified T, reified T1, reified T2> checkAllMutationsWithThis(
2191+
protected inline fun <reified T, reified T1, reified T2, reified R> checkAllMutationsWithThis(
21922192
method: KFunction3<T, T1, T2, *>,
21932193
branches: ExecutionsNumberMatcher,
2194-
vararg matchers: (T, T1, T2, StaticsType, T, T1, T2, StaticsType) -> Boolean,
2194+
vararg matchers: (T, T1, T2, StaticsType, T, T1, T2, StaticsType, R) -> Boolean,
21952195
coverage: CoverageMatcher = Full,
21962196
mockStrategy: MockStrategyApi = NO_MOCKS,
21972197
additionalDependencies: Array<Class<*>> = emptyArray(),
@@ -2207,7 +2207,7 @@ abstract class UtValueTestCaseChecker(
22072207
summaryDisplayNameChecks = summaryDisplayNameChecks
22082208
)
22092209

2210-
protected inline fun <reified T, reified T1, reified T2, reified T3> checkAllMutationsWithThis(
2210+
protected inline fun <reified T, reified T1, reified T2, reified T3, reified R> checkAllMutationsWithThis(
22112211
method: KFunction4<T, T1, T2, T3, *>,
22122212
branches: ExecutionsNumberMatcher,
22132213
vararg matchers: (T, T1, T2, T3, StaticsType, T, T1, T2, T3, StaticsType) -> Boolean,
@@ -2226,7 +2226,7 @@ abstract class UtValueTestCaseChecker(
22262226
summaryDisplayNameChecks = summaryDisplayNameChecks
22272227
)
22282228

2229-
protected inline fun <reified T, reified T1, reified T2, reified T3, reified T4> checkAllMutationsWithThis(
2229+
protected inline fun <reified T, reified T1, reified T2, reified T3, reified T4, reified R> checkAllMutationsWithThis(
22302230
method: KFunction5<T, T1, T2, T3, T4, *>,
22312231
branches: ExecutionsNumberMatcher,
22322232
vararg matchers: (T, T1, T2, T3, T4, StaticsType, T, T1, T2, T3, T4, StaticsType) -> Boolean,
@@ -2795,7 +2795,7 @@ fun withMutationsAndThis(ex: UtValueExecution<*>) =
27952795
addAll(ex.paramsAfter)
27962796
add(ex.staticsAfter)
27972797

2798-
add(ex.returnValue)
2798+
add(ex.evaluatedResult)
27992799
}
28002800

28012801
private val UtValueExecution<*>.callerBefore get() = stateBefore.caller!!.value
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.utbot.examples.codegen.modifiers;
2+
3+
public class ClassWithPrivateMutableFieldOfPrivateType {
4+
@SuppressWarnings({"FieldCanBeLocal", "unused"})
5+
private PrivateClass privateMutableField = null;
6+
7+
public int changePrivateMutableFieldWithPrivateType() {
8+
privateMutableField = new PrivateClass();
9+
10+
return privateMutableField.x;
11+
}
12+
13+
private static class PrivateClass {
14+
int x = 0;
15+
}
16+
}

0 commit comments

Comments
 (0)