Skip to content

Instantly share code, notes, and snippets.

@egamble
Last active May 16, 2023 12:41

Revisions

  1. egamble revised this gist Dec 4, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions private.clj
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,8 @@
    (. m (invoke obj (into-array Object args)))))


    ;; This function comes from clojure.contrib.reflect.
    ;; It allows calling any method whose arguments have class types, not primitive types.
    ;; This function comes from clojure.contrib.reflect. A version of it is also in https://github.com/arohner/clj-wallhack.
    ;; It allows calling any method whose arguments have class types, but not primitive types.

    (defn call-method
    "Calls a private or protected method.
  2. egamble revised this gist Dec 4, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions private.clj
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@
    (. m (setAccessible true))
    (. m (invoke obj (into-array Object args)))))


    ;; This function comes from clojure.contrib.reflect.
    ;; It allows calling any method whose arguments have class types, not primitive types.

  3. egamble created this gist Dec 4, 2013.
    23 changes: 23 additions & 0 deletions private.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    ;; This fn allows calling any method, as long as it's the first with that name in getDeclaredMethods().
    ;; Works even when the arguments are primitive types.

    (defn call-method
    [obj method-name & args]
    (let [m (first (filter (fn [x] (.. x getName (equals method-name)))
    (.. obj getClass getDeclaredMethods)))]
    (. m (setAccessible true))
    (. m (invoke obj (into-array Object args)))))

    ;; This function comes from clojure.contrib.reflect.
    ;; It allows calling any method whose arguments have class types, not primitive types.

    (defn call-method
    "Calls a private or protected method.
    params is a vector of classes which correspond to the arguments to the method
    obj is nil for static methods, the instance object otherwise.
    The method-name is given a symbol or a keyword (something Named)."
    [klass method-name params obj & args]
    (-> klass (.getDeclaredMethod (name method-name)
    (into-array Class params))
    (doto (.setAccessible true))
    (.invoke obj (into-array Object args))))