-
Notifications
You must be signed in to change notification settings - Fork 3
/
reflect.clj
98 lines (83 loc) · 2.74 KB
/
reflect.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
(ns geex.java.reflect
(:require [bluebell.utils.wip.pareto :as pareto]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Inteface
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def boxed-primitive-classes
[[Character Character/TYPE]
[Boolean Boolean/TYPE]
[Byte Byte/TYPE]
[Short Short/TYPE]
[Integer Integer/TYPE]
[Long Long/TYPE]
[Float Float/TYPE]
[Double Double/TYPE]])
(defn boxes-or-unboxes-to? [a b]
(some
(fn [[cl ucl]]
(or (and (= cl a) (= ucl b))
(and (= ucl a) (= cl b))))
boxed-primitive-classes))
(defn arg-matches? [a b]
(or (isa? a b)
(boxes-or-unboxes-to? a b)))
(defn arglist-matches? [query-arglist available-arglist]
{:pre [(coll? query-arglist)
(coll? available-arglist)]}
(and (= (count query-arglist)
(count available-arglist))
(every?
identity
(map arg-matches? query-arglist available-arglist))))
(defn matching-method? [method-name arg-types method]
(and (= method-name (.getName method))
(let [ptypes (vec (.getParameterTypes method))]
(arglist-matches? arg-types ptypes))))
(defn list-matching-methods [cl method-name arg-types]
{:pre [(class? cl)
(string? method-name)
(coll? arg-types)
(every? class? arg-types)]}
(filter
(partial matching-method? method-name arg-types)
(.getMethods cl)))
(defn dominates-score [a b]
(if (arg-matches? a b)
(if (arg-matches? b a)
0
1)
(if (arg-matches? b a)
-1
nil)))
(defn method-dominates? [a b]
(let [a-args (.getParameterTypes a)
b-args (.getParameterTypes b)]
(assert (= (count a-args)
(count b-args)))
(let [scores (mapv dominates-score a-args b-args)]
(and (not (some nil? scores)) ;; no unclear relations
(some (partial = 1) scores) ;; some args that dominate
(not (some (partial = -1) scores)))))) ;; no args being dominated.
(defn get-matching-method [cl method-name arg-types]
(let [frontier
(pareto/elements
(transduce
(filter
(partial matching-method? method-name arg-types))
(completing pareto/insert)
(pareto/frontier method-dominates?)
(.getMethods cl)))
n (count frontier)]
(if (or (= 0 n)
(<= 2 n))
(throw (ex-info
(if (= 0 n)
"No matching method found. Did you consider specifying the return type using :ret? Only then can the calling code know what to expect."
"Ambiguous method resolution")
{:class cl
:method-name method-name
:arg-types arg-types
:methods frontier}))
(first frontier))))