source: trunk/abcl/src/org/armedbear/lisp/java.lisp @ 11528

Last change on this file since 11528 was 11528, checked in by Mark Evenson, 14 years ago

Enable optional use of JFluid profiler.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.7 KB
Line 
1;;; java.lisp
2;;;
3;;; Copyright (C) 2003-2007 Peter Graves, Andras Simon
4;;; $Id: java.lisp 11528 2009-01-03 13:08:56Z mevenson $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19;;;
20;;; As a special exception, the copyright holders of this library give you
21;;; permission to link this library with independent modules to produce an
22;;; executable, regardless of the license terms of these independent
23;;; modules, and to copy and distribute the resulting executable under
24;;; terms of your choice, provided that you also meet, for each linked
25;;; independent module, the terms and conditions of the license of that
26;;; module.  An independent module is a module which is not derived from
27;;; or based on this library.  If you modify this library, you may extend
28;;; this exception to your version of the library, but you are not
29;;; obligated to do so.  If you do not wish to do so, delete this
30;;; exception statement from your version.
31
32(in-package "JAVA")
33
34(require "CLOS")
35
36(defun jregister-handler (object event handler &key data count)
37  (%jregister-handler object event handler data count))
38
39(defun jinterface-implementation (interface &rest method-names-and-defs)
40  "Creates and returns an implementation of a Java interface with
41   methods calling Lisp closures as given in METHOD-NAMES-AND-DEFS.
42
43   INTERFACE is either a Java interface or a string naming one.
44
45   METHOD-NAMES-AND-DEFS is an alternating list of method names
46   (strings) and method definitions (closures).
47
48   For missing methods, a dummy implementation is provided that
49   returns nothing or null depending on whether the return type is
50   void or not. This is for convenience only, and a warning is issued
51   for each undefined method."
52  (let ((interface (jclass interface))
53        (implemented-methods
54         (loop for m in method-names-and-defs
55           for i from 0
56           if (evenp i)
57           do (assert (stringp m) (m) "Method names must be strings: ~s" m) and collect m
58           else
59           do (assert (or (symbolp m) (functionp m)) (m) "Methods must be function designators: ~s" m)))
60        (null (make-immediate-object nil :ref)))
61    (loop for method across
62      (jclass-methods interface :declared nil :public t)
63      for method-name = (jmethod-name method)
64      when (not (member method-name implemented-methods :test #'string=))
65      do
66      (let* ((void-p (string= (jclass-name (jmethod-return-type method)) "void"))
67             (arglist (when (plusp (length (jmethod-params method))) '(&rest ignore)))
68             (def `(lambda
69                     ,arglist
70                     ,(when arglist '(declare (ignore ignore)))
71                     ,(if void-p '(values) null))))
72        (warn "Implementing dummy method ~a for interface ~a"
73              method-name (jclass-name interface))
74        (push (coerce def 'function) method-names-and-defs)
75        (push method-name method-names-and-defs)))
76    (apply #'%jnew-proxy interface method-names-and-defs)))
77
78(defun jmake-invocation-handler (function)
79  (%jmake-invocation-handler function))
80 
81(when (autoloadp 'jmake-proxy)
82  (fmakunbound 'jmake-proxy))
83 
84(defgeneric jmake-proxy (interface implementation &optional lisp-this)
85   (:documentation "Returns a proxy Java object implementing the
86   provided interface using methods implemented in Lisp - typically
87   closures, but implementations are free to provide other
88   mechanisms. You can pass an optional 'lisp-this' object that will
89   be passed to the implementing methods as their first argument. If
90   you don't provide this object, NIL will be used. The second
91   argument of the Lisp methods is the name of the Java method being
92   implemented. This has the implication that overloaded methods are
93   merged, so you have to manually discriminate them if you want
94   to. The remaining arguments are java-objects wrapping the method's
95   parameters."))
96
97(defmethod jmake-proxy (interface invocation-handler &optional lisp-this)
98  "Basic implementation that directly uses an invocation handler."
99  (%jmake-proxy (jclass interface) invocation-handler lisp-this))
100
101(defmethod jmake-proxy (interface (implementation function) &optional lisp-this)
102   "Implements a Java interface forwarding method calls to a Lisp function."
103   (%jmake-proxy (jclass interface) (jmake-invocation-handler implementation) lisp-this))
104
105 (defmethod jmake-proxy (interface (implementation package) &optional lisp-this)
106   "Implements a Java interface mapping Java method names to symbols
107in a given package. javaMethodName is mapped to a JAVA-METHOD-NAME
108symbol. An error is signaled if no such symbol exists in the package,
109or if the symbol exists but does not name a function."
110
111   (flet ((java->lisp (name)
112      (with-output-to-string (str)
113        (let ((last-lower-p nil))
114    (map nil (lambda (char)
115         (let ((upper-p (char= (char-upcase char) char)))
116           (when (and last-lower-p upper-p)
117             (princ "-" str))
118           (setf last-lower-p (not upper-p))
119           (princ (char-upcase char) str)))
120         name)))))
121     (%jmake-proxy (jclass interface)
122       (jmake-invocation-handler 
123        (lambda (obj method &rest args)
124          (let ((sym (find-symbol
125          (java->lisp method)
126          implementation)))
127      (unless sym
128        (error "Symbol ~A, implementation of method ~A, not found in ~A"
129         (java->lisp method)
130         method
131         implementation))
132      (if (fboundp sym)
133          (apply (symbol-function sym) obj method args)
134          (error "Function ~A, implementation of method ~A, not found in ~A"
135           sym method implementation)))))
136       lisp-this)))
137
138(defmethod jmake-proxy (interface (implementation hash-table) &optional lisp-this)
139   "Implements a Java interface using closures in an hash-table keyed
140by Java method name."
141   (%jmake-proxy (jclass interface)
142    (jmake-invocation-handler 
143     (lambda (obj method &rest args)
144       (let ((fn (gethash method implementation)))
145         (if fn
146       (apply fn obj args)
147       (error "Implementation for method ~A not found in ~A"
148        method implementation)))))
149    lisp-this))
150 
151(defun jobject-class (obj)
152  "Returns the Java class that OBJ belongs to"
153  (jcall (jmethod "java.lang.Object" "getClass") obj))
154
155(defun jclass-superclass (class)
156  "Returns the superclass of CLASS, or NIL if it hasn't got one"
157  (jcall (jmethod "java.lang.Class" "getSuperclass") (jclass class)))
158
159(defun jclass-interfaces (class)
160  "Returns the vector of interfaces of CLASS"
161  (jcall (jmethod "java.lang.Class" "getInterfaces") (jclass class)))
162
163(defun jclass-interface-p (class)
164  "Returns T if CLASS is an interface"
165  (jcall (jmethod "java.lang.Class" "isInterface") (jclass class)))
166
167(defun jclass-superclass-p (class-1 class-2)
168  "Returns T if CLASS-1 is a superclass or interface of CLASS-2"
169  (jcall (jmethod "java.lang.Class" "isAssignableFrom" "java.lang.Class")
170         (jclass class-1)
171         (jclass class-2)))
172
173(defun jclass-array-p (class)
174  "Returns T if CLASS is an array class"
175  (jcall (jmethod "java.lang.Class" "isArray") (jclass class)))
176
177(defun jarray-component-type (atype)
178  "Returns the component type of the array type ATYPE"
179  (assert (jclass-array-p atype))
180  (jcall (jmethod "java.lang.Class" "getComponentType") atype))
181
182(defun jarray-length (java-array)
183  (jstatic "getLength" "java.lang.reflect.Array" java-array)  )
184
185(defun (setf jarray-ref) (new-value java-array &rest indices)
186  (apply #'jarray-set java-array new-value indices))
187
188(defun jnew-array-from-array (element-type array)
189  "Returns a new Java array with base type ELEMENT-TYPE (a string or a class-ref)
190   initialized from ARRAY"
191  (flet
192    ((row-major-to-index (dimensions n)
193                         (loop for dims on dimensions
194                           with indices
195                           do
196                           (multiple-value-bind (m r) (floor n (apply #'* (cdr dims)))
197                             (push m indices)
198                             (setq n r))
199                           finally (return (nreverse indices)))))
200    (let* ((fill-pointer (when (array-has-fill-pointer-p array) (fill-pointer array)))
201           (dimensions (if fill-pointer (list fill-pointer) (array-dimensions array)))
202           (jarray (apply #'jnew-array element-type dimensions)))
203      (dotimes (i (if fill-pointer fill-pointer (array-total-size array)) jarray)
204        #+maybe_one_day
205        (setf (apply #'jarray-ref jarray (row-major-to-index dimensions i)) (row-major-aref array i))
206        (apply #'(setf jarray-ref) (row-major-aref array i) jarray (row-major-to-index dimensions i))))))
207
208(defun jclass-constructors (class)
209  "Returns a vector of constructors for CLASS"
210  (jcall (jmethod "java.lang.Class" "getConstructors") (jclass class)))
211
212(defun jconstructor-params (constructor)
213  "Returns a vector of parameter types (Java classes) for CONSTRUCTOR"
214  (jcall (jmethod "java.lang.reflect.Constructor" "getParameterTypes") constructor))
215
216(defun jclass-fields (class &key declared public)
217  "Returns a vector of all (or just the declared/public, if DECLARED/PUBLIC is true) fields of CLASS"
218  (let* ((getter (if declared "getDeclaredFields" "getFields"))
219         (fields (jcall (jmethod "java.lang.Class" getter) (jclass class))))
220    (if public (delete-if-not #'jmember-public-p fields) fields)))
221
222(defun jclass-field (class field-name)
223  "Returns the field named FIELD-NAME of CLASS"
224  (jcall (jmethod "java.lang.Class" "getField" "java.lang.String")
225         (jclass class) field-name))
226
227(defun jfield-type (field)
228  "Returns the type (Java class) of FIELD"
229  (jcall (jmethod "java.lang.reflect.Field" "getType") field))
230
231(defun jfield-name (field)
232  "Returns the name of FIELD as a Lisp string"
233  (jcall (jmethod "java.lang.reflect.Field" "getName") field))
234
235(defun jclass-methods (class &key declared public)
236  "Return a vector of all (or just the declared/public, if DECLARED/PUBLIC is true) methods of CLASS"
237  (let* ((getter (if declared "getDeclaredMethods" "getMethods"))
238         (methods (jcall (jmethod "java.lang.Class" getter) (jclass class))))
239    (if public (delete-if-not #'jmember-public-p methods) methods)))
240
241(defun jmethod-params (method)
242  "Returns a vector of parameter types (Java classes) for METHOD"
243  (jcall (jmethod "java.lang.reflect.Method" "getParameterTypes") method))
244
245;; (defun jmethod-return-type (method)
246;;   "Returns the result type (Java class) of the METHOD"
247;;   (jcall (jmethod "java.lang.reflect.Method" "getReturnType") method))
248
249(defun jmethod-name (method)
250  "Returns the name of METHOD as a Lisp string"
251  (jcall (jmethod "java.lang.reflect.Method" "getName") method))
252
253(defun jinstance-of-p (obj class)
254  "OBJ is an instance of CLASS (or one of its subclasses)"
255  (and (java-object-p obj)
256       (jcall (jmethod "java.lang.Class" "isInstance" "java.lang.Object") (jclass class) obj)))
257
258(defun jmember-static-p (member)
259  "MEMBER is a static member of its declaring class"
260  (jstatic (jmethod "java.lang.reflect.Modifier" "isStatic" "int")
261           "java.lang.reflect.Modifier"
262           (jcall (jmethod "java.lang.reflect.Member" "getModifiers") member)))
263
264(defun jmember-public-p (member)
265  "MEMBER is a public member of its declaring class"
266  (jstatic (jmethod "java.lang.reflect.Modifier" "isPublic" "int")
267           "java.lang.reflect.Modifier"
268           (jcall (jmethod "java.lang.reflect.Member" "getModifiers") member)))
269
270(defun jmember-protected-p (member)
271  "MEMBER is a protected member of its declaring class"
272  (jstatic (jmethod "java.lang.reflect.Modifier" "isProtected" "int")
273           "java.lang.reflect.Modifier"
274           (jcall (jmethod "java.lang.reflect.Member" "getModifiers") member)))
275
276(defmethod make-load-form ((object java-object) &optional environment)
277  (declare (ignore environment))
278  (let ((class-name (jclass-name (jclass-of object))))
279    (cond
280     ((string= class-name "java.lang.reflect.Constructor")
281      `(java:jconstructor ,(jclass-name
282                            (jcall (jmethod "java.lang.reflect.Constructor"
283                                            "getDeclaringClass") object))
284                          ,@(loop for arg-type across
285                              (jcall
286                               (jmethod "java.lang.reflect.Constructor"
287                                        "getParameterTypes")
288                               object)
289                              collecting
290                              (jclass-name arg-type))))
291     ((string= class-name "java.lang.reflect.Method")
292      `(java:jmethod ,(jclass-name
293                       (jcall (jmethod "java.lang.reflect.Method"
294                                       "getDeclaringClass") object))
295                     ,(jmethod-name object)
296                     ,@(loop for arg-type across
297                         (jcall
298                          (jmethod "java.lang.reflect.Method"
299                                   "getParameterTypes")
300                          object)
301                         collecting
302                         (jclass-name arg-type))))
303     ((jinstance-of-p object "java.lang.Class")
304      `(java:jclass ,(jcall (jmethod "java.lang.Class" "getName") object)))
305     (t
306      (error "Unknown load-from for ~A" class-name)))))
307
308(provide "JAVA-EXTENSIONS")
309 (defun jproperty-value (obj prop)
310   (%jget-property-value obj prop))
311 
312 (defun (setf jproperty-value) (value obj prop)
313   (%jset-property-value obj prop value))
314
Note: See TracBrowser for help on using the repository browser.