In case it is of some interest, the code below is what I use with jss. It's main element of interest is the "try-harder" flag, which uses setAccessible to allow setting some fields that would otherwise cause an exception.<div>
<br></div><div><div>(defun get-java-field (object field &optional try-harder)</div><div> (if try-harder</div><div> (let* ((class (if (symbolp object)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(setq object (find-java-class object))</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> (if (equal "java.lang.Class" (jclass-name (jobject-class object)) )</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> object</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>(jobject-class object))))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (jfield (if (java-object-p field)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> field</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> (find field (#"getDeclaredFields" class) :key 'jfield-name :test 'equal))))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(#"setAccessible" jfield t)</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>(values (#"get" jfield object) jfield))</div><div> (if (symbolp object)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(let ((class (find-java-class object)))</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> (#"peekStatic" 'invoke class field))</div><div> (#"peek" 'invoke object field))))</div><div><br></div><div>(defun set-java-field (object field value &optional try-harder)</div>
<div> (if try-harder</div><div> (let* ((class (if (symbolp object)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(setq object (find-java-class object))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (if (equal "java.lang.Class" (jclass-name (jobject-class object)) )</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> object</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(jobject-class object))))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (jfield (if (java-object-p field)</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> field</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (find field (#"getDeclaredFields" class) :key 'jfield-name :test 'equal))))</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>(#"setAccessible" jfield t)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(values (#"set" jfield object value) jfield))</div>
<div> (if (symbolp object)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(let ((class (find-java-class object)))</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> (#"pokeStatic" 'invoke class field value))</div>
<div> (#"poke" 'invoke object field value))))</div><div><br></div><br><div class="gmail_quote">On Tue, Nov 24, 2009 at 4:27 PM, <span dir="ltr"><<a href="mailto:logicmoo@gmail.com">logicmoo@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Thanks, you are right, much better to use the built-in key stuff<br>
<br>
(defun (setf jfield) (newvalue class-ref-or-field field-or-instance<br>
&optional ( instance nil instance-supplied-p) unused-value)<br>
(declare (ignore unused-value))<br>
(if instance-supplied-p<br>
(jfield class-ref-or-field field-or-instance instance newvalue)<br>
(jfield class-ref-or-field field-or-instance newvalue)))<br>
<br>
<br>
----- Original Message -----<br>
From: "Thomas Russ" <<a href="mailto:tar@ISI.EDU">tar@ISI.EDU</a>><br>
To: <<a href="mailto:dmiles@users.sourceforge.net">dmiles@users.sourceforge.net</a>><br>
Sent: Tuesday, November 24, 2009 9:24 AM<br>
Subject: Re: [armedbear-devel] SETF for JFEILDS<br>
<br>
<br>
<br>
On Nov 23, 2009, at 6:47 AM, <a href="mailto:logicmoo@gmail.com">logicmoo@gmail.com</a> wrote:<br>
<br>
> So actually<br>
><br>
> (defun (setf jfield) (newvalue class-ref-or-field field-or-<br>
> instance &optional ( instance :noinst) unusedvalue)<br>
> (if (eq instance :noinst)<br>
> (jfield class-ref-or-field field-or-instance newvalue)<br>
> (jfield class-ref-or-field field-or-instance instance newvalue)))<br>
><br>
> Would be a little better.. good eye.<br>
<br>
For what it's worth, I am not a fan of using special values to<br>
determine whether optional (or keyword) arguments are actually passed<br>
or not. I prefer to use the predicate forms from the lambda list, so<br>
something more like<br>
<br>
(defun (setf jfield) (newvalue class-ref-or-field field-or-instance<br>
&optional ( instance nil instance-supplied-p)<br>
unusedvalue)<br>
(declare (ignore unusedvalue))<br>
(if instance-supplied-p<br>
(jfield class-ref-or-field field-or-instance newvalue)<br>
(jfield class-ref-or-field field-or-instance instance newvalue)))<br>
<br>
<br>
<br>
<br>
><br>
><br>
> ----- Original Message -----<br>
> From: <<a href="mailto:dmiles@users.sourceforge.net">dmiles@users.sourceforge.net</a>><br>
> To: "Alessio Stalla" <<a href="mailto:alessiostalla@gmail.com">alessiostalla@gmail.com</a>><br>
> Cc: "Armed Bear" <<a href="mailto:armedbear-devel@common-lisp.net">armedbear-devel@common-lisp.net</a>><br>
> Sent: Monday, November 23, 2009 6:27 AM<br>
> Subject: Re: [armedbear-devel] SETF for JFEILDS<br>
><br>
><br>
>><br>
>> ----- Original Message -----<br>
>> From: "Alessio Stalla" <<a href="mailto:alessiostalla@gmail.com">alessiostalla@gmail.com</a>><br>
>> To: <<a href="mailto:dmiles@users.sourceforge.net">dmiles@users.sourceforge.net</a>><br>
>> Cc: "Armed Bear" <<a href="mailto:armedbear-devel@common-lisp.net">armedbear-devel@common-lisp.net</a>><br>
>> Sent: Monday, November 23, 2009 5:42 AM<br>
>> Subject: Re: [armedbear-devel] SETF for JFEILDS<br>
>><br>
>><br>
>> On Mon, Nov 23, 2009 at 2:36 PM, <<a href="mailto:logicmoo@gmail.com">logicmoo@gmail.com</a>> wrote:<br>
>>>> Does anyone object to adding this to java.lisp? If not could it<br>
>>>> be done?<br>
>>>><br>
>>>> (defun (setf jfield)<br>
>>>> (newvalue class-ref-or-field field-or-instance &optional<br>
>>>> ( instance :noinst) (value :novalue))<br>
>>>> (if (eq instance :noinst)<br>
>>>> (jfield class-ref-or-field field-or-instance newvalue)<br>
>>>> (jfield class-ref-or-field field-or-instance instance newvalue)))<br>
>><br>
>>> It seems a nice idea to me, but there are a couple of things that I<br>
>>> don't understand:<br>
>><br>
>> A>- why :noinst and not simply nil?<br>
>><br>
>> JFIELD is defined as: class-ref-or-field field-or-instance<br>
>> &optional instance value<br>
>> The valid argument patterns for this operation are:<br>
>> (class-ref field-name): to retrieve the value of a static field.<br>
>> (class-ref field-name instance-ref): to retrieve the value of a<br>
>> class field of the instance.<br>
>> (class-ref field-name primitive-value:) to store primitive-value<br>
>> in a static field.<br>
>> (class-ref field-name instance-ref value): to store value in a<br>
>> class field of the instance.<br>
>> (class-ref field-name nil value): to store value in a static<br>
>> field (when value may be confused with an instance-ref).<br>
>> (field-name instance): to retrieve the value of a field of the<br>
>> instance. The class is derived from the instance.<br>
>> (field-name instance value): to store value in a field of the<br>
>> instance. The class is derived from the instance.<br>
>><br>
>> JFIELD doesn't distingusih between static and non static fields<br>
>><br>
>> Also cases of "class-ref field-name instance-ref<br>
>> value" (accessing superclass field)<br>
>><br>
>> Depending on how the field is defined<br>
>><br>
>> (setf (jfield *MyClass* "someStaticBooleanField") NIL)<br>
>> it is distinguable from<br>
>> (setf (jfield *MySuperClass* "nonStaticBooleanField" *my-<br>
>> instance* ) NIL)<br>
>><br>
>><br>
>> --------------------------------------------------------------------------------------------------------------------------<br>
>> A>- value is not used, what's the point of it?<br>
>><br>
>> correct the "default :novalue" is not needed.<br>
>> (I was debugging and trying to find corner cases with all those<br>
>> bizzare legal jfield signatures )<br>
>><br>
>><br>
>> but... "value" as an &optional was needed to match the signature<br>
>> of what is legal for jfield so things like this can work:<br>
>><br>
>> (define-symbol-macro %iscold (jfield "org.armedbear.lisp.Lisp"<br>
>> "cold"))<br>
>> %iscold ;; ==> NIL<br>
>> (setq %iscold T) ;; ==> T<br>
>> %iscold ;; ==> T<br>
>> (setq %iscold NIL) ;; ==> NIL<br>
>><br>
>><br>
><br>
> _______________________________________________<br>
> armedbear-devel mailing list<br>
> <a href="mailto:armedbear-devel@common-lisp.net">armedbear-devel@common-lisp.net</a><br>
> <a href="http://common-lisp.net/cgi-bin/mailman/listinfo/armedbear-devel" target="_blank">http://common-lisp.net/cgi-bin/mailman/listinfo/armedbear-devel</a><br>
<br>
<br>
_______________________________________________<br>
armedbear-devel mailing list<br>
<a href="mailto:armedbear-devel@common-lisp.net">armedbear-devel@common-lisp.net</a><br>
<a href="http://common-lisp.net/cgi-bin/mailman/listinfo/armedbear-devel" target="_blank">http://common-lisp.net/cgi-bin/mailman/listinfo/armedbear-devel</a><br>
</blockquote></div><br></div>