Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to explore Java methods through REPL

I am playing with Clojure and Java Interop and I came up with the following to inspect the methods of some instance:

(defn methods-of [instance & [string]]
  {:pre [(nil? instance)]}
  (filter #(re-find (re-pattern (or string #".*")) %) (map #(.getName %) (-> instance class .getMethods))))

But I was wondering, given that this is a very practical way to get this info from REPL, if there was some library out there to achieve this (and more).

like image 334
Andrea Richiardi Avatar asked Dec 25 '22 05:12

Andrea Richiardi


2 Answers

Clojure comes with a namespace that helps with reflection: clojure.reflect. The main function to use is clojure.reflect/reflect.

user> (require '[clojure.reflect :as reflect])
nil
user> (clojure.pprint/pprint (reflect/reflect (java.util.Date.)))
{:bases
 #{java.io.Serializable java.lang.Comparable java.lang.Object
   java.lang.Cloneable},
 :flags #{:public},
 :members
 #{{:name getSeconds,
    :return-type int,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:public}}
   {:name serialVersionUID,
    :type long,
    :declaring-class java.util.Date,
    :flags #{:private :static :final}}
   {:name getCalendarDate,
    :return-type sun.util.calendar.BaseCalendar$Date,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:private :final}}
   {:name after,
    :return-type boolean,
    :declaring-class java.util.Date,
    :parameter-types [java.util.Date],
    :exception-types [],
    :flags #{:public}}
   {:name hashCode,
    :return-type int,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:public}}
   {:name wtb,
    :type java.lang.String<>,
    :declaring-class java.util.Date,
    :flags #{:private :static :final}}
   {:name setMonth,
    :return-type void,
    :declaring-class java.util.Date,
    :parameter-types [int],
    :exception-types [],
    :flags #{:public}}
   {:name getMonth,
    :return-type int,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:public}}
   {:name getCalendarSystem,
    :return-type sun.util.calendar.BaseCalendar,
    :declaring-class java.util.Date,
    :parameter-types [int],
    :exception-types [],
    :flags #{:private :static :final}}
   {:name fastTime,
    :type long,
    :declaring-class java.util.Date,
    :flags #{:transient :private}}
   {:name defaultCenturyStart,
    :type int,
    :declaring-class java.util.Date,
    :flags #{:private :static}}
   {:name writeObject,
    :return-type void,
    :declaring-class java.util.Date,
    :parameter-types [java.io.ObjectOutputStream],
    :exception-types [java.io.IOException],
    :flags #{:private}}
   {:name getMillisOf,
    :return-type long,
    :declaring-class java.util.Date,
    :parameter-types [java.util.Date],
    :exception-types [],
    :flags #{:static :final}}
   {:name getMinutes,
    :return-type int,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:public}}
   {:name ttb,
    :type int<>,
    :declaring-class java.util.Date,
    :flags #{:private :static :final}}
   {:name setDate,
    :return-type void,
    :declaring-class java.util.Date,
    :parameter-types [int],
    :exception-types [],
    :flags #{:public}}
   {:name setSeconds,
    :return-type void,
    :declaring-class java.util.Date,
    :parameter-types [int],
    :exception-types [],
    :flags #{:public}}
   {:name java.util.Date,
    :declaring-class java.util.Date,
    :parameter-types [long],
    :exception-types [],
    :flags #{:public}}
   {:name getTimeImpl,
    :return-type long,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:private :final}}
   {:name setYear,
    :return-type void,
    :declaring-class java.util.Date,
    :parameter-types [int],
    :exception-types [],
    :flags #{:public}}
   {:name normalize,
    :return-type sun.util.calendar.BaseCalendar$Date,
    :declaring-class java.util.Date,
    :parameter-types [sun.util.calendar.BaseCalendar$Date],
    :exception-types [],
    :flags #{:private :final}}
   {:name getHours,
    :return-type int,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:public}}
   {:name getYear,
    :return-type int,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:public}}
   {:name java.util.Date,
    :declaring-class java.util.Date,
    :parameter-types [int int int int int int],
    :exception-types [],
    :flags #{:public}}
   {:name java.util.Date,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:public}}
   {:name UTC,
    :return-type long,
    :declaring-class java.util.Date,
    :parameter-types [int int int int int int],
    :exception-types [],
    :flags #{:static :public}}
   {:name getTime,
    :return-type long,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:public}}
   {:name getJulianCalendar,
    :return-type sun.util.calendar.BaseCalendar,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:synchronized :private :static :final}}
   {:name getCalendarSystem,
    :return-type sun.util.calendar.BaseCalendar,
    :declaring-class java.util.Date,
    :parameter-types [sun.util.calendar.BaseCalendar$Date],
    :exception-types [],
    :flags #{:private :static :final}}
   {:name setTime,
    :return-type void,
    :declaring-class java.util.Date,
    :parameter-types [long],
    :exception-types [],
    :flags #{:public}}
   {:name setMinutes,
    :return-type void,
    :declaring-class java.util.Date,
    :parameter-types [int],
    :exception-types [],
    :flags #{:public}}
   {:name cdate,
    :type sun.util.calendar.BaseCalendar$Date,
    :declaring-class java.util.Date,
    :flags #{:transient :private}}
   {:name getDay,
    :return-type int,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:public}}
   {:name toString,
    :return-type java.lang.String,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:public}}
   {:name java.util.Date,
    :declaring-class java.util.Date,
    :parameter-types [int int int int int],
    :exception-types [],
    :flags #{:public}}
   {:name java.util.Date,
    :declaring-class java.util.Date,
    :parameter-types [int int int],
    :exception-types [],
    :flags #{:public}}
   {:name toInstant,
    :return-type java.time.Instant,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:public}}
   {:name clone,
    :return-type java.lang.Object,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:public}}
   {:name from,
    :return-type java.util.Date,
    :declaring-class java.util.Date,
    :parameter-types [java.time.Instant],
    :exception-types [],
    :flags #{:static :public}}
   {:name compareTo,
    :return-type int,
    :declaring-class java.util.Date,
    :parameter-types [java.util.Date],
    :exception-types [],
    :flags #{:public}}
   {:name toLocaleString,
    :return-type java.lang.String,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:public}}
   {:name convertToAbbr,
    :return-type java.lang.StringBuilder,
    :declaring-class java.util.Date,
    :parameter-types [java.lang.StringBuilder java.lang.String],
    :exception-types [],
    :flags #{:private :static :final}}
   {:name getTimezoneOffset,
    :return-type int,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:public}}
   {:name toGMTString,
    :return-type java.lang.String,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:public}}
   {:name normalize,
    :return-type sun.util.calendar.BaseCalendar$Date,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:private :final}}
   {:name parse,
    :return-type long,
    :declaring-class java.util.Date,
    :parameter-types [java.lang.String],
    :exception-types [],
    :flags #{:static :public}}
   {:name jcal,
    :type sun.util.calendar.BaseCalendar,
    :declaring-class java.util.Date,
    :flags #{:private :static}}
   {:name gcal,
    :type sun.util.calendar.BaseCalendar,
    :declaring-class java.util.Date,
    :flags #{:private :static :final}}
   {:name before,
    :return-type boolean,
    :declaring-class java.util.Date,
    :parameter-types [java.util.Date],
    :exception-types [],
    :flags #{:public}}
   {:name compareTo,
    :return-type int,
    :declaring-class java.util.Date,
    :parameter-types [java.lang.Object],
    :exception-types [],
    :flags #{:synthetic :bridge :public}}
   {:name getDate,
    :return-type int,
    :declaring-class java.util.Date,
    :parameter-types [],
    :exception-types [],
    :flags #{:public}}
   {:name readObject,
    :return-type void,
    :declaring-class java.util.Date,
    :parameter-types [java.io.ObjectInputStream],
    :exception-types
    [java.io.IOException java.lang.ClassNotFoundException],
    :flags #{:private}}
   {:name setHours,
    :return-type void,
    :declaring-class java.util.Date,
    :parameter-types [int],
    :exception-types [],
    :flags #{:public}}
   {:name getCalendarSystem,
    :return-type sun.util.calendar.BaseCalendar,
    :declaring-class java.util.Date,
    :parameter-types [long],
    :exception-types [],
    :flags #{:private :static :final}}
   {:name equals,
    :return-type boolean,
    :declaring-class java.util.Date,
    :parameter-types [java.lang.Object],
    :exception-types [],
    :flags #{:public}}
   {:name java.util.Date,
    :declaring-class java.util.Date,
    :parameter-types [java.lang.String],
    :exception-types [],
    :flags #{:public}}}}
nil
user> 

Sometimes, it's easier to use bean to see the various fields present.

user> (clojure.pprint/pprint (bean (java.util.Date.)))
{:seconds 9,
 :date 30,
 :class java.util.Date,
 :minutes 13,
 :hours 8,
 :year 114,
 :timezoneOffset 420,
 :month 7,
 :day 6,
 :time 1409411589031}
nil

Using reflect/reflect to list all method names (as you were doing in your example):

user> (->> (reflect/reflect (java.util.Date.)) :members (filter :return-type) (map :name))
(getSeconds getCalendarDate after hashCode setMonth getMonth getCalendarSystem writeObject getMillisOf getMinutes setDate setSeconds getTimeImpl setYear normalize getHours getYear UTC getTime getJulianCalendar getCalendarSystem setTime setMinutes getDay toString toInstant clone from compareTo toLocaleString convertToAbbr getTimezoneOffset toGMTString normalize parse before compareTo getDate readObject setHours getCalendarSystem equals)
like image 116
noisesmith Avatar answered Jan 05 '23 11:01

noisesmith


Have a look at iroh.

> (.? String  #"^conta") ;; grep all fields/methods that start with conta
(#[contains :: (java.lang.String, java.lang.CharSequence) -> boolean])
> (.? String  #"^con" :name) ;; return just the name of the method/field
("concat" "contains" "contentEquals")
> (.? "string instance"  #"^con" :name) ;; works with instances also
("concat" "contains" "contentEquals")
> (.? "a string instance" :static :name :public :method) ; all public static methods
("copyValueOf" "format" "join" "valueOf")
> (.? "a string instance" #"^t" :instance :name :public :method) ;; all public instance methods that start with t
("toCharArray" "toLowerCase" "toString" "toUpperCase" "trim")
like image 36
DanLebrero Avatar answered Jan 05 '23 11:01

DanLebrero