some support to invoke Scala methods under program control;
authorwenzelm
Sun Jul 10 23:46:05 2011 +0200 (2011-07-10)
changeset 437442c7e1565b4a3
parent 43743 8786e36b8142
child 43745 562e35bc351e
some support to invoke Scala methods under program control;
src/Pure/System/invoke_scala.scala
src/Pure/build-jars
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/System/invoke_scala.scala	Sun Jul 10 23:46:05 2011 +0200
     1.3 @@ -0,0 +1,32 @@
     1.4 +/*  Title:      Pure/System/invoke_scala.scala
     1.5 +    Author:     Makarius
     1.6 +
     1.7 +Invoke static methods (String)String via reflection.
     1.8 +*/
     1.9 +
    1.10 +package isabelle
    1.11 +
    1.12 +
    1.13 +import java.lang.reflect.{Method, Modifier}
    1.14 +
    1.15 +
    1.16 +object Invoke_Scala
    1.17 +{
    1.18 +  private val STRING = Class.forName("java.lang.String")
    1.19 +
    1.20 +  def method(class_name: String, method_name: String): String => String =
    1.21 +  {
    1.22 +    val m =
    1.23 +      try { Class.forName(class_name).getMethod(method_name, STRING) }
    1.24 +      catch {
    1.25 +        case _: ClassNotFoundException =>
    1.26 +          error("Class not found: " + quote(class_name))
    1.27 +        case _: NoSuchMethodException =>
    1.28 +          error("No such method: " + quote(class_name + "." + method_name))
    1.29 +      }
    1.30 +    if (!Modifier.isStatic(m.getModifiers)) error("Not at static method: " + m.toString)
    1.31 +    if (m.getReturnType != STRING) error("Bad return type of method: " + m.toString)
    1.32 +
    1.33 +    (s: String) => m.invoke(null, s).asInstanceOf[String]
    1.34 +  }
    1.35 +}
     2.1 --- a/src/Pure/build-jars	Sun Jul 10 21:46:41 2011 +0200
     2.2 +++ b/src/Pure/build-jars	Sun Jul 10 23:46:05 2011 +0200
     2.3 @@ -40,6 +40,7 @@
     2.4    System/download.scala
     2.5    System/event_bus.scala
     2.6    System/gui_setup.scala
     2.7 +  System/invoke_scala.scala
     2.8    System/isabelle_charset.scala
     2.9    System/isabelle_process.scala
    2.10    System/isabelle_syntax.scala