Changeset 13285


Ignore:
Timestamp:
05/22/11 02:18:23 (10 years ago)
Author:
Mark Evenson
Message:

ENSURE-COMPATIBILTY now imports most of the external JSS into CL-USER.

Adjusted comments.

Optimize INVOKE-FIND-METHOD a bit by the use of a constant DEFMETHOD.

Location:
trunk/abcl/contrib/jss
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/abcl/contrib/jss/compat.lisp

    r13283 r13285  
    66(defun ensure-compatiblity ()
    77  (setf *cl-user-compatibility* t)
    8   (dolist (symbol '(get-java-field new))
    9     (unintern symbol :cl-user)
    10     (import symbol :cl-user)))
     8  (let ((dont-export '(add-to-classpath *cl-user-compatibility*)))
     9    (loop :for symbol :being :each :external-symbol :in :jss
     10       :when (not (find symbol dont-export))
     11       :do
     12         (unintern symbol :cl-user)
     13       :and :do
     14         (import symbol :cl-user))))
    1115
    1216   
  • trunk/abcl/contrib/jss/invoke.lisp

    r13284 r13285  
    1 ;; invoke.lisp v1.0
     1;; invoke.lisp v2.0
    22;;
    33;; Copyright (C) 2005 Alan Ruttenberg
     4;; Copyright (C) 2011 Mark Evenson
    45;;
    56;; Since most of this code is derivative of the Jscheme System, it is
     
    2930;;    distribution.
    3031
    31 ;; This file uses invoke.java from jscheme
    32 ;; (http://jscheme.sourceforge.net/jscheme/src/jsint/Invoke.java).
    33 ;; The easiest way to use it is to download
    34 ;; http://jscheme.sourceforge.net/jscheme/lib/jscheme.jar
    35 ;; and add it to the classpath in the file that invokes abcl.
    36 
    37 ;; Invoke.java  effectively implements dynamic dispatch of java methods. This
    38 ;; is used to make it real easy, if perhaps less efficient, to write
    39 ;; java code since you don't need to be bothered with imports, or with
    40 ;; figuring out which method to call.  The only time that you need to
    41 ;; know a class name is when you want to call a static method, or a
     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
    4238;; constructor, and in those cases, you only need to know enough of
    4339;; the class name that is unique wrt to the classes on your classpath.
     
    5248;;   (print (#"toString" sw)))
    5349
    54 ;; What's happened here? First, all the classes in all the jars in the classpath have
    55 ;; been collected.  For each class a.b.C.d, we have recorded that
    56 ;; b.c.d, b.C.d, C.d, c.d, and d potentially refer to this class. In
    57 ;; your call to new, as long as the symbol can refer to only one class, we use that
    58 ;; class. In this case, it is java.io.StringWriter. You could also have written
    59 ;; (new 'io.stringwriter), (new '|io.StringWriter|), (new 'java.io.StringWriter)...
     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)...
    6058
    6159;; the call (#"write" sw "Hello "), uses the code in invoke.java to
    62 ;; call the method named "write" with the arguments sw and "Hello
    63 ;; ". Invoke.java figures out the right java method to call, and calls
    64 ;; it.
     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.
    6562
    6663;; If you want to do a raw java call, use #0"toString". Raw calls
    67 ;; return their results as java objects, avoiding doing the usual java
    68 ;; object to lisp object conversions that abcl does.
     64;; return their results as Java objects, avoiding doing the usual Java
     65;; object to Lisp object conversions that ABCL does.
    6966
    7067;; (with-constant-signature ((name jname raw?)*) &body body)
     
    8885;;
    8986;; TODO
    90 ;;   - Use a package other than common-lisp-user
    9187;;   - Make with-constant-signature work for static methods too.
    9288;;   - #2"toString" to work like function scoped (with-constant-signature ((tostring "toString")) ...)
     
    125121
    126122(in-package :jss)
    127 
    128 ;; invoke takes it's arguments in a java array. In order to not cons
    129 ;; one up each time, but to be thread safe, we allocate a static array
    130 ;; of such arrays and save them in threadlocal storage. I'm lazy and
    131 ;; so I just assume you will never call a java method with more than
    132 ;; *max-java-method-args*. Fix this if it is a problem for you. We
    133 ;; don't need to worry about reentrancy as the array is used only
    134 ;; between when we call invoke and when invoke calls the actual
    135 ;; function you care about.
    136123
    137124(eval-when (:compile-toplevel :load-toplevel :execute)
     
    190177                (apply #'jcall method object args))))))
    191178
     179(defconstant +true+ (make-immediate-object t :boolean))
     180
    192181;;; Method name as String --> String  | Symbol --> jmethod
    193182(defvar *methods-cache* (make-hash-table :test #'equal))
     
    208197   jmethod))
    209198
    210 (defparameter *last-invoke-find-method-args* nil)
     199(defconstant +set-accessible+
     200  (jmethod "java.lang.reflect.AccessibleObject" "setAccessible" "boolean"))
     201
    211202;;; TODO optimize me!
    212203(defun invoke-find-method (method object args)
    213   (setf *last-invoke-find-method-args* (list method object args))
    214204  (let ((jmethod (get-jmethod method object)))
    215205    (unless jmethod
     
    222212                (apply #'jresolve-method
    223213                       method object args)))
    224       (jcall "setAccessible" jmethod +true+)
     214      (jcall +set-accessible+ jmethod +true+)
    225215      (set-jmethod method object jmethod))
    226216    jmethod))
     
    232222;; automagically convert the returned java object into a lisp object. So
    233223;; #0"toString" returns a java.lang.String object, where as #"toString" returns
    234 ;; a regular lisp string as abcl converts the java string to a lisp string.
     224;; a regular Lisp string as ABCL converts the Java string to a Lisp string.
    235225
    236226
     
    376366  (jmethod "java.lang.Class" "forName" "java.lang.String" "boolean" "java.lang.ClassLoader"))
    377367
    378 (defconstant +true+
    379   (make-immediate-object t :boolean))
    380 
    381368(defun find-java-class (name)
    382369  (or (jstatic +for-name+ "java.lang.Class"
  • trunk/abcl/contrib/jss/packages.lisp

    r13284 r13285  
    77   #:*do-auto-imports*
    88
     9   #:invoke-restargs
     10   #:with-constant-signature
     11
     12   #:invoke-add-imports
    913   #:add-directory-jars-to-class-path
    1014   #:add-to-classpath
Note: See TracChangeset for help on using the changeset viewer.