src/Pure/System/invoke_scala.scala
changeset 43744 2c7e1565b4a3
child 43748 c70bd78ec83c
equal deleted inserted replaced
43743:8786e36b8142 43744:2c7e1565b4a3
       
     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 }