Title: Unnecessary reflection when one constructor is used more than once · Issue #1353 · UnitTestBot/UTBotJava · GitHub
Open Graph Title: Unnecessary reflection when one constructor is used more than once · Issue #1353 · UnitTestBot/UTBotJava
X Title: Unnecessary reflection when one constructor is used more than once · Issue #1353 · UnitTestBot/UTBotJava
Description: 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 UnnecessaryReflec...
Open Graph Description: 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 ...
X Description: 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 ...
Opengraph URL: https://github.com/UnitTestBot/UTBotJava/issues/1353
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Unnecessary reflection when one constructor is used more than once","articleBody":"**Description**\r\n\r\nIn 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.\r\n\r\n**To Reproduce**\r\n\r\nLaunch action on class `UnnecessaryReflection` from below:\r\n\r\n```Java\r\nclass MyPoint {\r\n public final int x, y;\r\n public MyPoint(int x, int y) {\r\n this.x = x;\r\n this.y = y;\r\n }\r\n}\r\n\r\nclass ClassWithPoint {\r\n public MyPoint p;\r\n public ClassWithPoint(MyPoint p) {\r\n this.p = p;\r\n }\r\n}\r\n\r\npublic class UnnecessaryReflection {\r\n public ClassWithPoint f(ClassWithPoint c) {\r\n if (c.p.x == 1)\r\n return new ClassWithPoint(new MyPoint(2, 3));\r\n return c;\r\n }\r\n}\r\n```\r\n\r\n**Expected behavior**\r\n\r\nTests are generated correctly and without reflection.\r\n\r\n**Actual behavior**\r\n\r\nIn some tests, some UtBot doesn't instantiate `Point` with constructor and uses reflection instead.\r\n\r\n**Visual proofs (screenshots, logs, images)**\r\n\r\n```Java\r\n@Test\r\npublic void testF2() throws Exception {\r\n UnnecessaryReflection unnecessaryReflection = new UnnecessaryReflection();\r\n MyPoint myPoint = new MyPoint(1, -1464141532);\r\n ClassWithPoint c = new ClassWithPoint(myPoint);\r\n\r\n ClassWithPoint actual = unnecessaryReflection.f(c);\r\n\r\n ClassWithPoint expected = new ClassWithPoint(null);\r\n MyPoint myPoint1 = ((MyPoint) createInstance(\"testpackage.MyPoint\"));\r\n setField(myPoint1, \"testpackage.MyPoint\", \"x\", 2);\r\n setField(myPoint1, \"testpackage.MyPoint\", \"y\", 3);\r\n expected.p = myPoint1;\r\n MyPoint expectedP = expected.p;\r\n MyPoint actualP = actual.p;\r\n int expectedPX = expectedP.x;\r\n int actualPX = actualP.x;\r\n assertEquals(expectedPX, actualPX);\r\n\r\n int expectedPY = expectedP.y;\r\n int actualPY = actualP.y;\r\n assertEquals(expectedPY, actualPY);\r\n\r\n}\r\n```\r\n\r\n**Environment**\r\n\r\nFuzzing 25% Symbolic execution 75%\r\n\r\n**Additional context**\r\n\r\nSeems 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](https://github.com/UnitTestBot/UTBotJava/pull/1293)\r\n","author":{"url":"https://github.com/volivan239","@type":"Person","name":"volivan239"},"datePublished":"2022-11-11T09:43:53.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/1353/UTBotJava/issues/1353"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:27e40057-8488-6e96-b77c-a4c63d961cf9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E8BE:F0B83:4B78E:5F926:6990A393 |
| html-safe-nonce | 15f1697e9c49bcc175bbd67439d13a02113dc54f8e034d1000dd94f4f94a6c88 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOEJFOkYwQjgzOjRCNzhFOjVGOTI2OjY5OTBBMzkzIiwidmlzaXRvcl9pZCI6IjM3MTI3Njg1NTA3NjQ1ODU4NzUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e367c21241a35581939da67fb13de054c3a7aeff324529661187d5ae9c907ac5 |
| hovercard-subject-tag | issue:1445214702 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/UnitTestBot/UTBotJava/1353/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c79e7c1639c1d1f471166c6e1b4c5aa6c72236ac43713ecda2de84c7fd933339/UnitTestBot/UTBotJava/issues/1353 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c79e7c1639c1d1f471166c6e1b4c5aa6c72236ac43713ecda2de84c7fd933339/UnitTestBot/UTBotJava/issues/1353 |
| og:image:alt | 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 ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | volivan239 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 42c603b9d642c4a9065a51770f75e5e27132fef0e858607f5c9cb7e422831a7b |
| turbo-cache-control | no-preview |
| go-import | github.com/UnitTestBot/UTBotJava git https://github.com/UnitTestBot/UTBotJava.git |
| octolytics-dimension-user_id | 87413538 |
| octolytics-dimension-user_login | UnitTestBot |
| octolytics-dimension-repository_id | 480810501 |
| octolytics-dimension-repository_nwo | UnitTestBot/UTBotJava |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 480810501 |
| octolytics-dimension-repository_network_root_nwo | UnitTestBot/UTBotJava |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 3b33c5aedc9808f45bc5fcf0b1e4404cf749dac7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width