Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 725
Jeremy Avatar asked Nov 27 '10 23:11

Jeremy


People also ask

How do you call a method 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.

What are primitive methods in Java?

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.

Are primitive types classes 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.

Do primitive types have methods Java?

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.


1 Answers

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)>
like image 92
Jeremy Avatar answered Sep 20 '22 14:09

Jeremy