mirror of https://github.com/sbt/sbt.git
Merge pull request #366 from alexarchambault/topic/system-classloader
Use system classloader as base classloader
This commit is contained in:
commit
e430b6051c
|
|
@ -382,7 +382,7 @@ public class Bootstrap {
|
|||
parentClassLoader = new IsolatedClassLoader(contextURLs, parentClassLoader, new String[]{ isolationID });
|
||||
}
|
||||
|
||||
ClassLoader classLoader = new BootstrapClassLoader(localURLs.toArray(new URL[localURLs.size()]), parentClassLoader);
|
||||
ClassLoader classLoader = new URLClassLoader(localURLs.toArray(new URL[localURLs.size()]), parentClassLoader);
|
||||
|
||||
Class<?> mainClass = null;
|
||||
Method mainMethod = null;
|
||||
|
|
|
|||
|
|
@ -1,25 +0,0 @@
|
|||
package coursier;
|
||||
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
public class BootstrapClassLoader extends URLClassLoader {
|
||||
|
||||
public BootstrapClassLoader(
|
||||
URL[] urls,
|
||||
ClassLoader parent
|
||||
) {
|
||||
super(urls, parent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Can be called by reflection by launched applications, to find the "main" `ClassLoader`
|
||||
* that loaded them, and possibly short-circuit it to load other things for example.
|
||||
*
|
||||
* The `launch` command of coursier does that.
|
||||
*/
|
||||
public boolean isBootstrapLoader() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -584,22 +584,7 @@ class Helper(
|
|||
|
||||
def contextLoader = Thread.currentThread().getContextClassLoader
|
||||
|
||||
// TODO Would ClassLoader.getSystemClassLoader be better here?
|
||||
lazy val baseLoader: ClassLoader =
|
||||
Launch.mainClassLoader(contextLoader)
|
||||
.flatMap(cl => Option(cl.getParent))
|
||||
.getOrElse {
|
||||
// proguarded -> no risk of conflicts, no absolute need to find a specific ClassLoader
|
||||
val isProguarded = Try(contextLoader.loadClass("coursier.cli.Launch")).isFailure
|
||||
if (warnBaseLoaderNotFound && !isProguarded && common.verbosityLevel >= 0)
|
||||
Console.err.println(
|
||||
"Warning: cannot find the main ClassLoader that launched coursier.\n" +
|
||||
"Was coursier launched by its main launcher? " +
|
||||
"The ClassLoader of the application that is about to be launched will be intertwined " +
|
||||
"with the one of coursier, which may be a problem if their dependencies conflict."
|
||||
)
|
||||
contextLoader
|
||||
}
|
||||
def baseLoader = ClassLoader.getSystemClassLoader
|
||||
|
||||
lazy val (parentLoader, filteredFiles) = {
|
||||
|
||||
|
|
|
|||
|
|
@ -6,34 +6,8 @@ import java.net.{ URL, URLClassLoader }
|
|||
|
||||
import caseapp._
|
||||
|
||||
import scala.annotation.tailrec
|
||||
import scala.language.reflectiveCalls
|
||||
import scala.util.Try
|
||||
|
||||
object Launch {
|
||||
|
||||
@tailrec
|
||||
def mainClassLoader(cl: ClassLoader): Option[ClassLoader] =
|
||||
if (cl == null)
|
||||
None
|
||||
else {
|
||||
val isMainLoader = try {
|
||||
val cl0 = cl.asInstanceOf[Object {
|
||||
def isBootstrapLoader: Boolean
|
||||
}]
|
||||
|
||||
cl0.isBootstrapLoader
|
||||
} catch {
|
||||
case e: Exception =>
|
||||
false
|
||||
}
|
||||
|
||||
if (isMainLoader)
|
||||
Some(cl)
|
||||
else
|
||||
mainClassLoader(cl.getParent)
|
||||
}
|
||||
|
||||
def run(
|
||||
loader: ClassLoader,
|
||||
mainClass: String,
|
||||
|
|
@ -125,19 +99,8 @@ case class Launch(
|
|||
else
|
||||
options.mainClass
|
||||
|
||||
val extraJars = options.extraJars.filter(_.nonEmpty)
|
||||
|
||||
val loader =
|
||||
if (extraJars.isEmpty)
|
||||
helper.loader
|
||||
else
|
||||
new URLClassLoader(
|
||||
extraJars.map(new File(_).toURI.toURL).toArray,
|
||||
helper.loader
|
||||
)
|
||||
|
||||
Launch.run(
|
||||
loader,
|
||||
helper.loader,
|
||||
mainClass,
|
||||
userArgs,
|
||||
options.common.verbosityLevel
|
||||
|
|
|
|||
Loading…
Reference in New Issue