Ticket #362: fix-set-java-field.patch

File fix-set-java-field.patch, 2.4 KB (added by Robert Goldman, 10 years ago)

Patch for invoke.lisp that extends the fix for GET-JAVA-FIELD to SET-JAVA-FIELD

  • contrib/jss/invoke.lisp

    diff --git a/contrib/jss/invoke.lisp b/contrib/jss/invoke.lisp
    index f23095d..b462f26 100644
    a b CLASS-NAME may either be a symbol or a string according to the usual JSS convent 
    314314
    315315(defvar *running-in-osgi* (ignore-errors (jclass "org.osgi.framework.BundleActivator")))
    316316
     317(define-condition no-such-java-field (error)
     318  ((field-name
     319    :initarg :field-name
     320    :reader field-name
     321    )
     322   (object
     323    :initarg :object
     324    :reader object
     325    ))
     326  (:report (lambda (c stream)
     327             (format stream "Unable to find a FIELD named ~a for ~a"
     328                     (field-name c) (object c))))
     329  )
     330
    317331(defun get-java-field (object field &optional (try-harder *running-in-osgi*))
    318332  "Get the value of the FIELD contained in OBJECT.
    319333If OBJECT is a symbol it names a dot qualified static FIELD."
    If OBJECT is a symbol it names a dot qualified static FIELD." 
    326340             (jfield (if (java-object-p field)
    327341                         field
    328342                         (or (find-declared-field field class)
    329                              (error "Unable to find a FIELD named ~a for ~a"
    330                                     field object)))))
     343                             (error 'no-such-java-field :field-name field :object object)))))
    331344        (#"setAccessible" jfield +true+)
    332345        (values (#"get" jfield object) jfield))
    333346      (if (symbolp object)
    associated is used to look up the static FIELD." 
    367380                        (jobject-class object))))
    368381             (jfield (if (java-object-p field)
    369382                         field
    370            (find field (#"getDeclaredFields" class) :key 'jfield-name :test 'equal))))
     383                       (or (find-declared-field field class)
     384                           (error 'no-such-java-field :field-name field :object object)))))
    371385        (#"setAccessible" jfield +true+)
    372386        (values (#"set" jfield object value) jfield))
    373387    (if (symbolp object)
    associated is used to look up the static FIELD." 
    377391            (setf (jfield (jclass-of object) field) value)
    378392            (setf (jfield object field) value)))))
    379393
     394(defun (setf get-java-field) (value object field &optional (try-harder *running-in-osgi*))
     395  (set-java-field object field value try-harder))
     396
    380397
    381398(defconstant +for-name+
    382399  (jmethod "java.lang.Class" "forName" "java.lang.String" "boolean" "java.lang.ClassLoader"))