Skip to content

Unnecessary reflection when one constructor is used more than once #1353

Description

@volivan239

Description

In some cases, generated tests will have to call one constructor more then once. In this case, second and following calls may use reflection even though it is unnecessary.

To Reproduce

Launch action on class UnnecessaryReflection from below:

classMyPoint {
publicfinalintx, y;
publicMyPoint(intx, inty) {
this.x = x;
this.y = y;
}
}
classClassWithPoint {
publicMyPointp;
publicClassWithPoint(MyPointp) {
this.p = p;
}
}
publicclassUnnecessaryReflection {
publicClassWithPointf(ClassWithPointc) {
if (c.p.x == 1)
returnnewClassWithPoint(newMyPoint(2, 3));
returnc;
}
}

Expected behavior

Tests are generated correctly and without reflection.

Actual behavior

In some tests, some UtBot doesn't instantiate Point with constructor and uses reflection instead.

Visual proofs (screenshots, logs, images)

@TestpublicvoidtestF2() throwsException {
UnnecessaryReflectionunnecessaryReflection = newUnnecessaryReflection();
MyPointmyPoint = newMyPoint(1, -1464141532);
ClassWithPointc = newClassWithPoint(myPoint);
ClassWithPointactual = unnecessaryReflection.f(c);
ClassWithPointexpected = newClassWithPoint(null);
MyPointmyPoint1 = ((MyPoint) createInstance("testpackage.MyPoint"));
setField(myPoint1, "testpackage.MyPoint", "x", 2);
setField(myPoint1, "testpackage.MyPoint", "y", 3);
expected.p = myPoint1;
MyPointexpectedP = expected.p;
MyPointactualP = actual.p;
intexpectedPX = expectedP.x;
intactualPX = actualP.x;
assertEquals(expectedPX, actualPX);
intexpectedPY = expectedP.y;
intactualPY = actualP.y;
assertEquals(expectedPY, actualPY);
}

Environment

Fuzzing 25% Symbolic execution 75%

Additional context

Seems like problem is in ConstructorAnalyzer.analyze() method which does nothing when called more then once. However, we can't just remove visited checks from there, because this would lead to infinite recursion in examples like org.utbot.examples.codegen.deepequals.ClassWithCrossReferenceRelationshipTest#testClassWithCrossReferenceRelationship -- see failed attempt to fix issue this way

Metadata

Metadata

Assignees

Labels

comp-codegenIssue is related to code generatorctg-bugIssue is a bugpriority-top-focusTop priority chosen by dev team

Type

No type

Projects

Status
Done

Relationships

None yet

Development

No branches or pull requests

Issue actions