如何从Clojure获取Java类的方法?
发布于 2011-05-04 08:48:50
试试clojure.reflect,它在最近的clojure1.3.0-alpha*版本中可用。它返回Clojure数据结构,您可以根据需要进行搜索/过滤。
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}}
...
发布于 2012-06-30 06:28:28
您可以使用此方法,该方法使用clojure.reflect并扩展前面的答案:
(use 'clojure.reflect)
(defn all-methods [x]
(->> x reflect
:members
(filter :return-type)
(map :name)
sort
(map #(str "." %) )
distinct
println))
用法:
(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)
发布于 2011-04-28 23:44:42
这段代码将打印所有的公共方法,包括声明的和继承的。
(doseq [m (.getMethods (type "Hello"))]
(println "Method Name: " (.getName m))
(println "Return Type: " (.getReturnType m) "\n"))
https://stackoverflow.com/questions/5821286
复制相似问题