Skip to content

Fix direct assigning of fields with private type #731 #735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.utbot.engine.ResolvedModels
import org.utbot.engine.isPrivate
import org.utbot.engine.isPublic
import org.utbot.framework.UtSettings
import org.utbot.framework.codegen.model.util.isAccessibleFrom
import org.utbot.framework.modifications.AnalysisMode.SettersAndDirectAccessors
import org.utbot.framework.modifications.ConstructorAnalyzer
import org.utbot.framework.modifications.ConstructorAssembleInfo
Expand Down Expand Up @@ -255,6 +256,11 @@ class AssembleModelGenerator(private val methodUnderTest: UtMethod<*>) {
if (fieldId.isFinal) {
throw AssembleException("Final field $fieldId can't be set in an object of the class $classId")
}
if (!fieldId.type.isAccessibleFrom(methodPackageName)) {
throw AssembleException(
"Field $fieldId can't be set in an object of the class $classId because its type is inaccessible"
)
}
//fill field value if it hasn't been filled by constructor, and it is not default
if (fieldId in constructorInfo.affectedFields ||
(fieldId !in constructorInfo.setFields && !fieldModel.hasDefaultValue())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ internal class ClassWithStaticAndInnerClassesTest : UtValueTestCaseChecker(testC
)
}

@Test
fun testGetValueFromPublicFieldWithPrivateType() {
check(
ClassWithStaticAndInnerClasses::getValueFromPublicFieldWithPrivateType,
eq(2),
coverage = DoNotCalculate
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JFYI: it would be better to use coverage = DoNotCalculate only to avoid false positive test failure when coverage computation returns wrong results (it seems that it is the case here) although the set of executions is correct. It would also be useful to add matchers to be sure that executions of interest have been really found.

I think this test can be left as is, as it only provides data to the integration test (the only way to check the codegen) and follows the style of existing examples. Nevertheless, adding matchers could help to detect engine bugs as well.

)
}

@Test
fun testPublicStaticClassWithPrivateField_DeepNestedStatic_g() {
checkAllCombinations(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
public class ClassWithStaticAndInnerClasses {
public int z = 0;

// public field that exposes private type PrivateInnerClassWithPublicField
public PrivateInnerClassWithPublicField publicFieldWithPrivateType = new PrivateInnerClassWithPublicField(0);

private static class PrivateStaticClassWithPublicField {
public int x;

Expand Down Expand Up @@ -239,4 +242,8 @@ PackagePrivateFinalInnerClassWithPackagePrivateField usePackagePrivateFinalInner

return innerClass.createFromIncrement(x);
}

int getValueFromPublicFieldWithPrivateType() {
return publicFieldWithPrivateType.x;
}
}