Search code examples
javaclojurejava-interop

How do you refer to primitive Java types in Clojure?


I'd like to use reflection to get a method of a Java object from Clojure. One of the argument types is a Java primitive and I don't know how to refer to them from Clojure.

For example, say I wanted to get String.valueOf(boolean). My nearest guess would be to do

(.getDeclaredMethod String "valueOf" (into-array [Boolean]))

but this fails because Boolean is not the primitive type itself, but the boxed version. I've tried boolean, but that refers to a built-in Clojure function, and bool is undefined.

How do I refer to a primitive Java type in Clojure?


Solution

  • You can refer to the primitive types through the TYPE property of their boxed equivalent. For example:

    user=> (.getDeclaredMethod String "valueOf" (into-array [Boolean/TYPE]))
    #<Method public static java.lang.String java.lang.String.valueOf(boolean)>