Skip to content

Commit 9838c1d

Browse files
committed
Used real variable type after field access with reflection if possible
1 parent 56bf6ab commit 9838c1d

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
@@ -229,7 +229,9 @@ internal class CgFieldStateManagerImpl(val context: CgContext)
229229
if (index > path.lastIndex) return@generateSequence null
230230
val passedPath = FieldPath(path.subList(0, index + 1))
231231
val name = if (index == path.lastIndex) customName else getFieldVariableName(prev, passedPath)
232-
val expression = when (val newElement = path[index++]) {
232+
233+
val newElement = path[index++]
234+
val expression = when (newElement) {
233235
is FieldAccess -> {
234236
val fieldId = newElement.field
235237
utilsClassId[getFieldValue](prev, fieldId.declaringClass.name, fieldId.name)
@@ -238,7 +240,7 @@ internal class CgFieldStateManagerImpl(val context: CgContext)
238240
Array::class.id[getArrayElement](prev, newElement.index)
239241
}
240242
}
241-
newVar(objectClassId, name) { expression }
243+
newVar(newElement.type, name) { expression }
242244
}.last()
243245
}
244246

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
@@ -2151,10 +2151,10 @@ abstract class UtValueTestCaseChecker(
21512151
)
21522152

21532153
// checks mutations in this, parameters and statics
2154-
protected inline fun <reified T> checkAllMutationsWithThis(
2155-
method: KFunction1<T, *>,
2154+
protected inline fun <reified T, reified R> checkAllMutationsWithThis(
2155+
method: KFunction1<T, R>,
21562156
branches: ExecutionsNumberMatcher,
2157-
vararg matchers: (T, StaticsType, T, StaticsType) -> Boolean,
2157+
vararg matchers: (T, StaticsType, T, StaticsType, R) -> Boolean,
21582158
coverage: CoverageMatcher = Full,
21592159
mockStrategy: MockStrategyApi = NO_MOCKS,
21602160
additionalDependencies: Array<Class<*>> = emptyArray(),
@@ -2170,10 +2170,10 @@ abstract class UtValueTestCaseChecker(
21702170
summaryDisplayNameChecks = summaryDisplayNameChecks
21712171
)
21722172

2173-
protected inline fun <reified T, reified T1> checkAllMutationsWithThis(
2173+
protected inline fun <reified T, reified T1, reified R> checkAllMutationsWithThis(
21742174
method: KFunction2<T, T1, *>,
21752175
branches: ExecutionsNumberMatcher,
2176-
vararg matchers: (T, T1, StaticsType, T, T1, StaticsType) -> Boolean,
2176+
vararg matchers: (T, T1, StaticsType, T, T1, StaticsType, R) -> Boolean,
21772177
coverage: CoverageMatcher = Full,
21782178
mockStrategy: MockStrategyApi = NO_MOCKS,
21792179
additionalDependencies: Array<Class<*>> = emptyArray(),
@@ -2189,10 +2189,10 @@ abstract class UtValueTestCaseChecker(
21892189
summaryDisplayNameChecks = summaryDisplayNameChecks
21902190
)
21912191

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

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

2230-
protected inline fun <reified T, reified T1, reified T2, reified T3, reified T4> checkAllMutationsWithThis(
2230+
protected inline fun <reified T, reified T1, reified T2, reified T3, reified T4, reified R> checkAllMutationsWithThis(
22312231
method: KFunction5<T, T1, T2, T3, T4, *>,
22322232
branches: ExecutionsNumberMatcher,
22332233
vararg matchers: (T, T1, T2, T3, T4, StaticsType, T, T1, T2, T3, T4, StaticsType) -> Boolean,
@@ -2798,7 +2798,7 @@ fun withMutationsAndThis(ex: UtValueExecution<*>) =
27982798
addAll(ex.paramsAfter)
27992799
add(ex.staticsAfter)
28002800

2801-
add(ex.returnValue)
2801+
add(ex.evaluatedResult)
28022802
}
28032803

28042804
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)