private ClassLoader generateClassLoader(File result, File dexCache, ClassLoader parent) {
    try {
      boolean shareClassLoader = sharedClassLoader != null;

      ClassLoader preferredClassLoader = null;
      if (parent != null) {
        preferredClassLoader = parent;
      } else if (sharedClassLoader != null) {
        preferredClassLoader = sharedClassLoader;
      }

      Class baseDexClassLoaderClass = Class.forName("dalvik.system.BaseDexClassLoader");

      if (shareClassLoader) {
        if (!baseDexClassLoaderClass.isAssignableFrom(preferredClassLoader.getClass())) {
          if (!preferredClassLoader.getClass().getName().equals("java.lang.BootClassLoader")) {
            if (!didWarnNonBaseDexClassLoader) {
              System.err.println(
                  "Cannot share classloader as shared classloader '"
                      + preferredClassLoader
                      + "' is not a subclass of '"
                      + baseDexClassLoaderClass
                      + "'");
              didWarnNonBaseDexClassLoader = true;
            }
          }

          shareClassLoader = false;
        }
      }

      // Try to load the class so that it can call hidden APIs. This is required for spying
      // on system classes as real-methods of these classes might call blacklisted APIs
      if (markAsTrusted) {
        try {
          if (shareClassLoader) {
            preferredClassLoader
                .getClass()
                .getMethod("addDexPath", String.class, Boolean.TYPE)
                .invoke(preferredClassLoader, result.getPath(), true);
            return preferredClassLoader;
          } else {
            return (ClassLoader)
                baseDexClassLoaderClass
                    .getConstructor(
                        String.class, File.class, String.class, ClassLoader.class, Boolean.TYPE)
                    .newInstance(
                        result.getPath(),
                        dexCache.getAbsoluteFile(),
                        null,
                        preferredClassLoader,
                        true);
          }
        } catch (InvocationTargetException e) {
          if (e.getCause() instanceof SecurityException) {
            if (!didWarnBlacklistedMethods) {
              System.err.println(
                  "Cannot allow to call blacklisted super methods. "
                      + "This might break spying on system classes."
                      + e.getCause());
              didWarnBlacklistedMethods = true;
            }
          } else {
            throw e;
          }
        }
      }

      if (shareClassLoader) {
        preferredClassLoader
            .getClass()
            .getMethod("addDexPath", String.class)
            .invoke(preferredClassLoader, result.getPath());
        return preferredClassLoader;
      } else {
        return (ClassLoader)
            Class.forName("dalvik.system.DexClassLoader")
                .getConstructor(String.class, String.class, String.class, ClassLoader.class)
                .newInstance(
                    result.getPath(), dexCache.getAbsolutePath(), null, preferredClassLoader);
      }
    } catch (ClassNotFoundException e) {
      throw new UnsupportedOperationException("load() requires a Dalvik VM", e);
    } catch (InvocationTargetException e) {
      throw new RuntimeException(e.getCause());
    } catch (InstantiationException e) {
      throw new AssertionError();
    } catch (NoSuchMethodException e) {
      throw new AssertionError();
    } catch (IllegalAccessException e) {
      throw new AssertionError();
    }
  }
Set shared class loader to use.

<p>If a class wants to call package private methods of another class they need to share a class
loader. One common case for this requirement is a mock class wanting to mock package private
methods of the original class.

<p>If the classLoader is not a subclass of {@code dalvik.system.BaseDexClassLoader} this option
is ignored.

@param classLoader the class loader the new class should be loaded by
android/android_framework/dex-maker/src/main/java/com/android/dx/DexMaker.java