source: tags/1.0.0/abcl/contrib/jss/invoke.lisp

Last change on this file was 13430, checked in by Mark Evenson, 13 years ago

Refactor ASDF extensions from JSS into ABCL-ASDF.

The JAR-FILE, JAR-DIRECTORY, and CLASS-FILE-DIRECTORY ASDF extensions
are now part of the ABCL-ASDF contrib as we aim to centralize all such
things in one place. *ADDED-TO-CLASSPATH* is now part of the
ABCL-ASDF package as well.

There is currently a (mostly) recursive relationship between JSS and
ABCL-ASDF, as each (mostly) requires the other for operation.
JSS:ENSURE-COMPATIBILITY will ensure that JSS continues to understand
the refactored extensions.

File size: 24.5 KB
Line 
1;; invoke.lisp v2.0
2;;
3;; Copyright (C) 2005 Alan Ruttenberg
4;; Copyright (C) 2011 Mark Evenson
5;;
6;; Since most of this code is derivative of the Jscheme System, it is
7;; licensed under the same terms, namely:
8
9;; This software is provided 'as-is', without any express or
10;; implied warranty.
11
12;; In no event will the author be held liable for any damages
13;; arising from the use of this software.
14
15;; Permission is granted to anyone to use this software for any
16;; purpose, including commercial applications, and to alter it
17;; and redistribute it freely, subject to the following
18;; restrictions:
19
20;; 1. The origin of this software must not be misrepresented; you
21;;    must not claim that you wrote the original software. If you
22;;    use this software in a product, an acknowledgment in the
23;;    product documentation would be appreciated but is not
24;;    required.
25
26;; 2. Altered source versions must be plainly marked as such, and
27;;    must not be misrepresented as being the original software.
28
29;; 3. This notice may not be removed or altered from any source
30;;    distribution.
31
32
33;; The dynamic dispatch of the java.lang.reflect package is used to
34;; make it real easy, if perhaps less efficient, to write Java code
35;; since you don't need to be bothered with imports, or with figuring
36;; out which method to call.  The only time that you need to know a
37;; class name is when you want to call a static method, or a
38;; constructor, and in those cases, you only need to know enough of
39;; the class name that is unique wrt to the classes on your classpath.
40;;
41;; Java methods look like this: #"toString". Java classes are
42;; represented as symbols, which are resolved to the appropriate java
43;; class name. When ambiguous, you need to be more specific. A simple example:
44
45;; (let ((sw (new 'StringWriter)))
46;;   (#"write" sw "Hello ")
47;;   (#"write" sw "World")
48;;   (print (#"toString" sw)))
49
50;; What's happened here? First, all the classes in all the jars in the
51;; classpath have been collected.  For each class a.b.C.d, we have
52;; recorded that b.c.d, b.C.d, C.d, c.d, and d potentially refer to
53;; this class. In your call to new, as long as the symbol can refer to
54;; only one class, we use that class. In this case, it is
55;; java.io.StringWriter. You could also have written (new
56;; 'io.stringwriter), (new '|io.StringWriter|), (new
57;; 'java.io.StringWriter)...
58
59;; the call (#"write" sw "Hello "), uses the code in invoke.java to
60;; call the method named "write" with the arguments sw and "Hello ".
61;; JSS figures out the right java method to call, and calls it.
62
63;; If you want to do a raw java call, use #0"toString". Raw calls
64;; return their results as Java objects, avoiding doing the usual Java
65;; object to Lisp object conversions that ABCL does.
66
67;; (with-constant-signature ((name jname raw?)*) &body body)
68;; binds a macro which expands to a jcall, promising that the same method
69;; will be called every time. Use this if you are making a lot of calls and
70;; want to avoid the overhead of a the dynamic dispatch.
71;; e.g. (with-constant-signature ((tostring "toString"))
72;;        (time (dotimes (i 10000) (tostring "foo"))))
73;; runs about 3x faster than (time (dotimes (i 10000) (#"toString" "foo")))
74;;
75;; (with-constant-signature ((tostring "toString" t)) ...) will cause the
76;; toString to be a raw java call. see get-all-jar-classnames below for an example.
77;;
78;; Implementation is that the first time the function is called, the
79;; method is looked up based on the arguments passed, and thereafter
80;; that method is called directly.  Doesn't work for static methods at
81;; the moment (lazy)
82;;
83;; (japropos string) finds all class names matching string
84;; (jcmn class-name) lists the names of all methods for the class
85;;
86;; TODO
87;;   - Make with-constant-signature work for static methods too.
88;;   - #2"toString" to work like function scoped (with-constant-signature ((tostring "toString")) ...)
89;;   - #3"toString" to work like runtime scoped (with-constant-signature ((tostring "toString")) ...)
90;;      (both probably need compiler support to work)
91;;   - Maybe get rid of second " in reader macro. #"toString looks nicer, but might
92;;     confuse lisp mode.
93;;   - write jmap, analogous to map, but can take java collections, java arrays etc.
94;;   - write loop clauses for java collections.
95;;   - Register classes in .class files below classpath directories (when :wild-inferiors works)
96;;   - Make documentation like Edi Weitz
97;;
98;; Thanks: Peter Graves, Jscheme developers, Mike Travers for skij, 
99;; Andras Simon for jfli-abcl which bootstrapped me and taught me how to do
100;; get-all-jar-classnames
101;;
102
103;; changelog
104
105;; Sat January 28, 2006, alanr:
106
107;; Change imports strategy. Only index by last part of class name,
108;; case insensitive. Make the lookup-class-name logic be a bit more
109;; complicated. This substantially reduces the time it takes to do the
110;; auto imports and since class name lookup is relatively infrequent,
111;; and in any case cached, this doesn't effect run time speed.  (did
112;; try caching, but didn't pay - more time was spent reading and
113;; populating large hash table)
114;;
115;; Split class path by ";" in addition to ":" for windows.
116;;
117;; Tested on windows, linux.
118
119;; 2011-05-21 Mark Evenson
120;;   "ported" to native ABCL without needing the jscheme.jar or bsh-2.0b4.jar
121
122(in-package :jss)
123
124(eval-when (:compile-toplevel :load-toplevel :execute)
125  (defvar *do-auto-imports* t))
126
127(defvar *imports-resolved-classes* (make-hash-table :test 'equal))
128
129(defun find-java-class (name)
130  (jclass (maybe-resolve-class-against-imports name)))
131
132(defmacro invoke-add-imports (&rest imports)
133  "Push these imports onto the search path. If multiple, earlier in list take precedence"
134  `(eval-when (:compile-toplevel :load-toplevel :execute)
135     (clrhash *imports-resolved-classes*)
136     (dolist (i (reverse ',imports))
137       (setq *imports-resolved-classes* (delete i *imports-resolved-classes* :test 'equal))
138       )))
139
140(defun clear-invoke-imports ()
141  (clrhash *imports-resolved-classes*))
142
143(defun maybe-resolve-class-against-imports (classname)
144  (or (gethash classname *imports-resolved-classes*)
145      (let ((found (lookup-class-name classname)))
146  (if found
147      (progn 
148        (setf (gethash classname *imports-resolved-classes*) found)
149        found)
150      (string classname)))))
151
152(defvar *class-name-to-full-case-insensitive* (make-hash-table :test 'equalp))
153
154;; This is the function that calls invoke to call your java
155;; method. The first argument is the method name or 'new. The second
156;; is the object you are calling it on, followed by the rest of the
157;; arguments. If the "object" is a symbol, then that symbol is assumed
158;; to be a java class, and a static method on the class is called,
159;; otherwise a regular method is called.
160
161(defun invoke (method object &rest args)
162    (invoke-restargs method object args))
163
164(defun invoke-restargs (method object args &optional (raw? nil))
165  (let* ((object-as-class-name 
166          (if (symbolp object) (maybe-resolve-class-against-imports object)))
167         (object-as-class 
168          (if object-as-class-name (find-java-class object-as-class-name))))
169    (if (eq method 'new)
170        (apply #'jnew (or object-as-class-name object) args)
171        (if raw?
172            (if (symbolp object)
173                (apply #'jstatic-raw method object-as-class  args)
174                (apply #'jcall-raw method object  args))
175            (if (symbolp object)
176                (apply #'jstatic method object-as-class args)
177                (apply #'jcall method object args))))))
178
179;;; Method name as String --> String  | Symbol --> jmethod
180(defvar *methods-cache* (make-hash-table :test #'equal))
181
182(defun get-jmethod (method object) 
183  (when (gethash method *methods-cache*)
184    (gethash 
185     (if (symbolp object) (lookup-class-name object) (jobject-class object))
186     (gethash method *methods-cache*))))
187
188(defun set-jmethod (method object jmethod) 
189  (unless (gethash method *methods-cache*)
190    (setf (gethash method *methods-cache*) (make-hash-table :test #'equal)))
191  (setf 
192   (gethash 
193    (if (symbolp object) (lookup-class-name object) (jobject-class object))
194    (gethash method *methods-cache*))
195   jmethod))
196
197(defconstant +set-accessible+ 
198  (jmethod "java.lang.reflect.AccessibleObject" "setAccessible" "boolean"))
199
200;;; TODO optimize me!
201(defun invoke-find-method (method object args)
202  (let ((jmethod (get-jmethod method object)))
203    (unless jmethod
204      (setf jmethod 
205            (if (symbolp object)
206                ;;; static method
207                (apply #'jmethod (lookup-class-name object) 
208                       method (mapcar #'jobject-class args))
209                  ;;; instance method
210                (apply #'jresolve-method 
211                       method object args)))
212      (jcall +set-accessible+ jmethod +true+)
213      (set-jmethod method object jmethod))
214    jmethod))
215
216;; This is the reader macro for java methods. it translates the method
217;; into a lambda form that calls invoke. Which is nice because you
218;; can, e.g. do this: (mapcar #"toString" list-of-java-objects). The reader
219;; macro takes one arg. If 0, then jstatic-raw is called, so that abcl doesn't
220;; automagically convert the returned java object into a lisp object. So
221;; #0"toString" returns a java.lang.String object, where as #"toString" returns
222;; a regular Lisp string as ABCL converts the Java string to a Lisp string.
223
224(eval-when (:compile-toplevel :load-toplevel :execute)
225  (defun read-invoke (stream char arg) 
226    (unread-char char stream)
227    (let ((name (read stream)))
228      (let ((object-var (gensym))
229            (args-var (gensym)))
230        `(lambda (,object-var &rest ,args-var) 
231           (invoke-restargs ,name  ,object-var ,args-var ,(eql arg 0))))))
232  (set-dispatch-macro-character #\# #\" 'read-invoke))
233
234(defmacro with-constant-signature (fname-jname-pairs &body body)
235  (if (null fname-jname-pairs)
236      `(progn ,@body)
237      (destructuring-bind ((fname jname &optional raw) &rest ignore) fname-jname-pairs
238  (declare (ignore ignore))
239  (let ((varname (gensym)))
240    `(let ((,varname nil))
241       (macrolet ((,fname (&rest args)
242        `(if ,',varname
243             (if ,',raw
244           (jcall-raw ,',varname ,@args)
245           (jcall ,',varname ,@args))
246             (progn
247         (setq ,',varname (invoke-find-method ,',jname ,(car args) (list ,@(rest args))))
248         (if ,',raw
249             (jcall-raw ,',varname ,@args)
250             (jcall ,',varname ,@args))))))
251         (with-constant-signature ,(cdr fname-jname-pairs)
252     ,@body)))))))
253
254(defun lookup-class-name (name)
255  (setq name (string name))
256  (let* (;; cant (last-name-pattern (#"compile" '|java.util.regex.Pattern| ".*?([^.]*)$"))
257   ;; reason: bootstrap - the class name would have to be looked up...
258   (last-name-pattern (load-time-value (jstatic (jmethod "java.util.regex.Pattern" "compile"
259                     (jclass "java.lang.String"))
260                  (jclass "java.util.regex.Pattern") 
261                  ".*?([^.]*)$")))
262
263   (last-name 
264    (let ((matcher (#0"matcher" last-name-pattern name)))
265      (#"matches" matcher)
266      (#"group" matcher 1))))
267    (let* ((bucket (gethash last-name *class-name-to-full-case-insensitive*))
268     (bucket-length (length bucket)))
269      (or (find name bucket :test 'equalp)
270    (flet ((matches-end (end full test)
271       (= (+ (or (search end full :from-end t :test test) -10)
272       (length end))
273          (length full)))
274     (ambiguous (choices)
275       (error "Ambiguous class name: ~a can be ~{~a~^, ~}" name choices)))
276      (if (zerop bucket-length)
277    name
278    (let ((matches (loop for el in bucket when (matches-end name el 'char=) collect el)))
279      (if (= (length matches) 1)
280          (car matches)
281          (if (= (length matches) 0)
282        (let ((matches (loop for el in bucket when (matches-end name el 'char-equal) collect el)))
283          (if (= (length matches) 1)
284        (car matches)
285        (if (= (length matches) 0)
286            name
287            (ambiguous matches))))
288        (ambiguous matches))))))))))
289
290(defun get-all-jar-classnames (jar-file-name)
291  (let* ((jar (jnew (jconstructor "java.util.jar.JarFile" (jclass "java.lang.String")) (namestring (truename jar-file-name))))
292         (entries (#"entries" jar)))
293    (with-constant-signature ((matcher "matcher" t) (substring "substring")
294            (jreplace "replace" t) (jlength "length")
295            (matches "matches") (getname "getName" t)
296            (next "nextElement" t) (hasmore "hasMoreElements")
297            (group "group"))
298      (loop while (hasmore entries)
299   for name =  (getname (next entries))
300   with class-pattern = (#"compile" '|java.util.regex.Pattern| "[^$]*\\.class$")
301   with name-pattern = (#"compile" '|java.util.regex.Pattern| ".*?([^.]*)$")
302   when (matches (matcher class-pattern name))
303   collect
304     (let* ((fullname (substring (jreplace name #\/ #\.) 0 (- (jlength name) 6)))
305      (matcher (matcher name-pattern fullname))
306      (name (progn (matches matcher) (group matcher 1))))
307       (cons name fullname))
308   ))))
309
310(defun jar-import (file)
311  (when (probe-file file)
312    (loop for (name . full-class-name) in (get-all-jar-classnames file)
313       do 
314   (pushnew full-class-name (gethash name *class-name-to-full-case-insensitive*) 
315      :test 'equal))))
316
317(defun new (class-name &rest args)
318  (invoke-restargs 'new class-name args))
319
320(defvar *running-in-osgi* (ignore-errors (jclass "org.osgi.framework.BundleActivator")))
321
322
323(defun get-java-field (object field &optional (try-harder *running-in-osgi*))
324  (if try-harder
325      (let* ((class (if (symbolp object)
326      (setq object (find-java-class object))
327                        (if (equal "java.lang.Class" (jclass-name (jobject-class object)))
328                            object
329                            (jobject-class object))))
330       (jfield (if (java-object-p field)
331       field
332                         (find field (#"getDeclaredFields" class) 
333                               :key 'jfield-name :test 'equal))))
334  (#"setAccessible" jfield t)
335  (values (#"get" jfield object) jfield))
336      (if (symbolp object)
337          (let ((class (find-java-class object)))
338            (jfield class field))
339          (jfield field object))))
340
341;; use #"getSuperclass" and #"getInterfaces" to see whether there are fields in superclasses that we might set
342(defun set-java-field (object field value &optional (try-harder *running-in-osgi*))
343  (if try-harder
344      (let* ((class (if (symbolp object)
345      (setq object (find-java-class object))
346          (if (equal "java.lang.Class" (jclass-name (jobject-class object)) )
347        object
348      (jobject-class object))))
349       (jfield (if (java-object-p field)
350       field
351           (find field (#"getDeclaredFields" class) :key 'jfield-name :test 'equal))))
352  (#"setAccessible" jfield t)
353  (values (#"set" jfield object value) jfield))
354    (if (symbolp object)
355  (let ((class (find-java-class object)))
356    (#"pokeStatic" 'invoke class field value))
357      (#"poke" 'invoke object field value))))
358
359(defconstant +for-name+ 
360  (jmethod "java.lang.Class" "forName" "java.lang.String" "boolean" "java.lang.ClassLoader"))
361
362(defun find-java-class (name)
363  (or (jstatic +for-name+ "java.lang.Class" 
364               (maybe-resolve-class-against-imports name) +true+ java::*classloader*)
365      (ignore-errors (jclass (maybe-resolve-class-against-imports name)))))
366
367(defmethod print-object ((obj (jclass "java.lang.Class")) stream) 
368  (print-unreadable-object (obj stream :identity nil)
369    (format stream "java class ~a" (jclass-name obj))))
370
371(defmethod print-object ((obj (jclass "java.lang.reflect.Method")) stream) 
372  (print-unreadable-object (obj stream :identity nil)
373    (format stream "method ~a" (#"toString" obj))))
374
375(defun do-auto-imports ()
376  (flet ((import-class-path (cp)
377     (map nil
378    (lambda(s) 
379      (setq s (jcall "toString" s))
380      (when *load-verbose*
381        (format t ";Importing ~a~%" s))
382      (cond 
383        ((file-directory-p s) )
384        ((equal (pathname-type s) "jar")
385         (jar-import (merge-pathnames (jcall "toString" s) 
386                                                  (format nil "~a/" (jstatic "getProperty" "java.lang.System" "user.dir")))))))
387    (jcall "split" cp 
388                       (string (jfield (jclass "java.io.File") "pathSeparatorChar"))))))
389    (import-class-path (jcall "getClassPath" (jstatic "getRuntimeMXBean" '|java.lang.management.ManagementFactory|)))
390    (import-class-path (jcall "getBootClassPath" (jstatic "getRuntimeMXBean" '|java.lang.management.ManagementFactory|)))
391    ))
392
393(eval-when (:load-toplevel :execute)
394  (when *do-auto-imports* 
395    (do-auto-imports)))
396
397(defun japropos (string)
398  (setq string (string string))
399  (let ((matches nil))
400    (maphash (lambda(key value) 
401         (declare (ignore key))
402         (loop for class in value
403      when (search string class :test 'string-equal)
404        do (pushnew (list class "Java Class") matches :test 'equal)))
405       *class-name-to-full-case-insensitive*)
406    (loop for (match type) in (sort matches 'string-lessp :key 'car)
407   do (format t "~a: ~a~%" match type))
408    ))
409
410(defun jclass-method-names (class &optional full)
411  (if (java-object-p class)
412      (if (equal (jclass-name (jobject-class class)) "java.lang.Class")
413    (setq class (jclass-name class))
414    (setq class (jclass-name (jobject-class class)))))
415  (union
416   (remove-duplicates (map 'list (if full #"toString" 'jmethod-name) (#"getMethods" (find-java-class class))) :test 'equal)
417   (ignore-errors (remove-duplicates (map 'list (if full #"toString" 'jmethod-name) (#"getConstructors" (find-java-class class))) :test 'equal))))
418
419(defun jcmn (class &optional full)
420  (if full 
421      (dolist (method (jclass-method-names class t))
422  (format t "~a~%" method))
423      (jclass-method-names class)))
424
425(defun path-to-class (classname)
426  (let ((full (lookup-class-name classname)))
427    (#"toString" 
428     (#"getResource" 
429      (find-java-class full)
430      (concatenate 'string "/" (substitute #\/ #\. full) ".class")))))
431
432;; http://www.javaworld.com/javaworld/javaqa/2003-07/02-qa-0725-classsrc2.html
433
434(defun all-loaded-classes ()
435  (let ((classes-field 
436   (find "classes" (#"getDeclaredFields" (jclass "java.lang.ClassLoader"))
437         :key #"getName" :test 'equal)))
438    (#"setAccessible" classes-field t)
439    (loop for classloader in (mapcar #'first (dump-classpath))
440   append
441   (loop with classesv = (#"get" classes-field classloader)
442      for i below (#"size" classesv)
443      collect (#"getName" (#"elementAt" classesv i)))
444   append
445   (loop with classesv = (#"get" classes-field (#"getParent" classloader))
446      for i below (#"size" classesv)
447      collect (#"getName" (#"elementAt" classesv i))))))
448   
449(defun get-dynamic-class-path ()
450  (rest 
451   (find-if (lambda (loader) 
452              (string= "org.armedbear.lisp.JavaClassLoader"
453                       (jclass-name (jobject-class loader))))
454            (dump-classpath)
455            :key #'car)))
456
457(defun java-gc ()
458  (#"gc" (#"getRuntime" 'java.lang.runtime))
459  (#"runFinalization" (#"getRuntime" 'java.lang.runtime))
460  (#"gc" (#"getRuntime" 'java.lang.runtime))
461  (java-room))
462
463(defun java-room ()
464  (let ((rt (#"getRuntime" 'java.lang.runtime)))
465    (values (- (#"totalMemory" rt) (#"freeMemory" rt))
466     (#"totalMemory" rt)
467     (#"freeMemory" rt)
468     (list :used :total :free))))
469
470(defun verbose-gc (&optional (new-value nil new-value-supplied))
471  (if new-value-supplied
472      (progn (#"setVerbose" (#"getMemoryMXBean"  'java.lang.management.ManagementFactory) new-value) new-value)
473      (#"isVerbose" (#"getMemoryMXBean"  'java.lang.management.ManagementFactory))))
474
475(defun all-jars-below (directory) 
476  (loop with q = (system:list-directory directory) 
477     while q for top = (pop q)
478     if (null (pathname-name top)) do (setq q (append q (all-jars-below top))) 
479     if (equal (pathname-type top) "jar") collect top))
480
481(defun all-classfiles-below (directory) 
482  (loop with q = (system:list-directory directory) 
483     while q for top = (pop q)
484     if (null (pathname-name top)) do (setq q (append q (all-classfiles-below top ))) 
485     if (equal (pathname-type top) "class")
486     collect top
487     ))
488
489(defun all-classes-below-directory (directory)
490  (loop for file in (all-classfiles-below directory) collect
491       (format nil "~{~a.~}~a"
492         (subseq (pathname-directory file) (length (pathname-directory directory)))
493         (pathname-name file))
494       ))
495
496(defun classfiles-import (directory)
497  (setq directory (truename directory))
498  (loop for full-class-name in (all-classes-below-directory directory)
499       for name = (#"replaceAll" full-class-name "^.*\\." "")
500     do
501       (pushnew full-class-name (gethash name *class-name-to-full-case-insensitive*) 
502    :test 'equal)))
503
504(defun set-to-list (set)
505  (declare (optimize (speed 3) (safety 0)))
506  (with-constant-signature ((iterator "iterator" t) (hasnext "hasNext") (next "next"))
507    (loop with iterator = (iterator set)
508       while (hasNext iterator)
509       for item = (next iterator)
510       collect item)))
511
512(defun jlist-to-list (list)
513  "Convert a LIST implementing java.util.List to a Lisp list."
514  (declare (optimize (speed 3) (safety 0)))
515  (loop :for i :from 0 :below (jcall "size" list)
516     :collecting (jcall "get" list i)))
517
518(defun jarray-to-list (jarray)
519  (declare (optimize (speed 3) (safety 0)))
520  (jlist-to-list
521   (jstatic "asList" "java.util.Arrays" jarray)))
522
523;;; Deprecated
524;;;
525;;; XXX unclear what sort of list this would actually work on, as it
526;;; certainly doesn't seem to be any of the Java collection types
527;;; (what implements getNext())?
528(defun list-to-list (list)
529  (declare (optimize (speed 3) (safety 0)))
530  (with-constant-signature ((isEmpty "isEmpty") (getfirst "getFirst")
531                            (getNext "getNext"))
532    (loop until (isEmpty list)
533       collect (getFirst list)
534       do (setq list (getNext list)))))
535
536;; Contribution of Luke Hope. (Thanks!)
537
538(defun iterable-to-list (iterable)
539 (declare (optimize (speed 3) (safety 0)))
540 (let ((it (#"iterator" iterable)))
541   (with-constant-signature ((hasmore "hasMoreElements")
542           (next "nextElement"))
543     (loop while (hasmore it)
544  collect (next it)))))
545
546(defun vector-to-list (vector)
547 (declare (optimize (speed 3) (safety 0)))
548 (with-constant-signature ((hasmore "hasMoreElements")
549         (next "nextElement"))
550     (loop while (hasmore vector)
551  collect (next vector))))
552
553(defun hashmap-to-hashtable (hashmap &rest rest &key (keyfun #'identity) (valfun #'identity) (invert? nil)
554            table 
555             &allow-other-keys )
556  (let ((keyset (#"keySet" hashmap))
557  (table (or table (apply 'make-hash-table
558        (loop for (key value) on rest by #'cddr
559           unless (member key '(:invert? :valfun :keyfun :table)) 
560           collect key and collect value)))))
561    (with-constant-signature ((iterator "iterator" t) (hasnext "hasNext") (next "next"))
562      (loop with iterator = (iterator keyset)
563   while (hasNext iterator)
564   for item = (next iterator)
565   do (if invert?
566    (setf (gethash (funcall valfun (#"get" hashmap item)) table) (funcall keyfun item))
567    (setf (gethash (funcall keyfun item) table) (funcall valfun (#"get" hashmap item)))))
568    table)))
569     
570(defun jclass-all-interfaces (class)
571  "Return a list of interfaces the class implements"
572  (unless (java-object-p class)
573    (setq class (find-java-class class)))
574  (loop for aclass = class then (#"getSuperclass" aclass)
575     while aclass
576     append (coerce (#"getInterfaces" aclass) 'list)))
577
578(defun safely (f name)
579  (let ((fname (gensym)))
580    (compile fname
581       `(lambda(&rest args)
582    (with-simple-restart (top-level
583              "Return from lisp method implementation for ~a." ,name)
584      (apply ,f args))))
585    (symbol-function fname)))
586
587(defun jdelegating-interface-implementation (interface dispatch-to &rest method-names-and-defs)
588  "Creates and returns an implementation of a Java interface with
589   methods calling Lisp closures as given in METHOD-NAMES-AND-DEFS.
590
591   INTERFACE is an interface
592
593   DISPATCH-TO is an existing Java object
594
595   METHOD-NAMES-AND-DEFS is an alternating list of method names
596   (strings) and method definitions (closures).
597
598   For missing methods, a dummy implementation is provided that
599   calls the method on DISPATCH-TO."
600  (let ((implemented-methods
601         (loop for m in method-names-and-defs
602      for i from 0
603      if (evenp i) 
604      do (assert (stringp m) (m) "Method names must be strings: ~s" m) and collect m
605      else
606      do (assert (or (symbolp m) (functionp m)) (m) "Methods must be function designators: ~s" m))))
607    (let ((safe-method-names-and-defs 
608     (loop for (name function) on method-names-and-defs by #'cddr
609        collect name collect (safely function name))))
610      (loop for method across
611     (jclass-methods interface :declared nil :public t)
612     for method-name = (jmethod-name method)
613     when (not (member method-name implemented-methods :test #'string=))
614     do
615     (let* ((def  `(lambda
616           (&rest args)
617         (invoke-restargs ,(jmethod-name method) ,dispatch-to args t)
618         )))
619       (push (coerce def 'function) safe-method-names-and-defs)
620       (push method-name safe-method-names-and-defs)))
621      (apply #'java::%jnew-proxy  interface safe-method-names-and-defs))))
622
623
Note: See TracBrowser for help on using the repository browser.