package scala.tools.nsc
package interpreter
import java.lang.reflect
import java.util.concurrent.ConcurrentHashMap
import util.ScalaClassLoader
import ScalaClassLoader.getSystemLoader
object ByteCode {
private lazy val DECODER: Option[AnyRef] =
for (clazz <- getSystemLoader.tryToLoadClass[AnyRef]("scala.tools.scalap.Decode$")) yield
clazz.getField("MODULE$").get()
private def decoderMethod(name: String, args: JClass*): Option[reflect.Method] = {
for (decoder <- DECODER ; m <- Option(decoder.getClass.getMethod(name, args: _*))) yield m
}
private lazy val aliasMap = {
for (module <- DECODER ; method <- decoderMethod("typeAliases", classOf[String])) yield
method.invoke(module, _: String).asInstanceOf[Option[Map[String, String]]]
}
def scalaSigBytesForPath(path: String) =
for {
module <- DECODER
method <- decoderMethod("scalaSigAnnotationBytes", classOf[String])
names <- method.invoke(module, path).asInstanceOf[Option[Array[Byte]]]
}
yield names
def caseParamNamesForPath(path: String) =
for {
module <- DECODER
method <- decoderMethod("caseParamNames", classOf[String])
names <- method.invoke(module, path).asInstanceOf[Option[List[String]]]
}
yield names
def aliasesForPackage(pkg: String) = aliasMap flatMap (_(pkg))
def aliasForType(path: String): Option[String] = {
val (pkg, name) = (path lastIndexOf '.') match {
case -1 => return None
case idx => (path take idx, path drop (idx + 1))
}
aliasesForPackage(pkg) flatMap (_ get name)
}
}