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?
You can call a static method using (ClassName/methodName arguments) . However class is not a static method, it's a java keyword and you don't need it in clojure. To get the Class object associated with the String class, just use String . Save this answer.
Primitive types are the most basic data types available within the Java language. There are 8: boolean , byte , char , short , int , long , float and double . These types serve as the building blocks of data manipulation in Java.
Java provides a number of unary and binary operators for manipulating basic primitive values (e.g., 23*12 , ! true ). The rules and behaviors of these operators are unchanged. Because the basic primitive types are class types, they now have methods.
In a Java program, data always manifests itself as one of the eight primitive data types. Primitives simply represent a value, like the number seven or the boolean value of false. They have no methods and no complementary properties.
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)>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With