Skip to content

Instantly share code, notes, and snippets.

@christianromney
Last active November 30, 2016 16:19
Show Gist options
  • Save christianromney/6c8175b7fab58f1efeae97c336fe51c0 to your computer and use it in GitHub Desktop.
Save christianromney/6c8175b7fab58f1efeae97c336fe51c0 to your computer and use it in GitHub Desktop.
A handy little Clojure utility function to print the members of an object.
(defn inspect
"Reflects the members of an object and prints them. Returns the
number of members on the object. Constructors are labeled
with [ctor], public members are prefixed with +, private members are
prefixed with -, and static members are listed as Class/member.
Fields are annotated as fieldName : type, functions have a parameter
list enclosed in parentheses (param1, param2) followed by an arrow
-> and the return type. Varargs have an elipsis following the last
param (params...)."
[x]
(letfn [(shorten
[s]
(-> s clojure.core/name (clojure.string/split #"\.") last))
(format-name
[{:keys [name flags declaring-class] :as data}]
(if-not (flags :static)
(shorten name)
(format "%s/%s" (shorten declaring-class) name)))
(format-return
[{:keys [type return-type]}]
(cond
return-type
(str " -> " return-type)
type
(str " : " type)
:else
" [ctor]"))
(format-params
[{:keys [flags parameter-types return-type]}]
(cond
(seq parameter-types)
(str "("
(apply str (interpose ", " parameter-types))
(if (flags :varargs) "..." "")
")")
return-type
"()"
:else
""))
(format-visibility
[{:keys [flags]}]
(cond
(flags :public)
"+"
(flags :private)
"-"
:else
" "))
(format-method
[{:keys [return-type parameter-types flags] :as data}]
(->> data
((juxt format-visibility
format-name
format-params
format-return))
(apply str)))]
(let [type-name (str "Type: " (.getName (class x)))
methods (->> x clojure.reflect/reflect :members (map format-method) sort)]
(println type-name)
(println (apply str (repeat (count type-name) "-")))
(doseq [m methods]
(println m))
(count methods))))
@christianromney
Copy link
Author

christianromney commented Nov 30, 2016

Sample output for (inspect (java.nio.file.Paths/get (java.net.URI. "file:///home/christian"))):

Type: sun.nio.fs.UnixPath
-------------------------
 UnixPath(sun.nio.fs.UnixFileSystem, byte<>) [ctor]
 UnixPath(sun.nio.fs.UnixFileSystem, java.lang.String) [ctor]
 UnixPath/$assertionsDisabled : boolean
 UnixPath/normalizeAndCheck(java.lang.String) -> java.lang.String
 UnixPath/toUnixPath(java.nio.file.Path) -> sun.nio.fs.UnixPath
 asByteArray() -> byte<>
 checkDelete() -> void
 checkRead() -> void
 checkWrite() -> void
 getByteArrayForSysCalls() -> byte<>
 getPathForExceptionMessage() -> java.lang.String
 getPathForPermissionCheck() -> java.lang.String
 openForAttributeAccess(boolean) -> int
 resolve(byte<>) -> sun.nio.fs.UnixPath
+compareTo(java.lang.Object) -> int
+compareTo(java.nio.file.Path) -> int
+endsWith(java.nio.file.Path) -> boolean
+equals(java.lang.Object) -> boolean
+getFileName() -> java.nio.file.Path
+getFileName() -> sun.nio.fs.UnixPath
+getFileSystem() -> java.nio.file.FileSystem
+getFileSystem() -> sun.nio.fs.UnixFileSystem
+getName(int) -> java.nio.file.Path
+getName(int) -> sun.nio.fs.UnixPath
+getNameCount() -> int
+getParent() -> java.nio.file.Path
+getParent() -> sun.nio.fs.UnixPath
+getRoot() -> java.nio.file.Path
+getRoot() -> sun.nio.fs.UnixPath
+hashCode() -> int
+isAbsolute() -> boolean
+normalize() -> java.nio.file.Path
+register(java.nio.file.WatchService, java.nio.file.WatchEvent$Kind<>, java.nio.file.WatchEvent$Modifier<>...) -> java.nio.file.WatchKey
+relativize(java.nio.file.Path) -> java.nio.file.Path
+relativize(java.nio.file.Path) -> sun.nio.fs.UnixPath
+resolve(java.nio.file.Path) -> java.nio.file.Path
+resolve(java.nio.file.Path) -> sun.nio.fs.UnixPath
+startsWith(java.nio.file.Path) -> boolean
+subpath(int, int) -> java.nio.file.Path
+subpath(int, int) -> sun.nio.fs.UnixPath
+toAbsolutePath() -> java.nio.file.Path
+toAbsolutePath() -> sun.nio.fs.UnixPath
+toRealPath(java.nio.file.LinkOption<>...) -> java.nio.file.Path
+toString() -> java.lang.String
+toUri() -> java.net.URI
-UnixPath/checkNotNul(java.lang.String, char) -> void
-UnixPath/encode(sun.nio.fs.UnixFileSystem, java.lang.String) -> byte<>
-UnixPath/encoder : java.lang.ThreadLocal
-UnixPath/normalize(java.lang.String, int, int) -> java.lang.String
-UnixPath/resolve(byte<>, byte<>) -> byte<>
-emptyPath() -> sun.nio.fs.UnixPath
-fs : sun.nio.fs.UnixFileSystem
-hash : int
-initOffsets() -> void
-isEmpty() -> boolean
-offsets : int<>
-path : byte<>
-stringValue : java.lang.String
58

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment