diff options
author | Eugene Burmako <xeno.by@gmail.com> | 2012-01-31 12:12:23 +0100 |
---|---|---|
committer | Eugene Burmako <xeno.by@gmail.com> | 2012-02-01 23:10:55 +0100 |
commit | 1e0707786b118e3e33379e7acdc75306b45e6547 (patch) | |
tree | 2569832d1ee4db3466cec75ce0b8b10431c81f62 /src/compiler/scala/tools/nsc/interpreter/AbstractFileClassLoader.scala | |
parent | fbd5efe49cf23b446762dfa5026e8bac82ab04fc (diff) | |
download | scala-1e0707786b118e3e33379e7acdc75306b45e6547.tar.gz scala-1e0707786b118e3e33379e7acdc75306b45e6547.tar.bz2 scala-1e0707786b118e3e33379e7acdc75306b45e6547.zip |
Hardens classToType logic
Reflection now correctly processes classes, objects and inner classes
that are declared in classes and objects.
However classToType still crashes on compound types and local classes.
For more information on those, follow the links:
* Compound types: https://issues.scala-lang.org/browse/SI-5430
* Local classes: https://issues.scala-lang.org/browse/SI-5431
Fixes https://issues.scala-lang.org/browse/SI-5256.
Review by @paulp, @odersky.
Diffstat (limited to 'src/compiler/scala/tools/nsc/interpreter/AbstractFileClassLoader.scala')
-rw-r--r-- | src/compiler/scala/tools/nsc/interpreter/AbstractFileClassLoader.scala | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/src/compiler/scala/tools/nsc/interpreter/AbstractFileClassLoader.scala b/src/compiler/scala/tools/nsc/interpreter/AbstractFileClassLoader.scala index 70fa740eeb..3a605975f4 100644 --- a/src/compiler/scala/tools/nsc/interpreter/AbstractFileClassLoader.scala +++ b/src/compiler/scala/tools/nsc/interpreter/AbstractFileClassLoader.scala @@ -21,9 +21,6 @@ class AbstractFileClassLoader(root: AbstractFile, parent: ClassLoader) { // private val defined = mutable.Map[String, Class[_]]() - // Widening to public - override def getPackage(name: String) = super.getPackage(name) - override protected def trace = sys.props contains "scala.debug.classloader" @@ -47,6 +44,22 @@ class AbstractFileClassLoader(root: AbstractFile, parent: ClassLoader) } } + protected def dirNameToPath(name: String): String = + name.replace('.', '/') + + protected def findAbstractDir(name: String): AbstractFile = { + var file: AbstractFile = root + val pathParts = dirNameToPath(name) split '/' + + for (dirPart <- pathParts) { + file = file.lookupName(dirPart, true) + if (file == null) + return null + } + + return file + } + override def getResourceAsStream(name: String) = findAbstractFile(name) match { case null => super.getResourceAsStream(name) case file => file.input @@ -78,4 +91,24 @@ class AbstractFileClassLoader(root: AbstractFile, parent: ClassLoader) // case null => super.getResource(name) // case file => new URL(...) // } + + private val packages = mutable.Map[String, Package]() + + override def definePackage(name: String, specTitle: String, specVersion: String, specVendor: String, implTitle: String, implVersion: String, implVendor: String, sealBase: URL): Package = { + throw new UnsupportedOperationException() + } + + override def getPackage(name: String): Package = { + findAbstractDir(name) match { + case null => super.getPackage(name) + case file => packages.getOrElseUpdate(name, { + val ctor = classOf[Package].getDeclaredConstructor(classOf[String], classOf[String], classOf[String], classOf[String], classOf[String], classOf[String], classOf[String], classOf[URL], classOf[ClassLoader]) + ctor.setAccessible(true) + ctor.newInstance(name, null, null, null, null, null, null, null, this) + }) + } + } + + override def getPackages(): Array[Package] = + root.iterator.filter(_.isDirectory).map(dir => getPackage(dir.name)).toArray } |