source: branches/0.20.x/abcl/src/org/armedbear/lisp/java.lisp

Last change on this file was 12661, checked in by astalla, 15 years ago

Fix computation of the class precedence list for Java classes in case of multiple occurrences of the same interface in the class hierarchy.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 17.1 KB
Line 
1;;; java.lisp
2;;;
3;;; Copyright (C) 2003-2007 Peter Graves, Andras Simon
4;;; $Id: java.lisp 12661 2010-05-09 14:58:36Z astalla $
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(require "PRINT-OBJECT")
36
37(defun jregister-handler (object event handler &key data count)
38  (%jregister-handler object event handler data count))
39
40(defun jinterface-implementation (interface &rest method-names-and-defs)
41  "Creates and returns an implementation of a Java interface with
42   methods calling Lisp closures as given in METHOD-NAMES-AND-DEFS.
43
44   INTERFACE is either a Java interface or a string naming one.
45
46   METHOD-NAMES-AND-DEFS is an alternating list of method names
47   (strings) and method definitions (closures).
48
49   For missing methods, a dummy implementation is provided that
50   returns nothing or null depending on whether the return type is
51   void or not. This is for convenience only, and a warning is issued
52   for each undefined method."
53  (let ((interface (jclass interface))
54        (implemented-methods
55         (loop for m in method-names-and-defs
56           for i from 0
57           if (evenp i)
58           do (assert (stringp m) (m) "Method names must be strings: ~s" m) and collect m
59           else
60           do (assert (or (symbolp m) (functionp m)) (m) "Methods must be function designators: ~s" m)))
61        (null (make-immediate-object nil :ref)))
62    (loop for method across
63      (jclass-methods interface :declared nil :public t)
64      for method-name = (jmethod-name method)
65      when (not (member method-name implemented-methods :test #'string=))
66      do
67      (let* ((void-p (string= (jclass-name (jmethod-return-type method)) "void"))
68             (arglist (when (plusp (length (jmethod-params method))) '(&rest ignore)))
69             (def `(lambda
70                     ,arglist
71                     ,(when arglist '(declare (ignore ignore)))
72                     ,(if void-p '(values) null))))
73        (warn "Implementing dummy method ~a for interface ~a"
74              method-name (jclass-name interface))
75        (push (coerce def 'function) method-names-and-defs)
76        (push method-name method-names-and-defs)))
77    (apply #'%jnew-proxy interface method-names-and-defs)))
78
79(defun jmake-invocation-handler (function)
80  (%jmake-invocation-handler function))
81
82(when (autoloadp 'jmake-proxy)
83  (fmakunbound 'jmake-proxy))
84
85(defgeneric jmake-proxy (interface implementation &optional lisp-this)
86  (:documentation "Returns a proxy Java object implementing the provided interface using methods implemented in Lisp - typically closures, but implementations are free to provide other mechanisms. You can pass an optional 'lisp-this' object that will be passed to the implementing methods as their first argument. If you don't provide this object, NIL will be used. The second argument of the Lisp methods is the name of the Java method being implemented. This has the implication that overloaded methods are merged, so you have to manually discriminate them if you want to. The remaining arguments are java-objects wrapping the method's parameters."))
87
88(defmethod jmake-proxy (interface invocation-handler &optional lisp-this)
89  "Basic implementation that directly uses an invocation handler."
90  (%jmake-proxy (jclass interface) invocation-handler lisp-this))
91
92(defmethod jmake-proxy (interface (implementation function) &optional lisp-this)
93  "Implements a Java interface forwarding method calls to a Lisp function."
94  (%jmake-proxy (jclass interface) (jmake-invocation-handler implementation) lisp-this))
95
96(defmethod jmake-proxy (interface (implementation package) &optional lisp-this)
97  "Implements a Java interface mapping Java method names to symbols in a given package. javaMethodName is mapped to a JAVA-METHOD-NAME symbol. An error is signaled if no such symbol exists in the package, or if the symbol exists but does not name a function."
98  (flet ((java->lisp (name)
99     (with-output-to-string (str)
100       (let ((last-lower-p nil))
101         (map nil (lambda (char)
102        (let ((upper-p (char= (char-upcase char) char)))
103          (when (and last-lower-p upper-p)
104            (princ "-" str))
105          (setf last-lower-p (not upper-p))
106          (princ (char-upcase char) str)))
107        name)))))
108    (%jmake-proxy (jclass interface)
109      (jmake-invocation-handler 
110       (lambda (obj method &rest args)
111         (let ((sym (find-symbol
112         (java->lisp method)
113         implementation)))
114           (unless sym
115       (error "Symbol ~A, implementation of method ~A, not found in ~A"
116          (java->lisp method)
117          method
118          implementation))
119       (if (fboundp sym)
120           (apply (symbol-function sym) obj method args)
121           (error "Function ~A, implementation of method ~A, not found in ~A"
122            sym method implementation)))))
123      lisp-this)))
124
125(defmethod jmake-proxy (interface (implementation hash-table) &optional lisp-this)
126  "Implements a Java interface using closures in an hash-table keyed by Java method name."
127  (%jmake-proxy (jclass interface)
128    (jmake-invocation-handler 
129     (lambda (obj method &rest args)
130       (let ((fn (gethash method implementation)))
131         (if fn
132       (apply fn obj args)
133       (error "Implementation for method ~A not found in ~A"
134        method implementation)))))
135    lisp-this))
136
137(defun jobject-class (obj)
138  "Returns the Java class that OBJ belongs to"
139  (jcall (jmethod "java.lang.Object" "getClass") obj))
140
141(defun jclass-superclass (class)
142  "Returns the superclass of CLASS, or NIL if it hasn't got one"
143  (jcall (jmethod "java.lang.Class" "getSuperclass") (jclass class)))
144
145(defun jclass-interfaces (class)
146  "Returns the vector of interfaces of CLASS"
147  (jcall (jmethod "java.lang.Class" "getInterfaces") (jclass class)))
148
149(defun jclass-interface-p (class)
150  "Returns T if CLASS is an interface"
151  (jcall (jmethod "java.lang.Class" "isInterface") (jclass class)))
152
153(defun jclass-superclass-p (class-1 class-2)
154  "Returns T if CLASS-1 is a superclass or interface of CLASS-2"
155  (jcall (jmethod "java.lang.Class" "isAssignableFrom" "java.lang.Class")
156         (jclass class-1)
157         (jclass class-2)))
158
159(defun jclass-array-p (class)
160  "Returns T if CLASS is an array class"
161  (jcall (jmethod "java.lang.Class" "isArray") (jclass class)))
162
163(defun jarray-component-type (atype)
164  "Returns the component type of the array type ATYPE"
165  (assert (jclass-array-p atype))
166  (jcall (jmethod "java.lang.Class" "getComponentType") atype))
167
168(defun jarray-length (java-array)
169  (jstatic "getLength" "java.lang.reflect.Array" java-array)  )
170
171(defun (setf jarray-ref) (new-value java-array &rest indices)
172  (apply #'jarray-set java-array new-value indices))
173
174(defun jnew-array-from-array (element-type array)
175  "Returns a new Java array with base type ELEMENT-TYPE (a string or a class-ref)
176   initialized from ARRAY"
177  (flet
178    ((row-major-to-index (dimensions n)
179                         (loop for dims on dimensions
180                           with indices
181                           do
182                           (multiple-value-bind (m r) (floor n (apply #'* (cdr dims)))
183                             (push m indices)
184                             (setq n r))
185                           finally (return (nreverse indices)))))
186    (let* ((fill-pointer (when (array-has-fill-pointer-p array) (fill-pointer array)))
187           (dimensions (if fill-pointer (list fill-pointer) (array-dimensions array)))
188           (jarray (apply #'jnew-array element-type dimensions)))
189      (dotimes (i (if fill-pointer fill-pointer (array-total-size array)) jarray)
190        #+maybe_one_day
191        (setf (apply #'jarray-ref jarray (row-major-to-index dimensions i)) (row-major-aref array i))
192        (apply #'(setf jarray-ref) (row-major-aref array i) jarray (row-major-to-index dimensions i))))))
193
194(defun jclass-constructors (class)
195  "Returns a vector of constructors for CLASS"
196  (jcall (jmethod "java.lang.Class" "getConstructors") (jclass class)))
197
198(defun jconstructor-params (constructor)
199  "Returns a vector of parameter types (Java classes) for CONSTRUCTOR"
200  (jcall (jmethod "java.lang.reflect.Constructor" "getParameterTypes") constructor))
201
202(defun jclass-fields (class &key declared public)
203  "Returns a vector of all (or just the declared/public, if DECLARED/PUBLIC is true) fields of CLASS"
204  (let* ((getter (if declared "getDeclaredFields" "getFields"))
205         (fields (jcall (jmethod "java.lang.Class" getter) (jclass class))))
206    (if public (delete-if-not #'jmember-public-p fields) fields)))
207
208(defun jclass-field (class field-name)
209  "Returns the field named FIELD-NAME of CLASS"
210  (jcall (jmethod "java.lang.Class" "getField" "java.lang.String")
211         (jclass class) field-name))
212
213(defun jfield-type (field)
214  "Returns the type (Java class) of FIELD"
215  (jcall (jmethod "java.lang.reflect.Field" "getType") field))
216
217(defun jfield-name (field)
218  "Returns the name of FIELD as a Lisp string"
219  (jcall (jmethod "java.lang.reflect.Field" "getName") field))
220
221
222(defun (setf jfield) (newvalue class-ref-or-field field-or-instance
223          &optional (instance nil instance-supplied-p) unused-value)
224  (declare (ignore unused-value))
225  (if instance-supplied-p
226      (jfield class-ref-or-field field-or-instance instance newvalue)
227      (jfield class-ref-or-field field-or-instance newvalue)))
228
229(defun jclass-methods (class &key declared public)
230  "Return a vector of all (or just the declared/public, if DECLARED/PUBLIC is true) methods of CLASS"
231  (let* ((getter (if declared "getDeclaredMethods" "getMethods"))
232         (methods (jcall (jmethod "java.lang.Class" getter) (jclass class))))
233    (if public (delete-if-not #'jmember-public-p methods) methods)))
234
235(defun jmethod-params (method)
236  "Returns a vector of parameter types (Java classes) for METHOD"
237  (jcall (jmethod "java.lang.reflect.Method" "getParameterTypes") method))
238
239(defun jmethod-return-type (method)
240  "Returns the result type (Java class) of the METHOD"
241  (jcall (jmethod "java.lang.reflect.Method" "getReturnType") method))
242
243(defun jmethod-declaring-class (method)
244  "Returns the Java class declaring METHOD"
245  (jcall (jmethod "java.lang.reflect.Method" "getDeclaringClass") method))
246
247(defun jmethod-name (method)
248  "Returns the name of METHOD as a Lisp string"
249  (jcall (jmethod "java.lang.reflect.Method" "getName") method))
250
251(defun jinstance-of-p (obj class)
252  "OBJ is an instance of CLASS (or one of its subclasses)"
253  (and (java-object-p obj)
254       (jcall (jmethod "java.lang.Class" "isInstance" "java.lang.Object") (jclass class) obj)))
255
256(defun jmember-static-p (member)
257  "MEMBER is a static member of its declaring class"
258  (jstatic (jmethod "java.lang.reflect.Modifier" "isStatic" "int")
259           "java.lang.reflect.Modifier"
260           (jcall (jmethod "java.lang.reflect.Member" "getModifiers") member)))
261
262(defun jmember-public-p (member)
263  "MEMBER is a public member of its declaring class"
264  (jstatic (jmethod "java.lang.reflect.Modifier" "isPublic" "int")
265           "java.lang.reflect.Modifier"
266           (jcall (jmethod "java.lang.reflect.Member" "getModifiers") member)))
267
268(defun jmember-protected-p (member)
269  "MEMBER is a protected member of its declaring class"
270  (jstatic (jmethod "java.lang.reflect.Modifier" "isProtected" "int")
271           "java.lang.reflect.Modifier"
272           (jcall (jmethod "java.lang.reflect.Member" "getModifiers") member)))
273
274(defmethod make-load-form ((object java-object) &optional environment)
275  (declare (ignore environment))
276  (let ((class-name (jclass-name (jclass-of object))))
277    (cond
278     ((string= class-name "java.lang.reflect.Constructor")
279      `(java:jconstructor ,(jclass-name
280                            (jcall (jmethod "java.lang.reflect.Constructor"
281                                            "getDeclaringClass") object))
282                          ,@(loop for arg-type across
283                              (jcall
284                               (jmethod "java.lang.reflect.Constructor"
285                                        "getParameterTypes")
286                               object)
287                              collecting
288                              (jclass-name arg-type))))
289     ((string= class-name "java.lang.reflect.Method")
290      `(java:jmethod ,(jclass-name
291                       (jcall (jmethod "java.lang.reflect.Method"
292                                       "getDeclaringClass") object))
293                     ,(jmethod-name object)
294                     ,@(loop for arg-type across
295                         (jcall
296                          (jmethod "java.lang.reflect.Method"
297                                   "getParameterTypes")
298                          object)
299                         collecting
300                         (jclass-name arg-type))))
301     ((jinstance-of-p object "java.lang.Class")
302      `(java:jclass ,(jcall (jmethod "java.lang.Class" "getName") object)))
303     (t
304      (error "Unknown load-form for ~A" class-name)))))
305
306(defun jproperty-value (obj prop)
307  (%jget-property-value obj prop))
308
309(defun (setf jproperty-value) (value obj prop)
310  (%jset-property-value obj prop value))
311
312;;; print-object
313
314(defmethod print-object ((obj java:java-object) stream)
315  (write-string (sys::%write-to-string obj) stream))
316
317(defmethod print-object ((e java:java-exception) stream)
318  (if *print-escape*
319      (print-unreadable-object (e stream :type t :identity t)
320        (format stream "~A"
321                (java:jcall (java:jmethod "java.lang.Object" "toString")
322                            (java:java-exception-cause e))))
323      (format stream "Java exception '~A'."
324              (java:jcall (java:jmethod "java.lang.Object" "toString")
325                          (java:java-exception-cause e)))))
326
327;;; JAVA-CLASS support
328(defconstant +java-lang-object+ (jclass "java.lang.Object"))
329
330(defclass java-class (standard-class)
331  ((jclass :initarg :java-class
332     :initform (error "class is required")
333     :reader java-class-jclass)))
334
335;;init java.lang.Object class
336(defconstant +java-lang-object-class+
337  (%register-java-class +java-lang-object+
338      (mop::ensure-class (make-symbol "java.lang.Object")
339             :metaclass (find-class 'java-class)
340             :direct-superclasses (list (find-class 'java-object))
341             :java-class +java-lang-object+)))
342
343(defun ensure-java-class (jclass)
344  (let ((class (%find-java-class jclass)))
345    (if class
346  class
347  (%register-java-class
348   jclass (mop::ensure-class
349     (make-symbol (jclass-name jclass))
350     :metaclass (find-class 'java-class)
351     :direct-superclasses
352     (let ((supers
353      (mapcar #'ensure-java-class
354        (delete nil
355          (concatenate 'list
356                 (list (jclass-superclass jclass))
357                 (jclass-interfaces jclass))))))
358       (if (jclass-interface-p jclass)
359           (append supers (list (find-class 'java-object)))
360           supers))
361     :java-class jclass)))))
362
363(defmethod mop::compute-class-precedence-list ((class java-class))
364  "Sort classes this way:
365   1. Java classes (but not java.lang.Object)
366   2. Java interfaces
367   3. java.lang.Object
368   4. other classes
369   Rationale:
370   1. Concrete classes are the most specific.
371   2. Then come interfaces.
372     So if a generic function is specialized both on an interface and a concrete class,
373     the concrete class comes first.
374   3. because everything is an Object.
375   4. to handle base CLOS classes.
376   Note: Java interfaces are not sorted among themselves in any way, so if a
377   gf is specialized on two different interfaces and you apply it to an object that
378   implements both, it is unspecified which method will be called."
379  (let ((cpl (nreverse (mop::collect-superclasses* class))))
380    (flet ((score (class)
381       (if (not (typep class 'java-class))
382     4
383     (cond
384       ((jcall (jmethod "java.lang.Object" "equals" "java.lang.Object")
385         (java-class-jclass class) +java-lang-object+) 3)
386       ((jclass-interface-p (java-class-jclass class)) 2)
387       (t 1)))))
388      (stable-sort cpl #'(lambda (x y)
389         (< (score x) (score y)))))))
390   
391(defmethod make-instance ((class java-class) &rest initargs &key &allow-other-keys)
392  (declare (ignore initargs))
393  (error "make-instance not supported for ~S" class))
394
395(provide "JAVA")
Note: See TracBrowser for help on using the repository browser.