How can I get Java class methods from Clojure?

How can I get Java class methods from Clojure?

+54
clojure
Apr 28 2018-11-11T00:
source share
6 answers

[EDIT 2]

In a commentary on M Smith below, this does the same, but provides sorting by method name and returns methods:

(print-table (sort-by :name (filter :exception-types (:members (r/reflect "foo"))))) 

[/ EDIT 2]

[EDIT]

My initial answer was about Clojure 1.2, but that all changed with Clojure 1.3. Now this works without any dependency on Clojure contributors:

 (require '[clojure.reflect :as r]) (use '[clojure.pprint :only [print-table]]) (print-table (:members (r/reflect "foo"))) 

This provides a much more decoupled approach, while the reflect function provides all kinds of information about the passed argument (in this case a String "foo" ) and the print-table function, which accepts any general table-based data structure and prints it as such.

This is from this thread in the google group .

[/ EDIT]

I would use the show function in the clojure.contrib.repl-utils namespace, which will print all static and instance elements for an object (or object class). I demand it like this:

 (require '[clojure.contrib.repl-utils :as ru]) 

Here is an example of using Joda Time:

 (import 'org.joda.time.DateTime) (ru/show DateTime) (ru/show (DateTime.)) 

The first example demonstrates how you can simply pass a class to show , and the second demonstrates that you can also pass an instance of the class.

This, of course, works for a variety of Clojure elements, which are the Java classes below. Here is an example of viewing all available methods for an instance of java.lang.String:

 (ru/show "foo") 
+52
Apr 28 '11 at 16:08
source share

Try clojure.reflect , available in recent releases of Clojure 1.3.0-alpha *. It returns Clojure data structures that you can search / filter as needed.

 Clojure 1.3.0-alpha6 user=> (use 'clojure.reflect 'clojure.pprint) nil user=> (pprint (reflect "hello")) {:bases #{java.io.Serializable java.lang.Comparable java.lang.Object java.lang.CharSequence}, :flags #{:public :final}, :members #{{:name valueOf, :return-type java.lang.String, :declaring-class java.lang.String, :parameter-types [boolean], :exception-types [], :flags #{:static :public}} ... 
+19
May 4 '11 at a.m.
source share

You can use this method that uses clojure.reflect and extends the previous answers:

 (use 'clojure.reflect) (defn all-methods [x] (->> x reflect :members (filter :return-type) (map :name) sort (map #(str "." %) ) distinct println)) 

Using:

  (all-methods "") ; => (.charAt .checkBounds .codePointAt .codePointBefore .codePointCount .compareTo .compareToIgnoreCase .concat .contains .contentEquals .copyValueOf .endsWith .equals .equalsIgnoreCase .format .getBytes .getChars .hashCode .indexOf .intern .isEmpty .lastIndexOf .length .matches .offsetByCodePoints .regionMatches .replace .replaceAll .replaceFirst .split .startsWith .subSequence .substring .toCharArray .toLowerCase .toString .toUpperCase .trim .valueOf) (all-methods 1) ; => (.bitCount .byteValue .compareTo .decode .doubleValue .equals .floatValue .getChars .getLong .hashCode .highestOneBit .intValue .longValue .lowestOneBit .numberOfLeadingZeros .numberOfTrailingZeros .parseLong .reverse .reverseBytes .rotateLeft .rotateRight .shortValue .signum .stringSize .toBinaryString .toHexString .toOctalString .toString .toUnsignedString .valueOf) (all-methods java.util.StringTokenizer) ; => (.countTokens .hasMoreElements .hasMoreTokens .isDelimiter .nextElement .nextToken .scanToken .setMaxDelimCodePoint .skipDelimiters) 
+15
Jun 29 '12 at 22:28
source share

This code will print all public methods declared and inherited.

 (doseq [m (.getMethods (type "Hello"))] (println "Method Name: " (.getName m)) (println "Return Type: " (.getReturnType m) "\n")) 
+9
Apr 28 '11 at 15:44
source share

this will return an array of Java declared methods:

 (:declaredMethods (bean String)) (seq (:declaredMethods (bean String))) 

the advantage of bean is in clojure.core

+4
Apr 7 '13 at 21:48
source share

Try my new library:

http://github.com/zcaudate/iroh

 (.? String #"^c" :name) ;;=> ["charAt" "checkBounds" "codePointAt" "codePointBefore" ;; "codePointCount" "compareTo" "compareToIgnoreCase". ;; "concat" "contains" "contentEquals" "copyValueOf"] 
+3
Feb 13 '14 at 11:37
source share



All Articles