Skip to content

Codegen unable to properly instantiate public inner classes #761

Closed
@volivan239

Description

@volivan239

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

Metadata

Metadata

Assignees

Labels

comp-codegenIssue is related to code generatorctg-bugIssue is a bug

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions