From 7cda6c28d59bf226c3bef767788ef395e72b8165 Mon Sep 17 00:00:00 2001 From: Christopher Vogt Date: Mon, 7 Nov 2016 02:53:00 -0500 Subject: Make classloader overridable. CBT isolates classloaders, so Eval could only see itself, but it often needs to see things in the classloaders of user code, so now we can provie the classloader. --- libraries/eval/Eval.scala | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'libraries/eval') diff --git a/libraries/eval/Eval.scala b/libraries/eval/Eval.scala index 057fd53..395fc2f 100644 --- a/libraries/eval/Eval.scala +++ b/libraries/eval/Eval.scala @@ -94,7 +94,7 @@ class Eval(target: Option[File]) { Seq( new IncludePreprocessor( Seq( - new ClassScopedResolver(getClass), + new ClassLoaderResolver(classLoader), new FilesystemResolver(new File(".")), new FilesystemResolver(new File("." + File.separator + "config")) ) ++ ( @@ -387,15 +387,17 @@ class Eval(target: Option[File]) { new FileInputStream(file(path)) } - class ClassScopedResolver(clazz: Class[_]) extends Resolver { + class ClassLoaderResolver(classLoader: ClassLoader) extends Resolver { private[this] def quotePath(path: String) = "/" + path - def resolvable(path: String): Boolean = - clazz.getResourceAsStream(quotePath(path)) != null + def resolvable(path: String): Boolean = { + classLoader.getResourceAsStream(quotePath(path)) != null + } - def get(path: String): InputStream = - clazz.getResourceAsStream(quotePath(path)) + def get(path: String): InputStream = { + classLoader.getResourceAsStream(quotePath(path)) + } } class ResolutionFailedException(message: String) extends Exception @@ -464,6 +466,12 @@ class Eval(target: Option[File]) { classpath.value = (pathList ::: impliedClassPath).mkString(File.pathSeparator) } + /** Class loader for finding classes compiled by StringCompiler. + * Override if the classloader of Eval does not have access to + * all classes used by the code it compiles. + */ + def classLoader = this.getClass.getClassLoader + /** * Dynamic scala compiler. Lots of (slow) state is created, so it may be advantageous to keep * around one of these and reuse it. @@ -522,7 +530,7 @@ class Eval(target: Option[File]) { * Class loader for finding classes compiled by this StringCompiler. * After each reset, this class loader will not be able to find old compiled classes. */ - var classLoader = new AbstractFileClassLoader(target, this.getClass.getClassLoader) + var classLoader = new AbstractFileClassLoader(target, Eval.this.classLoader) def reset() { targetDir match { @@ -539,7 +547,7 @@ class Eval(target: Option[File]) { } cache.clear() reporter.reset() - classLoader = new AbstractFileClassLoader(target, this.getClass.getClassLoader) + classLoader = new AbstractFileClassLoader(target, Eval.this.classLoader) } def resetReporter(): Unit = { -- cgit v1.2.3