Skip to content
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

Codegen unable to properly instantiate public inner classes #761

Closed
volivan239 opened this issue Aug 22, 2022 · 1 comment · Fixed by #1009
Closed

Codegen unable to properly instantiate public inner classes #761

volivan239 opened this issue Aug 22, 2022 · 1 comment · Fixed by #1009
Assignees
Labels
comp-codegen Issue is related to code generator ctg-bug Issue is a bug

Comments

@volivan239
Copy link
Collaborator

Description

Java allows to correctly instantiate public inner classes anywhere in the code. However, in generated code we do not do this.

To Reproduce

Launch plugin on the following code snippet:

class A {
    public class B {
        public int x;
        public B(int x) { this.x = x; }
    }
    int f(B b) {
        return b.x * b.x;
    }
}

Expected behavior

In generated tests, A$B is instantiated like A a = new A(0); B b = a.new B();

Actual behavior

  1. If fuzzer is disabled, classes are instantiated through reflection (which is technically correct, but not optimal)
  2. If only fuzzer is used, classes are instantiated incorrectly.

Visual proofs (screenshots, logs, images)

Test generated with engine only:

  public void testF_ReturnBXMultiplyBX() throws Exception {
      A a = new A();
      B b = ((B) createInstance("nested.A$B"));
      b.x = 1;

      int actual = a.f(b);

      assertEquals(1, actual);
  }

Test generated with fuzzer only:

  public void testFReturnsZero() {
      A a = new A();
      A a1 = new A();
      B b = new B(a1, 0);

      int actual = a.f(b);

      assertEquals(0, actual);
  }

Additional context

It is difficult to properly handle this case, because constructors for inner classes have misleading signatures like public nested.A$B(nested.A,int), so we have either to ignore such constructors and use composite model in these cases (as it is done in assembleCompositeModel), or to handle them as a special case.

This issue seems to be a duplicate of SAT-1461

@volivan239 volivan239 added ctg-bug Issue is a bug comp-codegen Issue is related to code generator labels Aug 22, 2022
@EgorkaKulikov EgorkaKulikov self-assigned this Aug 22, 2022
@korifey korifey moved this to Todo in UTBot Java Aug 22, 2022
@alisevych alisevych added this to the Release preparation milestone Sep 7, 2022
@EgorkaKulikov
Copy link
Collaborator

Discussed with @korifey, these functionality seems to be useless, but requires difficult changes in codegen.
@Markoutte, please avoid creating assemble models for such classes in Fuzzer as it is done in Engine. @volivan239 can provide more details.

Markoutte added a commit that referenced this issue Sep 27, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Repository owner moved this from Todo to Done in UTBot Java Sep 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-codegen Issue is related to code generator ctg-bug Issue is a bug
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants