src/Pure/System/invoke_scala.scala
author wenzelm
Sun Jul 10 23:46:05 2011 +0200 (2011-07-10)
changeset 43744 2c7e1565b4a3
child 43748 c70bd78ec83c
permissions -rw-r--r--
some support to invoke Scala methods under program control;
     1 /*  Title:      Pure/System/invoke_scala.scala
     2     Author:     Makarius
     3 
     4 Invoke static methods (String)String via reflection.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import java.lang.reflect.{Method, Modifier}
    11 
    12 
    13 object Invoke_Scala
    14 {
    15   private val STRING = Class.forName("java.lang.String")
    16 
    17   def method(class_name: String, method_name: String): String => String =
    18   {
    19     val m =
    20       try { Class.forName(class_name).getMethod(method_name, STRING) }
    21       catch {
    22         case _: ClassNotFoundException =>
    23           error("Class not found: " + quote(class_name))
    24         case _: NoSuchMethodException =>
    25           error("No such method: " + quote(class_name + "." + method_name))
    26       }
    27     if (!Modifier.isStatic(m.getModifiers)) error("Not at static method: " + m.toString)
    28     if (m.getReturnType != STRING) error("Bad return type of method: " + m.toString)
    29 
    30     (s: String) => m.invoke(null, s).asInstanceOf[String]
    31   }
    32 }