首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从Clojure获取Java类的方法?

如何从Clojure获取Java类的方法?
EN

Stack Overflow用户
提问于 2011-04-28 23:42:11
回答 5查看 17.7K关注 0票数 65

如何从Clojure获取Java类的方法?

EN

回答 5

Stack Overflow用户

发布于 2011-05-04 08:48:50

试试clojure.reflect,它在最近的clojure1.3.0-alpha*版本中可用。它返回Clojure数据结构,您可以根据需要进行搜索/过滤。

代码语言:javascript
复制
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}}
...
票数 25
EN

Stack Overflow用户

发布于 2012-06-30 06:28:28

您可以使用此方法,该方法使用clojure.reflect并扩展前面的答案:

代码语言:javascript
复制
(use 'clojure.reflect)

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

用法:

代码语言:javascript
复制
 (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)
票数 21
EN

Stack Overflow用户

发布于 2011-04-28 23:44:42

这段代码将打印所有的公共方法,包括声明的和继承的。

代码语言:javascript
复制
(doseq [m (.getMethods (type "Hello"))]
  (println "Method Name: " (.getName m))
  (println "Return Type: " (.getReturnType m) "\n"))
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5821286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档