sem*_*ros 59
[编辑2]
Per M Smith在下面的评论中,这完成了相同的但是提供了按方法名称排序并且只返回方法:
(print-table
(sort-by :name
(filter :exception-types (:members (r/reflect "foo")))))
Run Code Online (Sandbox Code Playgroud)
[/编辑2]
[编辑]
我的原始答案是指Clojure 1.2,但Clojure 1.3已经改变了.现在可以在不依赖Clojure的contrib库的情况下工作:
(require '[clojure.reflect :as r])
(use '[clojure.pprint :only [print-table]])
(print-table (:members (r/reflect "foo")))
Run Code Online (Sandbox Code Playgroud)
这提供了一种更加分离的方法,该reflect函数提供了关于传入的参数的各种信息(在本例中为a String "foo"),并且该print-table函数采用任何通用的表格数据结构并且相当地打印它.
这最初来自谷歌集团的这个主题.
[/编辑]
我将show在clojure.contrib.repl-utils命名空间中使用该函数,该函数将打印对象(或对象的类)的所有静态和实例成员.我这样要求:
(require '[clojure.contrib.repl-utils :as ru])
Run Code Online (Sandbox Code Playgroud)
以下是使用Joda Time的示例:
(import 'org.joda.time.DateTime)
(ru/show DateTime)
(ru/show (DateTime.))
Run Code Online (Sandbox Code Playgroud)
第一个示例演示了如何简单地将类传递给show,而第二个示例演示了您也可以传递类的实例.
这当然适用于下面是Java类的许多Clojure项目.以下是查看java.lang.String实例可用的所有方法的示例:
(ru/show "foo")
Run Code Online (Sandbox Code Playgroud)
Stu*_*rra 24
尝试使用最近的Clojure 1.3.0-alpha*版本中的clojure.reflect.它返回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}}
...
Run Code Online (Sandbox Code Playgroud)
zca*_*ate 21
您可以使用此方法使用clojure.reflect并扩展以前的答案:
(use 'clojure.reflect)
(defn all-methods [x]
(->> x reflect
:members
(filter :return-type)
(map :name)
sort
(map #(str "." %) )
distinct
println))
Run Code Online (Sandbox Code Playgroud)
用法:
(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)
Run Code Online (Sandbox Code Playgroud)
dby*_*rne 15
此代码将打印声明和继承的所有公共方法.
(doseq [m (.getMethods (type "Hello"))]
(println "Method Name: " (.getName m))
(println "Return Type: " (.getReturnType m) "\n"))
Run Code Online (Sandbox Code Playgroud)
这将返回声明方法的Java数组:
(:declaredMethods (bean String))
(seq (:declaredMethods (bean String)))
Run Code Online (Sandbox Code Playgroud)
优点是bean在clojure.core中