From a84abd088df0105c542aa3192de5a634f3ceda35 Mon Sep 17 00:00:00 2001 From: Heather Miller Date: Wed, 15 Oct 2014 16:46:21 -0700 Subject: SI-6502 Addressing review comments --- src/repl/scala/tools/nsc/interpreter/ILoop.scala | 28 ++++++++++++++++-------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'src/repl') diff --git a/src/repl/scala/tools/nsc/interpreter/ILoop.scala b/src/repl/scala/tools/nsc/interpreter/ILoop.scala index 9cbb6ae574..c35de9a424 100644 --- a/src/repl/scala/tools/nsc/interpreter/ILoop.scala +++ b/src/repl/scala/tools/nsc/interpreter/ILoop.scala @@ -19,6 +19,7 @@ import scala.reflect.internal.util.{ BatchSourceFile, ScalaClassLoader } import ScalaClassLoader._ import scala.reflect.io.{ File, Directory } import scala.tools.util._ +import io.AbstractFile import scala.collection.generic.Clearable import scala.concurrent.{ ExecutionContext, Await, Future, future } import ExecutionContext.Implicits._ @@ -221,7 +222,7 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter) nullary("power", "enable power user mode", powerCmd), nullary("quit", "exit the interpreter", () => Result(keepRunning = false, None)), cmd("replay", "[options]", "reset the repl and replay all previous commands", replayCommand), - cmd("require", "", "add a jar or directory to the classpath", require), + cmd("require", "", "add a jar to the classpath", require), cmd("reset", "[options]", "reset the repl to its initial state, forgetting all session entries", resetCommand), cmd("save", "", "save replayable session to a file", saveCommand), shCommand, @@ -613,7 +614,8 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter) if (f.exists) { addedClasspath = ClassPath.join(addedClasspath, f.path) intp.addUrlsToClassPath(f.toURI.toURL) - echo("Added '%s'. Your new classpath is:\n\"%s\"".format(f.path, intp.global.classPath.asClasspathString)) + echo("Added '%s' to classpath.".format(f.path, intp.global.classPath.asClasspathString)) + repldbg("Added '%s'. Your new classpath is:\n\"%s\"".format(f.path, intp.global.classPath.asClasspathString)) } else echo("The path '" + f + "' doesn't seem to exist.") } @@ -647,21 +649,28 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter) echo("Adding directories to the classpath is not supported. Add a jar instead.") return } - val jarFile = new java.util.jar.JarFile(arg) - val entries = jarFile.entries + + val jarFile = AbstractFile.getDirectory(new java.io.File(arg)) + + def flatten(f: AbstractFile): Iterator[AbstractFile] = + if (f.isClassContainer) f.iterator.flatMap(flatten) + else Iterator(f) + + val entries = flatten(jarFile) val cloader = new InfoClassLoader var exists = false - while (entries.hasMoreElements()) { - val entry = entries.nextElement() + while (entries.hasNext) { + val entry = entries.next() // skip directories and manifests - if (!entry.isDirectory() && !entry.getName().endsWith("MF")) { + if (!entry.isDirectory && entry.name.endsWith(".class")) { // for each entry get InputStream - val is = jarFile.getInputStream(entry) + val is = entry.input // read InputStream into Array[Byte] val arr = readFully(is) val clazz = cloader.classOf(arr) try { + // pass initialize = false because we don't want to execute class initializers: Class.forName(clazz.getName(), false, intp.classLoader) exists = true } catch { @@ -673,7 +682,8 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter) if (f.exists && !exists) { addedClasspath = ClassPath.join(addedClasspath, f.path) intp.addUrlsToClassPath(f.toURI.toURL) - echo("Added '%s'. Your new classpath is:\n\"%s\"".format(f.path, intp.global.classPath.asClasspathString)) + echo("Added '%s' to classpath.".format(f.path, intp.global.classPath.asClasspathString)) + repldbg("Added '%s'. Your new classpath is:\n\"%s\"".format(f.path, intp.global.classPath.asClasspathString)) } else if (exists) { echo("The path '" + f + "' cannot be loaded, because existing classpath entries conflict.") } else echo("The path '" + f + "' doesn't seem to exist.") -- cgit v1.2.3