Skip to content

Commit b9217e7

Browse files
committed
Used real variable type after field access with reflection if possible
1 parent 8b9bcc2 commit b9217e7

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
@@ -2054,10 +2054,10 @@ abstract class UtValueTestCaseChecker(
20542054
)
20552055

20562056
// checks mutations in this, parameters and statics
2057-
protected inline fun <reified T> checkAllMutationsWithThis(
2058-
method: KFunction1<T, *>,
2057+
protected inline fun <reified T, reified R> checkAllMutationsWithThis(
2058+
method: KFunction1<T, R>,
20592059
branches: ExecutionsNumberMatcher,
2060-
vararg matchers: (T, StaticsType, T, StaticsType) -> Boolean,
2060+
vararg matchers: (T, StaticsType, T, StaticsType, R) -> Boolean,
20612061
coverage: CoverageMatcher = Full,
20622062
mockStrategy: MockStrategyApi = NO_MOCKS,
20632063
additionalDependencies: Array<Class<*>> = emptyArray(),
@@ -2073,10 +2073,10 @@ abstract class UtValueTestCaseChecker(
20732073
summaryDisplayNameChecks = summaryDisplayNameChecks
20742074
)
20752075

2076-
protected inline fun <reified T, reified T1> checkAllMutationsWithThis(
2076+
protected inline fun <reified T, reified T1, reified R> checkAllMutationsWithThis(
20772077
method: KFunction2<T, T1, *>,
20782078
branches: ExecutionsNumberMatcher,
2079-
vararg matchers: (T, T1, StaticsType, T, T1, StaticsType) -> Boolean,
2079+
vararg matchers: (T, T1, StaticsType, T, T1, StaticsType, R) -> Boolean,
20802080
coverage: CoverageMatcher = Full,
20812081
mockStrategy: MockStrategyApi = NO_MOCKS,
20822082
additionalDependencies: Array<Class<*>> = emptyArray(),
@@ -2092,10 +2092,10 @@ abstract class UtValueTestCaseChecker(
20922092
summaryDisplayNameChecks = summaryDisplayNameChecks
20932093
)
20942094

2095-
protected inline fun <reified T, reified T1, reified T2> checkAllMutationsWithThis(
2095+
protected inline fun <reified T, reified T1, reified T2, reified R> checkAllMutationsWithThis(
20962096
method: KFunction3<T, T1, T2, *>,
20972097
branches: ExecutionsNumberMatcher,
2098-
vararg matchers: (T, T1, T2, StaticsType, T, T1, T2, StaticsType) -> Boolean,
2098+
vararg matchers: (T, T1, T2, StaticsType, T, T1, T2, StaticsType, R) -> Boolean,
20992099
coverage: CoverageMatcher = Full,
21002100
mockStrategy: MockStrategyApi = NO_MOCKS,
21012101
additionalDependencies: Array<Class<*>> = emptyArray(),
@@ -2111,7 +2111,7 @@ abstract class UtValueTestCaseChecker(
21112111
summaryDisplayNameChecks = summaryDisplayNameChecks
21122112
)
21132113

2114-
protected inline fun <reified T, reified T1, reified T2, reified T3> checkAllMutationsWithThis(
2114+
protected inline fun <reified T, reified T1, reified T2, reified T3, reified R> checkAllMutationsWithThis(
21152115
method: KFunction4<T, T1, T2, T3, *>,
21162116
branches: ExecutionsNumberMatcher,
21172117
vararg matchers: (T, T1, T2, T3, StaticsType, T, T1, T2, T3, StaticsType) -> Boolean,
@@ -2130,7 +2130,7 @@ abstract class UtValueTestCaseChecker(
21302130
summaryDisplayNameChecks = summaryDisplayNameChecks
21312131
)
21322132

2133-
protected inline fun <reified T, reified T1, reified T2, reified T3, reified T4> checkAllMutationsWithThis(
2133+
protected inline fun <reified T, reified T1, reified T2, reified T3, reified T4, reified R> checkAllMutationsWithThis(
21342134
method: KFunction5<T, T1, T2, T3, T4, *>,
21352135
branches: ExecutionsNumberMatcher,
21362136
vararg matchers: (T, T1, T2, T3, T4, StaticsType, T, T1, T2, T3, T4, StaticsType) -> Boolean,
@@ -2698,7 +2698,7 @@ fun withMutationsAndThis(ex: UtValueExecution<*>) =
26982698
addAll(ex.paramsAfter)
26992699
add(ex.staticsAfter)
27002700

2701-
add(ex.returnValue)
2701+
add(ex.evaluatedResult)
27022702
}
27032703

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