Skip to content

Commit 3612396

Browse files
committed
Rolled back changes in codegen
1 parent 78ed251 commit 3612396

File tree

6 files changed

+34
-32
lines changed

6 files changed

+34
-32
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import org.utbot.framework.codegen.model.tree.CgEqualTo
4343
import org.utbot.framework.codegen.model.tree.CgErrorTestMethod
4444
import org.utbot.framework.codegen.model.tree.CgExecutableCall
4545
import org.utbot.framework.codegen.model.tree.CgExpression
46+
import org.utbot.framework.codegen.model.tree.CgFieldAccess
4647
import org.utbot.framework.codegen.model.tree.CgGetJavaClass
4748
import org.utbot.framework.codegen.model.tree.CgIfStatement
4849
import org.utbot.framework.codegen.model.tree.CgLiteral
@@ -928,7 +929,7 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
928929
// Can directly access field only if it is declared in variable class (or in its ancestors)
929930
// and is accessible from current package
930931
if (variable.type.hasField(this) && isAccessibleFrom(testClassPackageName)) {
931-
if (field.isStatic) CgStaticFieldAccess(this) else variable[this]
932+
if (field.isStatic) CgStaticFieldAccess(this) else CgFieldAccess(variable, this)
932933
} else {
933934
testClassThisInstance[getFieldValue](variable, stringLiteral(name))
934935
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import org.utbot.framework.codegen.model.tree.CgAllocateInitializedArray
1515
import org.utbot.framework.codegen.model.tree.CgDeclaration
1616
import org.utbot.framework.codegen.model.tree.CgEnumConstantAccess
1717
import org.utbot.framework.codegen.model.tree.CgExpression
18+
import org.utbot.framework.codegen.model.tree.CgFieldAccess
1819
import org.utbot.framework.codegen.model.tree.CgGetJavaClass
1920
import org.utbot.framework.codegen.model.tree.CgLiteral
2021
import org.utbot.framework.codegen.model.tree.CgStaticFieldAccess
@@ -45,6 +46,7 @@ import org.utbot.framework.plugin.api.UtPrimitiveModel
4546
import org.utbot.framework.plugin.api.UtReferenceModel
4647
import org.utbot.framework.plugin.api.UtVoidModel
4748
import org.utbot.framework.plugin.api.util.defaultValueModel
49+
import org.utbot.framework.plugin.api.util.field
4850
import org.utbot.framework.plugin.api.util.findFieldByIdOrNull
4951
import org.utbot.framework.plugin.api.util.id
5052
import org.utbot.framework.plugin.api.util.intClassId
@@ -125,17 +127,21 @@ internal class CgVariableConstructor(val context: CgContext) :
125127
}
126128

