Skip to content

Instantly share code, notes, and snippets.

@Gastove
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gastove/b083083f22d7f198d964 to your computer and use it in GitHub Desktop.
Save Gastove/b083083f22d7f198d964 to your computer and use it in GitHub Desktop.
;; I am trying to parse a nested Java object (a Google Protobuffer), which comes in as raw bytes. To parse the bytes,
;; I have to know the class of the object. Fortunately, for the innermost object, the class is specified as a field in
;; an outer object. The parsing function is specified as a static method on the class, with expected use like:
;; (com.url.place.Type$SubType/parseFrom (.getByteArrayFromInnerObj inner-obj))
;; What I'm trying to do is something like this:
(def decl-obj-map
{:type-subtype Type$SubType})
(defn call-fn
[class n-args method]
(let [o (gensym)
args (repeatedly n-args gensym)
assure-symbol (fn [thing] (if (symbol? thing) thing (symbol thing)))
method (assure-symbol method)]
(eval
`(fn [~o ~@args]
(. ~(with-meta o {:tag class})
(~method ~@args))))))
(def parse-func (call-fn (:type-subtype decl-obj-map) 0 "parseFrom")
(parse-func (.getByteArrayFromInnerObj inner-obj)) ;; This call explodes.
@Gastove
Copy link
Author

Gastove commented Jun 29, 2014

Worth pointing out that parseFrom dispatches on both arity and type; there are something like six different parseFrom methods on this object.

@Gastove
Copy link
Author

Gastove commented Jun 29, 2014

call-fn works well with public, non-static methods; it explodes on static methods. I've also been trying http://clojuredocs.org/clojure_contrib/clojure.contrib.reflect/call-method, which throws IllegalArgumentException array element type mismatch java.lang.reflect.Array.set (Array.java:-2) Which I do not at all understand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment