summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/interpreter/AbstractFileClassLoader.scala
blob: 0c3d24d46fa0db533c8d4cc0a83b4ecac3ffc7f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* NSC -- new Scala compiler
 * Copyright 2005-2010 LAMP/EPFL
 */

package scala.tools.nsc
package interpreter

import scala.tools.nsc.io.AbstractFile
import util.ScalaClassLoader

/**
 * A class loader that loads files from a {@link scala.tools.nsc.io.AbstractFile}.
 *
 * @author Lex Spoon
 */
class AbstractFileClassLoader(root: AbstractFile, parent: ClassLoader)
    extends ClassLoader(parent)
    with ScalaClassLoader
{
  def getBytesForClass(name: String): Array[Byte] = {
    def onull[T](x: T): T = if (x == null) throw new ClassNotFoundException(name) else x
    var file: AbstractFile = root
    val pathParts = name.split("[./]").toList

    for (dirPart <- pathParts.init)
      file = onull(file.lookupName(dirPart, true))

    file = onull(file.lookupName(pathParts.last+".class", false))
    file.toByteArray
  }

  override def findClass(name: String): Class[_] = {
    val bytes = getBytesForClass(name)
    defineClass(name, bytes, 0, bytes.length)
  }
}