127129
for ((fieldId, fieldModel) in model.fields) {
130+
val field = fieldId.field
128131
val variableForField = getOrCreateVariable(fieldModel)
129-
val field = obj.type.findFieldByIdOrNull(fieldId)
132+
val fieldFromVariableSpecifiedType = obj.type.findFieldByIdOrNull(fieldId)
130133

131134
// we cannot set field directly if variable declared type does not have such field
132135
// or we cannot directly create variable for field with the specified type (it is private, for example)
133136
// Example:
134137
// Object heapByteBuffer = createInstance("java.nio.HeapByteBuffer");
135138
// branchRegisterRequest.byteBuffer = heapByteBuffer;
136139
// byteBuffer is field of type ByteBuffer and upper line is incorrect
137-
if (field != null && field.type.id == variableForField.type && fieldId.canBeSetIn(testClassPackageName)) {
138-
val fieldAccess = if (field.isStatic) CgStaticFieldAccess(fieldId) else obj[fieldId]
140+
val canFieldBeDirectlySetByVariableAndFieldTypeRestrictions =
141+
fieldFromVariableSpecifiedType != null && fieldFromVariableSpecifiedType.type.id == variableForField.type
142+
if (canFieldBeDirectlySetByVariableAndFieldTypeRestrictions && fieldId.canBeSetIn(testClassPackageName)) {
143+
// TODO: check if it is correct to use declaringClass of a field here
144+
val fieldAccess = if (field.isStatic) CgStaticFieldAccess(fieldId) else CgFieldAccess(obj, fieldId)
139145
fieldAccess `=` variableForField
140146
} else {
141147
// composite models must not have info about static fields, hence only non-static fields are set here

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/util/DslUtil.kt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.utbot.framework.codegen.model.util
22

3-
import org.utbot.common.findDirectAccessedFieldOrNull
43
import org.utbot.framework.codegen.model.constructor.tree.CgCallableAccessManager
54
import org.utbot.framework.codegen.model.tree.CgArrayElementAccess
65
import org.utbot.framework.codegen.model.tree.CgDecrement
@@ -17,7 +16,6 @@ import org.utbot.framework.codegen.model.tree.CgLessThan
1716
import org.utbot.framework.codegen.model.tree.CgLiteral
1817
import org.utbot.framework.codegen.model.tree.CgStaticFieldAccess
1918
import org.utbot.framework.codegen.model.tree.CgThisInstance
20-
import org.utbot.framework.codegen.model.tree.CgTypeCast
2119
import org.utbot.framework.codegen.model.tree.CgVariable
2220
import org.utbot.framework.plugin.api.ClassId
2321
import org.utbot.framework.plugin.api.CodegenLanguage
@@ -27,11 +25,9 @@ import org.utbot.framework.plugin.api.util.booleanClassId
2725
import org.utbot.framework.plugin.api.util.byteClassId
2826
import org.utbot.framework.plugin.api.util.charClassId
2927
import org.utbot.framework.plugin.api.util.doubleClassId
30-
import org.utbot.framework.plugin.api.util.field
3128
import org.utbot.framework.plugin.api.util.floatClassId
3229
import org.utbot.framework.plugin.api.util.intClassId
3330
import org.utbot.framework.plugin.api.util.isArray
34-
import org.utbot.framework.plugin.api.util.jClass
3531
import org.utbot.framework.plugin.api.util.longClassId
3632
import org.utbot.framework.plugin.api.util.objectClassId
3733
import org.utbot.framework.plugin.api.util.shortClassId
@@ -76,11 +72,7 @@ fun stringLiteral(string: String) = CgLiteral(stringClassId, string)
7672

7773
// non-static fields
7874
operator fun CgExpression.get(fieldId: FieldId): CgFieldAccess =
79-
if (type.jClass.findDirectAccessedFieldOrNull(fieldId.name) != fieldId.field) {
80-
CgFieldAccess(CgTypeCast(fieldId.declaringClass, this), fieldId)
81-
} else {
82-
CgFieldAccess(this, fieldId)
83-
}
75+
CgFieldAccess(this, fieldId)
8476

8577
// static fields
8678
// TODO: unused receiver

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/visitor/CgKotlinRenderer.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ internal class CgKotlinRenderer(context: CgContext, printer: CgPrinter = CgPrint
121121
// Property access
122122

123123
override fun visit(element: CgFieldAccess) {
124-
if (element.caller is CgTypeCast) print("(")
125124
element.caller.accept(this)
126-
if (element.caller is CgTypeCast) print(")")
127125
renderAccess(element.caller)
128126
print(element.fieldId.name)
129127
}
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package org.utbot.examples.objects
22

33
import org.utbot.examples.UtValueTestCaseChecker
4-
import org.utbot.examples.DoNotCalculate
54
import org.utbot.examples.eq
65
import org.junit.jupiter.api.Test
76

87
internal class HiddenFieldAccessModifiersTest : UtValueTestCaseChecker(testClass = HiddenFieldAccessModifiersExample::class) {
98
@Test
109
fun testCheckSuperFieldEqualsOne() {
11-
check(
12-
HiddenFieldAccessModifiersExample::checkSuperFieldEqualsOne,
13-
eq(3),
14-
{ o, _ -> o == null },
15-
{ _, r -> r == true },
16-
{ _, r -> r == false},
17-
)
10+
// TODO: currently, codegen can't handle tests with field hiding
11+
withEnabledTestingCodeGeneration(testCodeGeneration = false) {
12+
check(
13+
HiddenFieldAccessModifiersExample::checkSuperFieldEqualsOne,
14+
eq(3),
15+
{ o, _ -> o == null },
16+
{ _, r -> r == true },
17+
{ _, r -> r == false },
18+
)
19+
}
1820
}
1921
}

utbot-framework/src/test/kotlin/org/utbot/examples/objects/HiddenFieldExampleTest.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ internal class HiddenFieldExampleTest : UtValueTestCaseChecker(testClass = Hidde
2121

2222
@Test
2323
fun testCheckSuccField() {
24-
check(
25-
HiddenFieldExample::checkSuccField,
26-
eq(5),
27-
{ o, _ -> o == null },
28-
{ o, r -> o.a == 1 && r == 1 },
29-
{ o, r -> o.a != 1 && o.b == 2.0 && r == 2 },
30-
{ o, r -> o.a != 1 && o.b != 2.0 && (o as HiddenFieldSuperClass).b == 3 && r == 3 },
31-
{ o, r -> o.a != 1 && o.b != 2.0 && (o as HiddenFieldSuperClass).b != 3 && r == 4 },
32-
)
24+
// TODO: currently, codegen can't handle tests with field hiding
25+
withEnabledTestingCodeGeneration(testCodeGeneration = false) {
26+
check(
27+
HiddenFieldExample::checkSuccField,
28+
eq(5),
29+
{ o, _ -> o == null },
30+
{ o, r -> o.a == 1 && r == 1 },
31+
{ o, r -> o.a != 1 && o.b == 2.0 && r == 2 },
32+
{ o, r -> o.a != 1 && o.b != 2.0 && (o as HiddenFieldSuperClass).b == 3 && r == 3 },
33+
{ o, r -> o.a != 1 && o.b != 2.0 && (o as HiddenFieldSuperClass).b != 3 && r == 4 },
34+
)
35+
}
3336
}
3437
}

0 commit comments

Comments
 (0)