scala.mobile.Location.scala Maven / Gradle / Ivy
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2002-2010, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
package scala.mobile
import java.lang.ClassLoader
import java.net._
import scala.collection.mutable._
/** The class Location provides a create
* method to instantiate objects from a network location by
* specifying the URL address of the jar/class file.
*
* An update of the jar/class file should not break your code as far
* as the used class names and method signatures are the same.
*
* Example:
* val url = new URL("http://scala-lang.org/classes/examples.jar");
* val obj = new Location(url) create "examples.sort";
*
* @see Code
*
* @author Stephane Micheloud
* @version 1.0, 04/05/2004
*/
class Location(url: URL) {
/** A cache containing all class loaders of this location.
*/
private var lcache: Map[URL, ClassLoader] = new HashMap
/** The class loader associated with this location.
*/
private val loader = if (url eq null)
ClassLoader.getSystemClassLoader()
else
lcache.get(url) match {
case Some(cl) =>
cl
case _ =>
val cl = new URLClassLoader(Array(url))
lcache(url) = cl
cl
}
/** A cache containing all classes of this location.
*/
private var ccache: Map[String, java.lang.Class[T] forSome { type T }] = new HashMap
/** Return the code description for the string className
* at this location.
*
* @param classname the name of the class
* @return the code description corresponding to
* className.
*/
def create(className: String) = new Code(
ccache.get(className) match {
case Some(clazz) =>
clazz
case _ =>
val clazz = if (loader.loadClass(className).isInterface()) {
// Scala source: class A { ... };
// Java bytecode: interface A.class + class A$class.class
loader.loadClass(className + "$class")
}
else {
// Scala source: object A { ... };
// Java bytecode: interface A.class + class A$.class
loader.loadClass(className + "$")
}
ccache(className) = clazz
clazz
}
)
}
/** The object Location can be used to instantiate
* objects on the same Java VM. It is just provided to illustrate
* the special case where resources are available locally.
*
* Example:
* val obj = Location.create("xcode.Math");
* val x = obj[Int, Int]("square")(5);
*
* @author Stephane Micheloud
* @version 1.0, 04/05/2004
*/
object Location extends Location(null)
© 2015 - 2025 Weber Informatics LLC | Privacy Policy