<span style="font-family:arial, sans-serif;font-size:13px;border-collapse:collapse">Greetings,<div><br></div><div>I find the following utility macros helpful when wanting to access Java from Lisp.  I will give some examples after the actual code.</div>


<div><br></div><div><div>(defmacro defun-class-method (lfun cls-name meth-name &rest arg-types)</div><div>  (let ((arglist (mapcar (lambda (x) (gensym)) arg-types))</div><div><span style="white-space:pre"> </span>(method (gensym))</div>


<div><span style="white-space:pre">       </span>(cls (gensym)))</div><div>    `(let ((,method (jmethod ,cls-name ,meth-name ,@arg-types))</div><div><span style="white-space:pre"> </span>   (,cls (jclass ,cls-name)))</div>
<div>       (defun ,lfun ,arglist</div><div><span style="white-space:pre">    </span> (jcall ,method ,cls ,@arglist)))))</div><div><br></div><div>(defmacro defun-instance-method (lfun cls-name meth-name &rest arg-types)</div>


<div>  (let ((arglist (mapcar (lambda (x) (gensym)) (cons nil arg-types)))</div><div><span style="white-space:pre">   </span>(method (gensym)))</div><div>    `(let ((,method (jmethod ,cls-name ,meth-name ,@arg-types)))</div>


<div>       (defun ,lfun ,arglist</div><div><span style="white-space:pre">    </span> (jcall ,method ,@arglist)))))</div><div><br></div><div>(defmacro defun-constructor (lfun cls-name &rest arg-types)</div><div>  (let ((arglist (mapcar (lambda (x) (gensym)) arg-types))</div>


<div><span style="white-space:pre">       </span>(method (gensym)))</div><div>    `(let ((,method (jconstructor ,cls-name ,@arg-types)))</div><div>       (defun ,lfun ,arglist</div><div><span style="white-space:pre">        </span> (jnew ,method ,@arglist)))))</div>


<div><br></div><div>Examples:</div><div><br></div><div>The following line defines a lisp function named "java-abs".  java-abs is a proxy for the java version.  It takes the same arguments.  </div><div><br></div>


<div>(defun-class-method java-abs "java.lang.Math" "abs" "int")</div><div><br></div><div>After the above is called, you may call the Java method from lisp as follows:</div><div><br></div><div>


(java-abs -44)</div><div><br></div><div>The lisp functions created by these utilities are fast because they are closures.  The class and method lookups are only done once.</div><div><br></div><div>Other examples:</div><div>


<br></div><div><div>(defun-constructor newInteger "java.lang.Integer" "int")</div><div>(defun-instance-method hashCode "java.lang.Integer" "hashCode")</div><div><br></div><div>I am in the process of doing the same thing for Java.  Stay tuned.</div>


<div><br></div><div>Blake McBride</div><div><br></div></div></div></span>