【发布时间】:2011-11-02 19:22:11
【问题描述】:
我在 Clojure 中使用什么函数来查看 Java 对象的方法?
user=> (some-function some-java-object)
... lots of methods ...
【问题讨论】:
我在 Clojure 中使用什么函数来查看 Java 对象的方法?
user=> (some-function some-java-object)
... lots of methods ...
【问题讨论】:
从 1.3 版开始,Clojure 与 clojure.reflect 命名空间捆绑在一起。函数reflect 特别可用于显示对象的所有方法(和其他信息)。使用起来不如show方便。另一方面,它更通用,使用reflect 作为构建块很容易编写自己的show 版本。
例如,如果您想查看返回 String 的 String 的所有方法:
user=> (use 'clojure.reflect)
user=> (use 'clojure.pprint)
user=> (->> (reflect "some object")
:members
(filter #(= (:return-type %) 'java.lang.String))
(map #(select-keys % [:name :parameter-types]))
print-table)
【讨论】:
使用 java 反射。
(.getClass myObject)
为您提供课程。要获取方法,
(.getMethods (.getClass myObject))
这为您提供了一系列方法。您可以将其视为一个序列;我可能会把它放到一个向量中,所以:
(vec (.getMethods (.getClass myObject)))
【讨论】:
user=> (map #(.getName %) (-> "foo" class .getMethods))
("equals" "toString" "hashCode" "compareTo" "compareTo" "indexOf" "indexOf" "indexOf" "indexOf" "valueOf" "valueOf" "valueOf" "valueOf" "valueOf" "valueOf" "valueOf" "valueOf" "valueOf" "length" "isEmpty" "charAt" "codePointAt" "codePointBefore" "codePointCount" "offsetByCodePoints" "getChars" "getBytes" "getBytes" "getBytes" "getBytes" "contentEquals" "contentEquals" "equalsIgnoreCase" "compareToIgnoreCase" "regionMatches" "regionMatches" "startsWith" "startsWith" "endsWith" "lastIndexOf" "lastIndexOf" "lastIndexOf" "lastIndexOf" "substring" "substring" "subSequence" "concat" "replace" "replace" "matches" "contains" "replaceFirst" "replaceAll" "split" "split" "toLowerCase" "toLowerCase" "toUpperCase" "toUpperCase" "trim" "toCharArray" "format" "format" "copyValueOf" "copyValueOf" "intern" "wait" "wait" "wait" "getClass" "notify" "notifyAll")
用你的对象替换“foo”。
【讨论】:
您曾经可以使用 show 来处理这类事情(例如,使用 clojure 1.2.0、clojure-contrib 1.2.0)。
(ns test.core
(:use [ clojure.contrib.repl-utils :only [show]]))
来自 REPL
(show Integer)
成功
=== public final java.lang.Integer ===
static MAX_VALUE : int
static MIN_VALUE : int
...
奇怪的是,我用 clojure 1.3.0 /clojure-contrib 1.2.0 尝试了这个,但没有成功。 doc 似乎也坏了。
【讨论】:
clojure.contrib 现已弃用!
IIRC 它不是内置的,但也很短--see this implementation。
(可能是现在。)
【讨论】:
您通常会列出这种方法,因为您正在寻找一种特定类型的方法……比如说类中的所有“get”类型的方法。以下是您可以为对象“obj”执行此操作的方法:
(filter #(re-find #"get" %) (map #(.getName %) (.. obj getClass getMethods)))
#"get" 是正则表达式对象,用于搜索名称中包含 get 的方法(根据您自己的需要进行自定义)。 map 表达式只是生成对象类中所有方法名称的序列; seq 被提供给匿名函数,它是传递给过滤器的第一个参数。
【讨论】: