Last active
August 29, 2015 14:03
-
-
Save Gastove/b083083f22d7f198d964 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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. |
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
Worth pointing out that
parseFrom
dispatches on both arity and type; there are something like six differentparseFrom
methods on this object.