src/Pure/General/untyped.scala
author wenzelm
Mon, 21 Dec 2020 21:53:12 +0100
changeset 72973 fc69884a6e5a
parent 64370 865b39487b5d
child 73337 0af9e7e4476f
permissions -rw-r--r--
clarified modules;
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
56905
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
    13
object Untyped
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
    14
{
72973
fc69884a6e5a clarified modules;
wenzelm
parents: 64370
diff changeset
    15
  def constructor[C](c: Class[C], arg_types: Class[_]*): Constructor[C] =
fc69884a6e5a clarified modules;
wenzelm
parents: 64370
diff changeset
    16
  {
fc69884a6e5a clarified modules;
wenzelm
parents: 64370
diff changeset
    17
    val con = c.getDeclaredConstructor(arg_types: _*)
fc69884a6e5a clarified modules;
wenzelm
parents: 64370
diff changeset
    18
    con.setAccessible(true)
fc69884a6e5a clarified modules;
wenzelm
parents: 64370
diff changeset
    19
    con
fc69884a6e5a clarified modules;
wenzelm
parents: 64370
diff changeset
    20
  }
fc69884a6e5a clarified modules;
wenzelm
parents: 64370
diff changeset
    21
59080
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    22
  def method(c: Class[_], name: String, arg_types: Class[_]*): Method =
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    23
  {
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    24
    val m = c.getDeclaredMethod(name, arg_types: _*)
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    25
    m.setAccessible(true)
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    26
    m
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    27
  }
611914621edb added Untyped.method convenience (for *this* class only);
wenzelm
parents: 59079
diff changeset
    28
58682
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    29
  def classes(obj: AnyRef): Iterator[Class[_ <: AnyRef]] = new Iterator[Class[_ <: AnyRef]] {
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    30
    private var next_elem: Class[_ <: AnyRef] = obj.getClass
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    31
    def hasNext(): Boolean = next_elem != null
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    32
    def next(): Class[_ <: AnyRef] = {
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    33
      val c = next_elem
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    34
      next_elem = c.getSuperclass.asInstanceOf[Class[_ <: AnyRef]]
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    35
      c
56905
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
    36
    }
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
    37
  }
58682
542fa5093ebf access class hierarchy;
wenzelm
parents: 56905
diff changeset
    38
63419
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    39
  def field(obj: AnyRef, x: String): Field =
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    40
  {
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    41
    val iterator =
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    42
      for {
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    43
        c <- classes(obj)
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    44
        field <- c.getDeclaredFields.iterator
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    45
        if field.getName == x
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    46
      } yield {
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    47
        field.setAccessible(true)
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    48
        field
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    49
      }
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    50
    if (iterator.hasNext) iterator.next
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    51
    else error("No field " + quote(x) + " for " + obj)
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    52
  }
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    53
59079
12a689755c3d tuned signature -- more explicit types;
wenzelm
parents: 58682
diff changeset
    54
  def get[A](obj: AnyRef, x: String): A =
12a689755c3d tuned signature -- more explicit types;
wenzelm
parents: 58682
diff changeset
    55
    if (obj == null) null.asInstanceOf[A]
63419
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    56
    else field(obj, x).get(obj).asInstanceOf[A]
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    57
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    58
  def set[A](obj: AnyRef, x: String, y: A): Unit =
f473b6b16c63 more operations;
wenzelm
parents: 59080
diff changeset
    59
    field(obj, x).set(obj, y)
56905
fb38a767a78b untyped, unscoped, unchecked access to JVM objects;
wenzelm
parents:
diff changeset
    60
}