How can I get the methods of a Java class from Clojure?

前端 未结 6 551
予麋鹿
予麋鹿 2020-12-12 10:56

How can I get the methods of a Java class from Clojure?

6条回答
  •  攒了一身酷
    2020-12-12 11:30

    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))
    

    Usage:

     (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)
    

提交回复
热议问题