summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-03-21 23:00:14 +0000
committerPaul Phillips <paulp@improving.org>2010-03-21 23:00:14 +0000
commit1935b66102808cc72262cffca3546f4bb5b7baac (patch)
tree71f3bd2a0287513c7ba1439f17692866bbaf6b1c /src
parent3d7e9c11ad92b125ae5c34105e8799cd5fbdaf7c (diff)
downloadscala-1935b66102808cc72262cffca3546f4bb5b7baac.tar.gz
scala-1935b66102808cc72262cffca3546f4bb5b7baac.tar.bz2
scala-1935b66102808cc72262cffca3546f4bb5b7baac.zip
Some support code related to partest changes.
Diffstat (limited to 'src')
-rw-r--r--src/compiler/scala/tools/nsc/interpreter/ByteCode.scala4
-rw-r--r--src/compiler/scala/tools/nsc/io/File.scala6
-rw-r--r--src/compiler/scala/tools/nsc/util/ScalaClassLoader.scala54
-rw-r--r--src/compiler/scala/tools/util/PathResolver.scala2
-rw-r--r--src/scalap/scala/tools/scalap/Decode.scala8
-rw-r--r--src/scalap/scala/tools/scalap/Main.scala12
6 files changed, 56 insertions, 30 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 {
diff --git a/src/scalap/scala/tools/scalap/Decode.scala b/src/scalap/scala/tools/scalap/Decode.scala
index 6e55558249..4b319da13a 100644
--- a/src/scalap/scala/tools/scalap/Decode.scala
+++ b/src/scalap/scala/tools/scalap/Decode.scala
@@ -10,7 +10,8 @@
package scala.tools.scalap
import scala.tools.scalap.scalax.rules.scalasig._
-import scala.tools.nsc.util.ScalaClassLoader.{ getSystemLoader, findBytesForClassName }
+import scala.tools.nsc.util.ScalaClassLoader
+import scala.tools.nsc.util.ScalaClassLoader.getSystemLoader
import Main.SCALA_SIG
/** Temporary decoder. This would be better off in the scala.tools.nsc
@@ -26,8 +27,9 @@ object Decode {
/** Return the classfile bytes representing the scala sig attribute.
*/
- def scalaSigBytes(name: String): Option[Array[Byte]] = {
- val bytes = findBytesForClassName(name)
+ def scalaSigBytes(name: String): Option[Array[Byte]] = scalaSigBytes(name, getSystemLoader())
+ def scalaSigBytes(name: String, classLoader: ScalaClassLoader): Option[Array[Byte]] = {
+ val bytes = classLoader.findBytesForClassName(name)
val reader = new ByteArrayReader(bytes)
val cf = new Classfile(reader)
cf.scalaSigAttribute map (_.data)
diff --git a/src/scalap/scala/tools/scalap/Main.scala b/src/scalap/scala/tools/scalap/Main.scala
index eee689c189..d386e7a08f 100644
--- a/src/scalap/scala/tools/scalap/Main.scala
+++ b/src/scalap/scala/tools/scalap/Main.scala
@@ -97,16 +97,16 @@ object Main {
baos.toString
}
-
- def decompileScala(bytes: Array[Byte], isPackageObject: Boolean) = {
+ def getDecompiledScala(bytes: Array[Byte], isPackageObject: Boolean) = {
val byteCode = ByteCode(bytes)
val classFile = ClassFileParser.parse(byteCode)
- classFile.attribute(SCALA_SIG).map(_.byteCode).map(ScalaSigAttributeParsers.parse) match {
- case Some(scalaSig) => Console.println(parseScalaSignature(scalaSig, isPackageObject))
- case None => //Do nothing
- }
+ val sig = classFile.attribute(SCALA_SIG).map(_.byteCode).map(ScalaSigAttributeParsers.parse)
+
+ sig map (x => parseScalaSignature(x, isPackageObject))
}
+ def decompileScala(bytes: Array[Byte], isPackageObject: Boolean) =
+ getDecompiledScala(bytes, isPackageObject) foreach (Console println _)
/**Executes scalap with the given arguments and classpath for the
* class denoted by <code>classname</code>.