diff options
author | Robey Pointer <robey@twitter.com> | 2011-04-12 20:37:31 -0700 |
---|---|---|
committer | Christopher Vogt <oss.nsp@cvogt.org> | 2016-11-07 02:08:38 -0500 |
commit | eb3f2f886e7857ad0c08b2be9aeb52ae1605315a (patch) | |
tree | 22673b491166cdfb6282b84f6703af7fa7ab05e9 /libraries | |
parent | 1f9ff6569de3993cb36629f1b8983a65452690a3 (diff) | |
download | cbt-eb3f2f886e7857ad0c08b2be9aeb52ae1605315a.tar.gz cbt-eb3f2f886e7857ad0c08b2be9aeb52ae1605315a.tar.bz2 cbt-eb3f2f886e7857ad0c08b2be9aeb52ae1605315a.zip |
add findClass.
Diffstat (limited to 'libraries')
-rw-r--r-- | libraries/eval/Eval.scala | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/libraries/eval/Eval.scala b/libraries/eval/Eval.scala index ee45256..ebbf1da 100644 --- a/libraries/eval/Eval.scala +++ b/libraries/eval/Eval.scala @@ -93,14 +93,14 @@ class Eval { * Eval[Int](getClass.getResourceAsStream("...")) */ def apply[T](stream: InputStream): T = { - apply(scala.io.Source.fromInputStream(stream).mkString) + apply(Source.fromInputStream(stream).mkString) } /** * Compile an entire source file into the virtual classloader. */ def compile(code: String) { - Eval.compiler(code) + compiler(code) } /** @@ -139,6 +139,10 @@ class Eval { check(scala.io.Source.fromInputStream(stream).mkString) } + def findClass(className: String): Class[_] = { + compiler.findClass(className).getOrElse { throw new ClassNotFoundException("no such class: " + className) } + } + private def uniqueId(code: String): String = { val digest = MessageDigest.getInstance("SHA-1").digest(code.getBytes()) val sha = new BigInteger(1, digest).toString(16) @@ -332,6 +336,21 @@ class Eval { } } + def findClass(className: String): Option[Class[_]] = { + synchronized { + cache.get(className).orElse { + try { + val cls = classLoader.loadClass(className) + cache(className) = cls + Some(cls) + } catch { + case e: ClassNotFoundException => None + } + } + } + } + + /** * Compile scala code. It can be found using the above class loader. */ @@ -355,15 +374,10 @@ class Eval { */ def apply(code: String, className: String, id: String, resetState: Boolean = true): Class[_] = { synchronized { - cache.get(id) match { - case Some(cls) => - cls - case None => - if (resetState) reset() - apply(code) - val cls = classLoader.loadClass(className) - cache(id) = cls - cls + if (resetState) reset() + findClass(className).getOrElse { + apply(code) + findClass(className).get } } } |