Skip to content

Instantly share code, notes, and snippets.

@0b1kn00b
Last active February 12, 2025 13:16
Show Gist options
  • Save 0b1kn00b/23f81b6482f1524bd45b57d3651edce0 to your computer and use it in GitHub Desktop.
Save 0b1kn00b/23f81b6482f1524bd45b57d3651edce0 to your computer and use it in GitHub Desktop.
(ns ron.reflect "reflection utils")
(require '[clojure.repl :refer [doc]])
(require '[clojure.java.io :as io])
(require '[babashka.classpath :as cp])
(require '[clojure.reflect :as cr])
(require '[clojure.pprint :as pp])
(require '[clojure.string :as str])
(require '[clojure.tools.namespace.find :as fns])
(import
(java.util.jar JarFile ))
(defn methods_map [sym]
(let [
map_keys (keys (ns-publics sym))
syms (map (fn [k] (symbol (name sym) (name k))) map_keys)
]
(zipmap (map keyword map_keys) syms)
))
(defn methods [sym] (keys (methods_map sym)))
(defn method [sym name] (-> (methods_map sym) (get name)))
(defn classpath [] cp/get-classpath)
(defn type.show [type] (->> type
cr/reflect
:members
(filter #(contains? (:flags %) :public))
pp/print-table))
(defn classpaths [] (str/split (cp/get-classpath) #":"))
(defn categorise- [s]
(let [is_jar (str/ends-with? s ".jar")]
{:is_jar is_jar
:location s}))
(defn get_ns_for_classpath [str]
(let
[cp (categorise- str)
fl (io/as-file (get cp :location))
ns (if (get cp :is_jar) (fns/find-namespaces-in-jarfile (JarFile. fl)) (fns/find-namespaces-in-dir fl))]
(assoc cp :ns ns)))
(defn namespaces.get []
(let
[cps (map (fn [x] (-> (get_ns_for_classpath x) :ns)) (classpaths))]
(map name (reduce
(fn [memo,next]
(concat memo next)) [] cps))))
(defn eval-doc
[sym]
(eval `(doc ~sym)))
(defn namespace.get [head] (filter (fn [x] (str/starts-with? x head)) (namespaces.get)))
(defn docs
([sym] (eval-doc sym) (methods sym))
([sym name] (let [method_name (method sym name)]
(eval-doc method_name)
))
)
@0b1kn00b
Copy link
Author

neil dep add clojure/tools.namespace

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