01/25/09 23:34:24 (12 years ago)

Merged the scripting branch, providing JSR-223 support and other new
features. JSR-233 is only built if the necessary javax.script.* classes
are found in the CLASSPATH.

1 edited


  • trunk/abcl/src/org/armedbear/lisp/java.lisp

    r11529 r11590  
    7575        (push method-name method-names-and-defs)))
    7676    (apply #'%jnew-proxy interface method-names-and-defs)))
     78(defun jmake-invocation-handler (function)
     79  (%jmake-invocation-handler function))
     81(when (autoloadp 'jmake-proxy)
     82  (fmakunbound 'jmake-proxy))
     84(defgeneric jmake-proxy (interface implementation &optional lisp-this)
     85  (: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(defmethod jmake-proxy (interface invocation-handler &optional lisp-this)
     88  "Basic implementation that directly uses an invocation handler."
     89  (%jmake-proxy (jclass interface) invocation-handler lisp-this))
     91(defmethod jmake-proxy (interface (implementation function) &optional lisp-this)
     92  "Implements a Java interface forwarding method calls to a Lisp function."
     93  (%jmake-proxy (jclass interface) (jmake-invocation-handler implementation) lisp-this))
     95(defmethod jmake-proxy (interface (implementation package) &optional lisp-this)
     96  "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."
     97  (flet ((java->lisp (name)
     98     (with-output-to-string (str)
     99       (let ((last-lower-p nil))
     100         (map nil (lambda (char)
     101        (let ((upper-p (char= (char-upcase char) char)))
     102          (when (and last-lower-p upper-p)
     103            (princ "-" str))
     104          (setf last-lower-p (not upper-p))
     105          (princ (char-upcase char) str)))
     106        name)))))
     107    (%jmake-proxy (jclass interface)
     108      (jmake-invocation-handler
     109       (lambda (obj method &rest args)
     110         (let ((sym (find-symbol
     111         (java->lisp method)
     112         implementation)))
     113           (unless sym
     114       (error "Symbol ~A, implementation of method ~A, not found in ~A"
     115          (java->lisp method)
     116          method
     117          implementation))
     118       (if (fboundp sym)
     119           (apply (symbol-function sym) obj method args)
     120           (error "Function ~A, implementation of method ~A, not found in ~A"
     121            sym method implementation)))))
     122      lisp-this)))
     124(defmethod jmake-proxy (interface (implementation hash-table) &optional lisp-this)
     125  "Implements a Java interface using closures in an hash-table keyed by Java method name."
     126  (%jmake-proxy (jclass interface)
     127    (jmake-invocation-handler
     128     (lambda (obj method &rest args)
     129       (let ((fn (gethash method implementation)))
     130         (if fn
     131       (apply fn obj args)
     132       (error "Implementation for method ~A not found in ~A"
     133        method implementation)))))
     134    lisp-this))
    78136(defun jobject-class (obj)
    233291      (error "Unknown load-from for ~A" class-name)))))
     293(defun jproperty-value (obj prop)
     294  (%jget-property-value obj prop))
     296(defun (setf jproperty-value) (value obj prop)
     297  (%jset-property-value obj prop value))
    235299(provide "JAVA-EXTENSIONS")
Note: See TracChangeset for help on using the changeset viewer.