author | wenzelm |
Mon, 21 Dec 2020 21:53:12 +0100 | |
changeset 72973 | fc69884a6e5a |
parent 64370 | 865b39487b5d |
child 73337 | 0af9e7e4476f |
permissions | -rw-r--r-- |
56905 | 1 |
/* Title: Pure/General/untyped.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Untyped, unscoped, unchecked access to JVM objects. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
72973 | 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 | 13 |
object Untyped |
14 |
{ |
|
72973 | 15 |
def constructor[C](c: Class[C], arg_types: Class[_]*): Constructor[C] = |
16 |
{ |
|
17 |
val con = c.getDeclaredConstructor(arg_types: _*) |
|
18 |
con.setAccessible(true) |
|
19 |
con |
|
20 |
} |
|
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 | 29 |
def classes(obj: AnyRef): Iterator[Class[_ <: AnyRef]] = new Iterator[Class[_ <: AnyRef]] { |
30 |
private var next_elem: Class[_ <: AnyRef] = obj.getClass |
|
31 |
def hasNext(): Boolean = next_elem != null |
|
32 |
def next(): Class[_ <: AnyRef] = { |
|
33 |
val c = next_elem |
|
34 |
next_elem = c.getSuperclass.asInstanceOf[Class[_ <: AnyRef]] |
|
35 |
c |
|
56905 | 36 |
} |
37 |
} |
|
58682 | 38 |
|
63419 | 39 |
def field(obj: AnyRef, x: String): Field = |
40 |
{ |
|
41 |
val iterator = |
|
42 |
for { |
|
43 |
c <- classes(obj) |
|
44 |
field <- c.getDeclaredFields.iterator |
|
45 |
if field.getName == x |
|
46 |
} yield { |
|
47 |
field.setAccessible(true) |
|
48 |
field |
|
49 |
} |
|
50 |
if (iterator.hasNext) iterator.next |
|
51 |
else error("No field " + quote(x) + " for " + obj) |
|
52 |
} |
|
53 |
||
59079 | 54 |
def get[A](obj: AnyRef, x: String): A = |
55 |
if (obj == null) null.asInstanceOf[A] |
|
63419 | 56 |
else field(obj, x).get(obj).asInstanceOf[A] |
57 |
||
58 |
def set[A](obj: AnyRef, x: String, y: A): Unit = |
|
59 |
field(obj, x).set(obj, y) |
|
56905 | 60 |
} |