Skip to content

Unnecessary reflections in code generated for arrays #743

Description

@volivan239

Description

In some cases codegen works with arrays using reflection even if there is no need for it.

To Reproduce

Run plugin on the following code:

package rndpkg;

class C {
    int x;
    public C(int x) { this.x = x; }
}

public class SomeClass {
    public int f(C[] c) {
        c[0].x -= 1;
        return c[0].x;
    }
}

Expected behavior

Produced tests don't use reflection.

Actual behavior

Tests look like this:

  @Test
  @DisplayName("f: c = C[0] -> throw ArrayIndexOutOfBoundsException")
  public void testFThrowsAIOOBEWithEmptyObjectArray() throws Throwable {
      SomeClass someClass = new SomeClass();
      Object[] c = createArray("rndpkg.C", 0);
      
      /* This test fails because method [rndpkg.SomeClass.f] produces [java.lang.ArrayIndexOutOfBoundsException: 0]
          rndpkg.SomeClass.f(SomeClass.java:10) */
      Class someClassClazz = Class.forName("rndpkg.SomeClass");
      Class cType = Class.forName("[Lrndpkg.C;");
      Method fMethod = someClassClazz.getDeclaredMethod("f", cType);
      fMethod.setAccessible(true);
      Object[] fMethodArguments = new Object[1];
      fMethodArguments[0] = c;
      try {
          fMethod.invoke(someClass, fMethodArguments);
      } catch (InvocationTargetException invocationTargetException) {
          throw invocationTargetException.getTargetException();
      }
  }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions