Skip to content

Commit ea6decd

Browse files
Egor KulikovEgorkaKulikov
Egor Kulikov
authored andcommitted
Do not set final fields with direct accessors in UtAssembleModel
1 parent 70e4942 commit ea6decd

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ class AssembleModelGenerator(private val methodUnderTest: UtMethod<*>) {
246246
if (fieldId.isStatic) {
247247
throw AssembleException("Static field $fieldId can't be set in an object of the class $classId")
248248
}
249+
if (fieldId.isFinal) {
250+
throw AssembleException("Final field $fieldId can't be set in an object of the class $classId")
251+
}
249252
//fill field value if it hasn't been filled by constructor, and it is not default
250253
if (fieldId in constructorInfo.affectedFields ||
251254
(fieldId !in constructorInfo.setFields && !fieldModel.hasDefaultValue())
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.utbot.examples.assemble;
2+
3+
/**
4+
* A class with a FINAL field allowing direct access.
5+
*/
6+
public class DirectAccessFinal {
7+
public final int[] array = {1, 2};
8+
}

utbot-framework/src/test/kotlin/org/utbot/framework/assemble/AssembleModelGeneratorTests.kt

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
package org.utbot.framework.assemble
22

3-
import org.utbot.examples.assemble.AssembleTestUtils
4-
import org.utbot.examples.assemble.ComplexField
5-
import org.utbot.examples.assemble.DirectAccess
6-
import org.utbot.examples.assemble.DirectAccessAndSetter
7-
import org.utbot.examples.assemble.InheritedField
8-
import org.utbot.examples.assemble.ListItem
9-
import org.utbot.examples.assemble.NoModifier
10-
import org.utbot.examples.assemble.PackagePrivateFields
11-
import org.utbot.examples.assemble.PrimitiveFields
123
import org.utbot.examples.assemble.arrays.ArrayOfComplexArrays
134
import org.utbot.examples.assemble.arrays.ArrayOfPrimitiveArrays
145
import org.utbot.examples.assemble.arrays.AssignedArray
@@ -54,6 +45,7 @@ import org.junit.jupiter.api.Assertions.assertTrue
5445
import org.junit.jupiter.api.BeforeEach
5546
import org.junit.jupiter.api.Disabled
5647
import org.junit.jupiter.api.Test
48+
import org.utbot.examples.assemble.*
5749

5850
/**
5951
* Test classes must be located in the same folder as [AssembleTestUtils] class.
@@ -337,6 +329,26 @@ class AssembleModelGeneratorTests {
337329
createModelAndAssert(compositeModel, expectedRepresentation)
338330
}
339331

332+
@Test
333+
fun testOnObjectWithFinalFields() {
334+
val testClassId = DirectAccessFinal::class.id
335+
336+
val arrayObjectFields = fields(
337+
testClassId,
338+
"array" to UtArrayModel(
339+
modelIdCounter.incrementAndGet(),
340+
intArrayClassId,
341+
length = 2,
342+
UtPrimitiveModel(0),
343+
mutableMapOf(0 to UtPrimitiveModel(1), 1 to UtPrimitiveModel(2)),
344+
),
345+
)
346+
val compositeModel =
347+
UtCompositeModel(modelIdCounter.incrementAndGet(), testClassId, isMock = false, arrayObjectFields)
348+
349+
createModelAndAssert(compositeModel, null)
350+
}
351+
340352
//region inheritance_tests
341353

342354
@Test

0 commit comments

Comments
 (0)