PUBLIC OBJECT

ClassLoaderBuilder

Class loaders are a hassle. They like to interfere with metaprogramming projects like Dagger. Even worse, it's awkward to write unit tests that exercise class loading bugs.

ClassLoaderBuilder is a dangerously delightful class that lets you create alternate realities and rip worm holes between them.

____ public class ClassLoadingTest {
______ static class Walter {}

______ @Test public void loadOneClassTwice() throws Exception {
________ String prefix = ClassLoadingTest.class.getName();
________ ClassLoader loader = new ClassLoaderBuilder()
____________ .withPrivateCopy(prefix)
____________ .build();
________ Class<?> walterClass = Walter.class;
________ Class<?> walternateClass = loader.loadClass(prefix + "$Walter");
________ assertEquals(walternateClass.getName(), walterClass.getName());
________ assertNotSame(walternateClass, walterClass);
______ }
____ }

You can find ClassLoaderBuilder hidden in AOSP's test support.