src/Pure/System/invoke_scala.scala
changeset 43744 2c7e1565b4a3
child 43748 c70bd78ec83c
     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 +}