src/Pure/General/untyped.scala
author wenzelm
Fri, 01 Apr 2022 17:06:10 +0200
changeset 75393 87ebf5a50283
parent 73344 f5c147654661
child 75408 e859c9f30db2
permissions -rw-r--r--
clarified formatting, for the sake of scala3;
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
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73344
diff changeset
    20
  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
    21
    val m = c.getDeclaredMethod(name, arg_types: _*)
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    22
    m.setAccessible(true)
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    23
    m
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    24
  }
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    25
58682
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    26
  def classes(obj: AnyRef): Iterator[Class[_ <: AnyRef]] = new Iterator[Class[_ <: AnyRef]] {
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    27
    private var next_elem: Class[_ <: AnyRef] = obj.getClass
73337
0af9e7e4476f tuned --- fewer warnings;
wenzelm
parents: 72973
diff changeset
    28
    def hasNext: Boolean = next_elem != null
58682
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    29
    def next(): Class[_ <: AnyRef] = {
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    30
      val c = next_elem
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    31
      next_elem = c.getSuperclass.asInstanceOf[Class[_ <: AnyRef]]
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    32
      c
56905
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
    33
    }
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
    34
  }
58682
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    35
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73344
diff changeset
    36
  def field(obj: AnyRef, x: String): Field = {
63419
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    37
    val iterator =
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    38
      for {
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    39
        c <- classes(obj)
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    40
        field <- c.getDeclaredFields.iterator
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    41
        if field.getName == x
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    42
      } yield {
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    43
        field.setAccessible(true)
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    44
        field
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    45
      }
73344
f5c147654661 tuned --- fewer warnings;
wenzelm
parents: 73337
diff changeset
    46
    if (iterator.hasNext) iterator.next()
63419
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    47
    else error("No field " + quote(x) + " for " + obj)
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    48
  }
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    49
59079
12a689755c3d tuned signature -- more explicit types;
wenzelm
parents: 58682
diff changeset
    50
  def get[A](obj: AnyRef, x: String): A =
12a689755c3d tuned signature -- more explicit types;
wenzelm
parents: 58682
diff changeset
    51
    if (obj == null) null.asInstanceOf[A]
63419
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    52
    else field(obj, x).get(obj).asInstanceOf[A]
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    53
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    54
  def set[A](obj: AnyRef, x: String, y: A): Unit =
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    55
    field(obj, x).set(obj, y)
56905
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
    56
}