src/Pure/General/untyped.scala
author wenzelm
Wed, 06 Apr 2022 12:11:30 +0200
changeset 75409 5640c4db7d37
parent 75408 e859c9f30db2
permissions -rw-r--r--
more operations; tuned message: Class.toString already says "class ...";
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56905
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/untyped.scala
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
     3
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
     4
Untyped, unscoped, unchecked access to JVM objects.
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
     5
*/
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
     6
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
     7
package isabelle
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
     8
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
     9
72973
fc69884a6e5a clarified modules;
wenzelm
parents: 64370
diff changeset
    10
import java.lang.reflect.{Constructor, Method, Field}
59080
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    11
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    12
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73344
diff changeset
    13
object Untyped {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73344
diff changeset
    14
  def constructor[C](c: Class[C], arg_types: Class[_]*): Constructor[C] = {
72973
fc69884a6e5a clarified modules;
wenzelm
parents: 64370
diff changeset
    15
    val con = c.getDeclaredConstructor(arg_types: _*)
fc69884a6e5a clarified modules;
wenzelm
parents: 64370
diff changeset
    16
    con.setAccessible(true)
fc69884a6e5a clarified modules;
wenzelm
parents: 64370
diff changeset
    17
    con
fc69884a6e5a clarified modules;
wenzelm
parents: 64370
diff changeset
    18
  }
fc69884a6e5a clarified modules;
wenzelm
parents: 64370
diff changeset
    19
75409
5640c4db7d37 more operations;
wenzelm
parents: 75408
diff changeset
    20
  def the_constructor[C](c:  Class[C]): Constructor[C] = {
5640c4db7d37 more operations;
wenzelm
parents: 75408
diff changeset
    21
    c.getDeclaredConstructors().toList match {
5640c4db7d37 more operations;
wenzelm
parents: 75408
diff changeset
    22
      case List(con) =>
5640c4db7d37 more operations;
wenzelm
parents: 75408
diff changeset
    23
        con.setAccessible(true)
5640c4db7d37 more operations;
wenzelm
parents: 75408
diff changeset
    24
        con.asInstanceOf[Constructor[C]]
5640c4db7d37 more operations;
wenzelm
parents: 75408
diff changeset
    25
      case Nil => error("No constructor for " + c)
5640c4db7d37 more operations;
wenzelm
parents: 75408
diff changeset
    26
      case _ => error("Multiple constructors for " + c)
5640c4db7d37 more operations;
wenzelm
parents: 75408
diff changeset
    27
    }
5640c4db7d37 more operations;
wenzelm
parents: 75408
diff changeset
    28
  }
5640c4db7d37 more operations;
wenzelm
parents: 75408
diff changeset
    29
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73344
diff changeset
    30
  def method(c: Class[_], name: String, arg_types: Class[_]*): Method = {
59080
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    31
    val m = c.getDeclaredMethod(name, arg_types: _*)
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    32
    m.setAccessible(true)
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    33
    m
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    34
  }
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    35
75408
e859c9f30db2 clarified signature;
wenzelm
parents: 75393
diff changeset
    36
  def the_method(c: Class[_], name: String): Method = {
e859c9f30db2 clarified signature;
wenzelm
parents: 75393
diff changeset
    37
    c.getDeclaredMethods().iterator.filter(m => m.getName == name).toList match {
e859c9f30db2 clarified signature;
wenzelm
parents: 75393
diff changeset
    38
      case List(m) =>
e859c9f30db2 clarified signature;
wenzelm
parents: 75393
diff changeset
    39
        m.setAccessible(true)
e859c9f30db2 clarified signature;
wenzelm
parents: 75393
diff changeset
    40
        m
75409
5640c4db7d37 more operations;
wenzelm
parents: 75408
diff changeset
    41
      case Nil => error("No method " + quote(name) + " for " + c)
75408
e859c9f30db2 clarified signature;
wenzelm
parents: 75393
diff changeset
    42
      case _ => error("Multiple methods " + quote(name) + " for " + c)
e859c9f30db2 clarified signature;
wenzelm
parents: 75393
diff changeset
    43
    }
e859c9f30db2 clarified signature;
wenzelm
parents: 75393
diff changeset
    44
  }
e859c9f30db2 clarified signature;
wenzelm
parents: 75393
diff changeset
    45
e859c9f30db2 clarified signature;
wenzelm
parents: 75393
diff changeset
    46
  def classes(c0: Class[_ <: AnyRef]): Iterator[Class[_ <: AnyRef]] = new Iterator[Class[_ <: AnyRef]] {
e859c9f30db2 clarified signature;
wenzelm
parents: 75393
diff changeset
    47
    private var next_elem: Class[_ <: AnyRef] = c0
73337
0af9e7e4476f tuned --- fewer warnings;
wenzelm
parents: 72973
diff changeset
    48
    def hasNext: Boolean = next_elem != null
58682
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    49
    def next(): Class[_ <: AnyRef] = {
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    50
      val c = next_elem
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    51
      next_elem = c.getSuperclass.asInstanceOf[Class[_ <: AnyRef]]
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    52
      c
56905
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
    53
    }
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
    54
  }
58682
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    55
75408
e859c9f30db2 clarified signature;
wenzelm
parents: 75393
diff changeset
    56
  def class_field(c0: Class[_ <: AnyRef], x: String): Field = {
63419
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    57
    val iterator =
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    58
      for {
75408
e859c9f30db2 clarified signature;
wenzelm
parents: 75393
diff changeset
    59
        c <- classes(c0)
63419
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    60
        field <- c.getDeclaredFields.iterator
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    61
        if field.getName == x
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    62
      } yield {
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    63
        field.setAccessible(true)
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    64
        field
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    65
      }
73344
f5c147654661 tuned --- fewer warnings;
wenzelm
parents: 73337
diff changeset
    66
    if (iterator.hasNext) iterator.next()
75408
e859c9f30db2 clarified signature;
wenzelm
parents: 75393
diff changeset
    67
    else error("No field " + quote(x) + " for " + c0)
63419
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    68
  }
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    69
75409
5640c4db7d37 more operations;
wenzelm
parents: 75408
diff changeset
    70
  def get_static[A](c: Class[_ <: AnyRef], x: String): A =
5640c4db7d37 more operations;
wenzelm
parents: 75408
diff changeset
    71
    class_field(c, x).get(null).asInstanceOf[A]
5640c4db7d37 more operations;
wenzelm
parents: 75408
diff changeset
    72
75408
e859c9f30db2 clarified signature;
wenzelm
parents: 75393
diff changeset
    73
  def field(obj: AnyRef, x: String): Field = class_field(obj.getClass, x)
e859c9f30db2 clarified signature;
wenzelm
parents: 75393
diff changeset
    74
59079
12a689755c3d tuned signature -- more explicit types;
wenzelm
parents: 58682
diff changeset
    75
  def get[A](obj: AnyRef, x: String): A =
12a689755c3d tuned signature -- more explicit types;
wenzelm
parents: 58682
diff changeset
    76
    if (obj == null) null.asInstanceOf[A]
63419
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    77
    else field(obj, x).get(obj).asInstanceOf[A]
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    78
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    79
  def set[A](obj: AnyRef, x: String, y: A): Unit =
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    80
    field(obj, x).set(obj, y)
56905
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
    81
}