/*
 * Dotty (https://dotty.epfl.ch) Copyright 2012-2020 EPFL Copyright 2012-2020 Lightbend, Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package scala_maven;

import java.net.URL;
import java.net.URLClassLoader;

// based on
// https://github.com/lampepfl/dotty/blob/96401b68a8bce4c125859806b0c24a1ffe3cbc1e/sbt-bridge/src/xsbt/CompilerClassLoader.java
public class ScalaCompilerLoader extends URLClassLoader {
  private final ClassLoader sbtLoader;

  public ScalaCompilerLoader(URL[] urls, ClassLoader sbtLoader) {
    super(urls, null);
    this.sbtLoader = sbtLoader;
  }

  @Override
  public Class<?> loadClass(String className, boolean resolve) throws ClassNotFoundException {
    if (className.startsWith("xsbti.")) {
      // We can't use the loadClass overload with two arguments because it's
      // protected, but we can do the same by hand (the classloader instance
      // from which we call resolveClass does not matter).
      Class<?> c = sbtLoader.loadClass(className);
      if (resolve) resolveClass(c);
      return c;
    } else {
      return super.loadClass(className, resolve);
    }
  }
}

/*
 * Dotty (https://dotty.epfl.ch) Copyright 2012-2020 EPFL Copyright 2012-2020 Lightbend, Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package scala_maven;

import sbt.internal.inc.classpath.ClasspathUtil;

import java.net.URL;
import java.net.URLClassLoader;

// based on
// https://github.com/lampepfl/dotty/blob/96401b68a8bce4c125859806b0c24a1ffe3cbc1e/sbt-bridge/src/xsbt/CompilerClassLoader.java
public class ScalaCompilerLoader extends URLClassLoader {
  private final ClassLoader sbtLoader;

  public ScalaCompilerLoader(URL[] urls, ClassLoader sbtLoader) {
    super(urls, ClasspathUtil.rootLoader());
    this.sbtLoader = sbtLoader;
  }

  @Override
  public Class<?> loadClass(String className, boolean resolve) throws ClassNotFoundException {
    if (className.startsWith("xsbti.")) {
      // We can't use the loadClass overload with two arguments because it's
      // protected, but we can do the same by hand (the classloader instance
      // from which we call resolveClass does not matter).
      Class<?> c = sbtLoader.loadClass(className);
      if (resolve) resolveClass(c);
      return c;
    } else {
      return super.loadClass(className, resolve);
    }
  }
}

Fix incremental compiler not being able to find JDK classes when compiler macros with Java 11, close #502