diff options
author | Paul Phillips <paulp@improving.org> | 2010-03-21 23:00:14 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-03-21 23:00:14 +0000 |
commit | 1935b66102808cc72262cffca3546f4bb5b7baac (patch) | |
tree | 71f3bd2a0287513c7ba1439f17692866bbaf6b1c /src/compiler | |
parent | 3d7e9c11ad92b125ae5c34105e8799cd5fbdaf7c (diff) | |
download | scala-1935b66102808cc72262cffca3546f4bb5b7baac.tar.gz scala-1935b66102808cc72262cffca3546f4bb5b7baac.tar.bz2 scala-1935b66102808cc72262cffca3546f4bb5b7baac.zip |
Some support code related to partest changes.
Diffstat (limited to 'src/compiler')
4 files changed, 45 insertions, 21 deletions
diff --git a/src/compiler/scala/tools/nsc/interpreter/ByteCode.scala b/src/compiler/scala/tools/nsc/interpreter/ByteCode.scala index 4b8e0ae60c..a998f9dfc8 100644 --- a/src/compiler/scala/tools/nsc/interpreter/ByteCode.scala +++ b/src/compiler/scala/tools/nsc/interpreter/ByteCode.scala @@ -34,8 +34,8 @@ object ByteCode { def scalaSigBytesForPath(path: String) = for { module <- DECODER - method <- decoderMethod("scalaSigBytes", classOf[String]) - names <- method.invoke(module, path).asInstanceOf[Option[Array[Byte]]] + method <- decoderMethod("scalaSigBytes", classOf[String], classOf[ClassLoader]) + names <- method.invoke(module, path, this.getClass.getClassLoader).asInstanceOf[Option[Array[Byte]]] } yield names diff --git a/src/compiler/scala/tools/nsc/io/File.scala b/src/compiler/scala/tools/nsc/io/File.scala index aecbfbd048..74446a699d 100644 --- a/src/compiler/scala/tools/nsc/io/File.scala +++ b/src/compiler/scala/tools/nsc/io/File.scala @@ -13,7 +13,7 @@ package io import java.io.{ FileInputStream, FileOutputStream, BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter, - BufferedInputStream, BufferedOutputStream, IOException, File => JFile } + BufferedInputStream, BufferedOutputStream, IOException, PrintStream, File => JFile } import java.nio.channels.{ Channel, FileChannel } import collection.Traversable import scala.io.Codec @@ -50,8 +50,7 @@ import Path._ */ class File(jfile: JFile)(implicit val creationCodec: Codec = null) extends Path(jfile) -with Streamable.Chars -{ +with Streamable.Chars { def withCodec(codec: Codec): File = new File(jfile)(codec) override def toDirectory: Directory = new Directory(jfile) override def toFile: File = this @@ -65,6 +64,7 @@ with Streamable.Chars /** Obtains a OutputStream. */ def outputStream(append: Boolean = false) = new FileOutputStream(jfile, append) def bufferedOutput(append: Boolean = false) = new BufferedOutputStream(outputStream(append)) + def printStream(append: Boolean = false) = new PrintStream(bufferedOutput(append)) /** Obtains an OutputStreamWriter wrapped around a FileOutputStream. * This should behave like a less broken version of java.io.FileWriter, diff --git a/src/compiler/scala/tools/nsc/util/ScalaClassLoader.scala b/src/compiler/scala/tools/nsc/util/ScalaClassLoader.scala index 9d65a3b2aa..5b1471c90d 100644 --- a/src/compiler/scala/tools/nsc/util/ScalaClassLoader.scala +++ b/src/compiler/scala/tools/nsc/util/ScalaClassLoader.scala @@ -12,8 +12,10 @@ import java.net.URL import ScalaClassLoader._ import scala.util.control.Exception.{ catching } -trait ScalaClassLoader extends JavaClassLoader -{ +trait ScalaClassLoader extends JavaClassLoader { + /** Override to see classloader activity traced */ + protected def trace: Boolean = false + /** Executing an action with this classloader as context classloader */ def asContext[T](action: => T): T = { val oldLoader = getContextLoader @@ -30,6 +32,9 @@ trait ScalaClassLoader extends JavaClassLoader /** Load, link and initialize a class with this classloader */ def tryToInitializeClass[T <: AnyRef](path: String): Option[Class[T]] = tryClass(path, true) + private def tryBody[T <: AnyRef](body: => Any): Option[T] = + catching(classOf[ClassNotFoundException], classOf[SecurityException]) opt body.asInstanceOf[T] + private def tryClass[T <: AnyRef](path: String, initialize: Boolean): Option[Class[T]] = catching(classOf[ClassNotFoundException], classOf[SecurityException]) opt Class.forName(path, initialize, this).asInstanceOf[Class[T]] @@ -42,6 +47,27 @@ trait ScalaClassLoader extends JavaClassLoader } } + override def findClass(name: String) = { + val result = super.findClass(name) + if (trace) println("findClass(%s) = %s".format(name, result)) + result + } + + override def loadClass(name: String, resolve: Boolean) = { + val result = super.loadClass(name, resolve) + if (trace) println("loadClass(%s, %s) = %s".format(name, resolve, result)) + result + } + + /** The actual bytes for a class file, or an empty array if it can't be found. */ + def findBytesForClassName(s: String): Array[Byte] = { + val name = s.replaceAll("""\.""", "/") + ".class" + val url = this.getResource(name) + + if (url == null) Array() + else new io.Streamable.Bytes { def inputStream() = url.openStream } . toByteArray() + } + /** Run the main method of a class to be loaded by this classloader */ def run(objectName: String, arguments: Seq[String]) { val clsToRun = tryToInitializeClass(objectName) getOrElse ( @@ -56,13 +82,20 @@ trait ScalaClassLoader extends JavaClassLoader } } - object ScalaClassLoader { class URLClassLoader(urls: Seq[URL], parent: JavaClassLoader) extends java.net.URLClassLoader(urls.toArray, parent) with ScalaClassLoader { + + private var classloaderURLs = urls.toList + /** Override to widen to public */ - override def addURL(url: URL) = super.addURL(url) + override def addURL(url: URL) = { + classloaderURLs +:= url + super.addURL(url) + } + + override def toString = urls.mkString("URLClassLoader(\n ", "\n ", "\n)\n") } def setContextLoader(cl: JavaClassLoader) = Thread.currentThread.setContextClassLoader(cl) @@ -70,8 +103,8 @@ object ScalaClassLoader { def getSystemLoader(): ScalaClassLoader = new JavaClassLoader(JavaClassLoader.getSystemClassLoader()) with ScalaClassLoader def defaultParentClassLoader() = findExtClassLoader() - def fromURLs(urls: Seq[URL]): URLClassLoader = - new URLClassLoader(urls.toList, defaultParentClassLoader()) + def fromURLs(urls: Seq[URL], parent: ClassLoader = defaultParentClassLoader()): URLClassLoader = + new URLClassLoader(urls.toList, parent) /** True if supplied class exists in supplied path */ def classExists(urls: Seq[URL], name: String): Boolean = @@ -91,15 +124,6 @@ object ScalaClassLoader { search(getContextLoader()) } - /** The actual bytes for a class file, or an empty array if it can't be found. */ - def findBytesForClassName(s: String): Array[Byte] = { - val name = s.replaceAll("""\.""", "/") + ".class" - val url = getSystemLoader.getResource(name) - - if (url == null) Array() - else new io.Streamable.Bytes { def inputStream() = url.openStream } . toByteArray() - } - /** Finding what jar a clazz or instance came from */ def origin(x: Any): Option[URL] = originOfClass(x.asInstanceOf[AnyRef].getClass) def originOfClass(x: Class[_]): Option[URL] = diff --git a/src/compiler/scala/tools/util/PathResolver.scala b/src/compiler/scala/tools/util/PathResolver.scala index cadd4d2051..58ac3bfa56 100644 --- a/src/compiler/scala/tools/util/PathResolver.scala +++ b/src/compiler/scala/tools/util/PathResolver.scala @@ -70,7 +70,7 @@ object PathResolver { */ def useJavaClassPath = true // hypothetically: - // def useJavaClassPath = propIsSet("scala.auto.class.path") + // def useJavaClassPath = propIsSet("scala.classpath.guess") override def toString = """ |object Environment { |