Skip to content

Commit

Permalink
Allow build tools to customize plugin classloader creation
Browse files Browse the repository at this point in the history
An extension point analagous to `findMacroClassLoader`
  • Loading branch information
retronym committed Oct 15, 2018
1 parent 5175550 commit 01a9dbd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/compiler/scala/tools/nsc/plugins/Plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ object Plugin {
* mitigate the cost of dynamic classloading as it has been
* measured in https://github.com/scala/scala-dev/issues/458.
*/
private def loaderFor(locations: Seq[Path], disableCache: Boolean): ScalaClassLoader = {
def loaderFor(locations: Seq[Path], disableCache: Boolean): ScalaClassLoader = {
def newLoader = () => {
val compilerLoader = classOf[Plugin].getClassLoader
val urls = locations map (_.toURL)
Expand Down Expand Up @@ -155,7 +155,7 @@ object Plugin {
paths: List[List[Path]],
dirs: List[Path],
ignoring: List[String],
disableClassLoaderCache: Boolean): List[Try[AnyClass]] =
findPluginClassloader: (Seq[Path] => ClassLoader)): List[Try[AnyClass]] =
{
// List[(jar, Try(descriptor))] in dir
def scan(d: Directory) =
Expand All @@ -166,7 +166,7 @@ object Plugin {
// scan plugin dirs for jars containing plugins, ignoring dirs with none and other jars
val fromDirs: PDResults = dirs filter (_.isDirectory) flatMap { d =>
scan(d.toDirectory) collect {
case (j, Success(pd)) => Success((pd, loaderFor(Seq(j), disableClassLoaderCache)))
case (j, Success(pd)) => Success((pd, findPluginClassloader(Seq(j))))
}
}

Expand All @@ -183,7 +183,7 @@ object Plugin {
loop(ps)
}
val fromPaths: PDResults = paths map (p => (p, findDescriptor(p))) map {
case (p, Success(pd)) => Success((pd, loaderFor(p, disableClassLoaderCache)))
case (p, Success(pd)) => Success((pd, findPluginClassloader(p)))
case (_, Failure(e)) => Failure(e)
}

Expand Down
6 changes: 5 additions & 1 deletion src/compiler/scala/tools/nsc/plugins/Plugins.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ trait Plugins { global: Global =>
def injectDefault(s: String) = if (s.isEmpty) Defaults.scalaPluginPath else s
asPath(settings.pluginsDir.value) map injectDefault map Path.apply
}
val maybes = Plugin.loadAllFrom(paths, dirs, settings.disable.value, settings.YcachePluginClassLoader.value == settings.CachePolicy.None.name)
val maybes = Plugin.loadAllFrom(paths, dirs, settings.disable.value, findPluginClassLoader(_))
val (goods, errors) = maybes partition (_.isSuccess)
// Explicit parameterization of recover to avoid -Xlint warning about inferred Any
errors foreach (_.recover[Any] {
Expand All @@ -53,6 +53,10 @@ trait Plugins { global: Global =>
classes map (Plugin.instantiate(_, this))
}

protected def findPluginClassLoader(classpath: Seq[Path]): ClassLoader = {
Plugin.loaderFor(classpath, settings.YcachePluginClassLoader.value == settings.CachePolicy.None.name)
}

protected lazy val roughPluginsList: List[Plugin] = loadRoughPluginsList()

/** Load all available plugins. Skips plugins that
Expand Down

0 comments on commit 01a9dbd

Please sign in to comment.