Skip to content

Rename MockMethodDispatcher.class to MockMethodDispatcher.raw on buildΒ #845

@raphw

Description

@raphw

The org.mockito.internal.creation.bytebuddy.MockMethodDispatcher.class file should be renamed to org.mockito.internal.creation.bytebuddy.MockMethodDispatcher.raw on building Mockito. This way, class loaders are no longer capable of loading the class directly what should never happen. Doing so, the bootstrap injection mechanism cannot be superseeded by child-first class loaders which break mockability due to the dispatcher class being loaded twice by two different class loaders.

This problem was observed with the Robolectrics class loader.

Additionally, the following change is required in the InlineByteBuddyMockMaker when creating the boot jar

String source = "org/mockito/internal/creation/bytebuddy/MockMethodDispatcher";
outputStream.putNextEntry(new JarEntry(source + ".class"));
InputStream inputStream = InlineByteBuddyMockMaker.class.getClassLoader().getResourceAsStream(source + ".raw");
if (inputStream == null) {
  throw new MockitoInitializationException(join(
    "The MockMethodDispatcher class file could not be located " + source,
    "",
    "The class loader responsible: " + InlineByteBuddyMockMaker.class.getClassLoader()
  ));
}
try {
  int length;
  byte[] buffer = new byte[1024];
  while ((length = inputStream.read(buffer)) != -1) {
    outputStream.write(buffer, 0, length);
  }
} finally {
  inputStream.close();
}
outputStream.closeEntry();

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions