Changeset 13285
- Timestamp:
- 05/22/11 02:18:23 (11 years ago)
- Location:
- trunk/abcl/contrib/jss
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/abcl/contrib/jss/compat.lisp
r13283 r13285 6 6 (defun ensure-compatiblity () 7 7 (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)))) 11 15 12 16 -
trunk/abcl/contrib/jss/invoke.lisp
r13284 r13285 1 ;; invoke.lisp v 1.01 ;; invoke.lisp v2.0 2 2 ;; 3 3 ;; Copyright (C) 2005 Alan Ruttenberg 4 ;; Copyright (C) 2011 Mark Evenson 4 5 ;; 5 6 ;; Since most of this code is derivative of the Jscheme System, it is … … 29 30 ;; distribution. 30 31 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 42 38 ;; constructor, and in those cases, you only need to know enough of 43 39 ;; the class name that is unique wrt to the classes on your classpath. … … 52 48 ;; (print (#"toString" sw))) 53 49 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)... 60 58 61 59 ;; 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. 65 62 66 63 ;; 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 java68 ;; object to lisp object conversions that abcldoes.64 ;; return their results as Java objects, avoiding doing the usual Java 65 ;; object to Lisp object conversions that ABCL does. 69 66 70 67 ;; (with-constant-signature ((name jname raw?)*) &body body) … … 88 85 ;; 89 86 ;; TODO 90 ;; - Use a package other than common-lisp-user91 87 ;; - Make with-constant-signature work for static methods too. 92 88 ;; - #2"toString" to work like function scoped (with-constant-signature ((tostring "toString")) ...) … … 125 121 126 122 (in-package :jss) 127 128 ;; invoke takes it's arguments in a java array. In order to not cons129 ;; one up each time, but to be thread safe, we allocate a static array130 ;; of such arrays and save them in threadlocal storage. I'm lazy and131 ;; so I just assume you will never call a java method with more than132 ;; *max-java-method-args*. Fix this if it is a problem for you. We133 ;; don't need to worry about reentrancy as the array is used only134 ;; between when we call invoke and when invoke calls the actual135 ;; function you care about.136 123 137 124 (eval-when (:compile-toplevel :load-toplevel :execute) … … 190 177 (apply #'jcall method object args)))))) 191 178 179 (defconstant +true+ (make-immediate-object t :boolean)) 180 192 181 ;;; Method name as String --> String | Symbol --> jmethod 193 182 (defvar *methods-cache* (make-hash-table :test #'equal)) … … 208 197 jmethod)) 209 198 210 (defparameter *last-invoke-find-method-args* nil) 199 (defconstant +set-accessible+ 200 (jmethod "java.lang.reflect.AccessibleObject" "setAccessible" "boolean")) 201 211 202 ;;; TODO optimize me! 212 203 (defun invoke-find-method (method object args) 213 (setf *last-invoke-find-method-args* (list method object args))214 204 (let ((jmethod (get-jmethod method object))) 215 205 (unless jmethod … … 222 212 (apply #'jresolve-method 223 213 method object args))) 224 (jcall "setAccessible"jmethod +true+)214 (jcall +set-accessible+ jmethod +true+) 225 215 (set-jmethod method object jmethod)) 226 216 jmethod)) … … 232 222 ;; automagically convert the returned java object into a lisp object. So 233 223 ;; #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. 235 225 236 226 … … 376 366 (jmethod "java.lang.Class" "forName" "java.lang.String" "boolean" "java.lang.ClassLoader")) 377 367 378 (defconstant +true+379 (make-immediate-object t :boolean))380 381 368 (defun find-java-class (name) 382 369 (or (jstatic +for-name+ "java.lang.Class" -
trunk/abcl/contrib/jss/packages.lisp
r13284 r13285 7 7 #:*do-auto-imports* 8 8 9 #:invoke-restargs 10 #:with-constant-signature 11 12 #:invoke-add-imports 9 13 #:add-directory-jars-to-class-path 10 14 #:add-to-classpath
Note: See TracChangeset
for help on using the changeset viewer.