SHARE
TWEET

Untitled

a guest Feb 27th, 2012 29 Never
  1. (ns strangeness.fn-arity
  2.   (:import java.lang.reflect.Method))
  3.  
  4. (defn declared-methods [^Class class]
  5.   (filter #(= class (.getDeclaringClass ^Method %))
  6.           (.getMethods class)))
  7.  
  8. (defn has-arity? [fun arity]
  9.   (let [cls (class fun)
  10.         methods (declared-methods cls)]
  11.     (or (when (instance? clojure.lang.RestFn fun)
  12.           (<= (.getRequiredArity ^clojure.lang.RestFn fun) arity))
  13.         (boolean (some (fn [^Method m]
  14.                          (and (= "invoke" (.getName m))
  15.                               (= (count (.getParameterTypes m)) arity)))
  16.                        methods)))))
RAW Paste Data
Top