java

2024-11-04T16:47:17.773229Z

Just checking, if I want to get all of the "methods as values" for a particular class, is there some shortcut to doing that without making a string + read/eval? This works, just wondering if there's a better way

(let [clazz clojure.lang.PersistentHashMap]
    (->> (.getDeclaredMethods clazz)
         (filter #(let [m (.getModifiers %)]
                    (and (not (Modifier/isStatic m))
                         (Modifier/isPublic m))))
         (map (fn [m]
                (eval
                  (with-meta
                    (read-string
                      (str
                        (-> (.getDeclaringClass m)
                            .getCanonicalName)
                        "/."
                        (.getName m)))
                    {:param-tags (mapv #(symbol
                                          (.getCanonicalName %))
                                   (.getParameterTypes m))}))))
         ))

Alex Miller (Clojure team) 2024-11-04T16:56:37.327229Z

you can use the symbol function to make a symbol and then with-meta to attach metadata

Alex Miller (Clojure team) 2024-11-04T16:59:26.616029Z

I guess you still need eval at the end to evaluate a symbol to method value

2024-11-04T17:00:25.779329Z

oh yeah, that was a slip. But there's no built in way to enumerate them all for some class right?

Alex Miller (Clojure team) 2024-11-04T17:00:52.972879Z

well, you can use clojure.reflect if you want to work in data

Alex Miller (Clojure team) 2024-11-04T17:02:57.245699Z

(clojure.reflect/reflect clojure.lang.PersistentMap)
will give you a set of members like:
{:name valAt,
    :return-type java.lang.Object,
    :declaring-class clojure.lang.PersistentHashMap,
    :parameter-types [java.lang.Object java.lang.Object],
    :exception-types [],
    :flags #{:public}}

2024-11-04T17:03:02.730879Z

yeah that's working for me now and get's the job done. Just was wondering if there was a shortcut to get the method values themselves without building them

Alex Miller (Clojure team) 2024-11-04T17:03:05.214829Z

which you can filter etc

Alex Miller (Clojure team) 2024-11-04T17:03:27.034559Z

I think the only way to get to a method value is by evaling a qualified method symbol

👍 1