src/Pure/System/invoke_scala.scala
author wenzelm
Mon, 11 Jul 2011 17:14:30 +0200
changeset 43751 8c7f69f1825b
parent 43748 c70bd78ec83c
child 44158 fe6d1ae7a065
permissions -rw-r--r--
proper InvocationTargetException.getCause for indirect exceptions; capture hard errors to ensure protocol integrity; tuned error messages;

/*  Title:      Pure/System/invoke_scala.scala
    Author:     Makarius

JVM method invocation service via Scala layer.
*/

package isabelle


import java.lang.reflect.{Method, Modifier, InvocationTargetException}
import scala.util.matching.Regex


object Invoke_Scala
{
  /* method reflection */

  private val Ext = new Regex("(.*)\\.([^.]*)")
  private val STRING = Class.forName("java.lang.String")

  private def get_method(name: String): String => String =
    name match {
      case Ext(class_name, method_name) =>
        val m =
          try { Class.forName(class_name).getMethod(method_name, STRING) }
          catch {
            case _: ClassNotFoundException | _: NoSuchMethodException =>
              error("No such method: " + quote(name))
          }
        if (!Modifier.isStatic(m.getModifiers)) error("Not at static method: " + m.toString)
        if (m.getReturnType != STRING) error("Bad method return type: " + m.toString)

        (arg: String) => {
          try { m.invoke(null, arg).asInstanceOf[String] }
          catch {
            case e: InvocationTargetException if e.getCause != null =>
              throw e.getCause
          }
        }
      case _ => error("Malformed method name: " + quote(name))
    }


  /* method invocation */

  object Tag extends Enumeration
  {
    val NULL = Value("0")
    val OK = Value("1")
    val ERROR = Value("2")
    val FAIL = Value("3")
  }

  def method(name: String, arg: String): (Tag.Value, String) =
    Exn.capture { get_method(name) } match {
      case Exn.Res(f) =>
        Exn.capture { f(arg) } match {
          case Exn.Res(null) => (Tag.NULL, "")
          case Exn.Res(res) => (Tag.OK, res)
          case Exn.Exn(ERROR(msg)) => (Tag.ERROR, msg)
          case Exn.Exn(e) => (Tag.ERROR, e.toString)
        }
      case Exn.Exn(ERROR(msg)) => (Tag.FAIL, msg)
      case Exn.Exn(e) => (Tag.FAIL, e.toString)
    }
}