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

InstrumentingClassLoader fails as a parent ClassLoader #2526

Closed
ejona86 opened this issue Jun 21, 2016 · 2 comments
Closed

InstrumentingClassLoader fails as a parent ClassLoader #2526

ejona86 opened this issue Jun 21, 2016 · 2 comments

Comments

@ejona86
Copy link

ejona86 commented Jun 21, 2016

Description

InstrumentingClassLoader does not work properly as a parent class loader, due to missing loadClass(String, boolean) overload which is called by ClassLoader.loadClass(String, boolean). Instead of overriding loadClass(String), InstrumentingClassLoader should override loadClass(String, boolean).

This can produce very weird behavior since multiple class definitions for the same class will exist, including ServiceLoader throwing java.util.ServiceConfigurationError "Provider X not a subtype" since the extending class and its ancestors were loaded from the normal class loader but the class doing the call is loaded from InstrumentingClassLoader

Steps to Reproduce

These two lines return different classes, but they shouldn't:

Class<?> class1 = Class.forName("some.class", false,
    this.getClass().getClassLoader());
Class<?> class2 = Class.forName("some.class", false,
    new ClassLoader(this.getClass().getClassLoader()){});
System.out.println(class1 == class2);

Robolectric & Android Version

3.1 SNAPSHOT, commit 78ef7fa

@ejona86
Copy link
Author

ejona86 commented Jul 1, 2016

My previous test didn't quite precise enough, because it depended a lot on the environment and order of operations happening outside of the code snippet. This unit test should work:

class OnlyUsedByChildClassLoaderTest {}

@Test
public void testChildClassLoaderReturnsSameInstanceAsParent() throws Exception {
  // Must not use .class. This class must be loaded from the child first
  String name = getClass().getName() + "$OnlyUsedByChildClassLoaderTest";
  // Note that reversing this line and the next line produces different results
  Class<?> fromChild = Class.forName(name, false,
      new ClassLoader(getClass().getClassLoader()) {});
  Class<?> fromIntrumented = Class.forName(name, false,
      getClass().getClassLoader());
  // Just a sanity test
  assertThat(fromIntrumented).isSameAs(OnlyUsedByChildClassLoaderTest.class);
  // The real verification
  assertThat(fromChild).isSameAs(fromIntrumented);
}

@jongerrish
Copy link
Contributor

I believe this has been fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants