Skip to content

Commit 0ee76e2

Browse files
authored
Fix direct assigning of fields with private type #731 (#735)
1 parent 13fae67 commit 0ee76e2

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.utbot.engine.ResolvedModels
66
import org.utbot.engine.isPrivate
77
import org.utbot.engine.isPublic
88
import org.utbot.framework.UtSettings
9+
import org.utbot.framework.codegen.model.util.isAccessibleFrom
910
import org.utbot.framework.modifications.AnalysisMode.SettersAndDirectAccessors
1011
import org.utbot.framework.modifications.ConstructorAnalyzer
1112
import org.utbot.framework.modifications.ConstructorAssembleInfo
@@ -255,6 +256,11 @@ class AssembleModelGenerator(private val methodUnderTest: UtMethod<*>) {
255256
if (fieldId.isFinal) {
256257
throw AssembleException("Final field $fieldId can't be set in an object of the class $classId")
257258
}
259+
if (!fieldId.type.isAccessibleFrom(methodPackageName)) {
260+
throw AssembleException(
261+
"Field $fieldId can't be set in an object of the class $classId because its type is inaccessible"
262+
)
263+
}
258264
//fill field value if it hasn't been filled by constructor, and it is not default
259265
if (fieldId in constructorInfo.affectedFields ||
260266
(fieldId !in constructorInfo.setFields && !fieldModel.hasDefaultValue())

utbot-framework/src/test/kotlin/org/utbot/examples/codegen/ClassWithStaticAndInnerClassesTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ internal class ClassWithStaticAndInnerClassesTest : UtValueTestCaseChecker(testC
9797
)
9898
}
9999

100+
@Test
101+
fun testGetValueFromPublicFieldWithPrivateType() {
102+
check(
103+
ClassWithStaticAndInnerClasses::getValueFromPublicFieldWithPrivateType,
104+
eq(2),
105+
coverage = DoNotCalculate
106+
)
107+
}
108+
100109
@Test
101110
fun testPublicStaticClassWithPrivateField_DeepNestedStatic_g() {
102111
checkAllCombinations(

utbot-sample/src/main/java/org/utbot/examples/codegen/ClassWithStaticAndInnerClasses.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
public class ClassWithStaticAndInnerClasses {
44
public int z = 0;
55

6+
// public field that exposes private type PrivateInnerClassWithPublicField
7+
public PrivateInnerClassWithPublicField publicFieldWithPrivateType = new PrivateInnerClassWithPublicField(0);
8+
69
private static class PrivateStaticClassWithPublicField {
710
public int x;
811

@@ -239,4 +242,8 @@ PackagePrivateFinalInnerClassWithPackagePrivateField usePackagePrivateFinalInner
239242

240243
return innerClass.createFromIncrement(x);
241244
}
245+
246+
int getValueFromPublicFieldWithPrivateType() {
247+
return publicFieldWithPrivateType.x;
248+
}
242249
}

0 commit comments

Comments
 (0)