chapter twenty one

21 Java Interoperation

 

The functions and macros in this chapter are predominantly related to Java interoperation, the part of the standard library that sits closer to the Java runtime. Clojure still offers a functional view over Java’s mutable world offering a syntax that is concise and readable. The following is a summary of what is described in the chapter:

  • dot "." is a versatile special form to access Java methods and attributes. double-dot ".." is useful to assemble chained calls while "doto" concatenates side-effecting calls instead.
  • new creates a new instance of an object.
  • try and catch (and related special forms finally and throw) are the fundamental mechanism for exception handling in Clojure.
  • ex-info and ex-data builds on top of Java exception mechanism by hiding some of the low level syntax and offering a mechanism to transport data with exceptions.
  • bean is a macro that wraps Java objects in map-like interfaces.
  • The functions in the clojure.reflect namespace perform introspection on the structure and content of Java classes.
  • Clojure also has a rich set of functions to deal with Java arrays. make-array is just one of the many ways to create an array from Clojure. Macros like aset, amap or areduce are useful to transform the content of the array. The section also contains a brief explanation of array macro variants to leverage primitive types (int, boolean, byte and so on).

21.1  ".", ".." and doto

21.2   new

21.3  try, catch, finally and throw

21.4  ex-info and ex-data

21.5  bean

21.6  reflect and type-reflect

21.7  Java Arrays

21.7.1  make-array

21.7.2  object-array and other typed initializers

21.7.3  to-array, into-array, to-array-2d

21.7.4  aget, aset, alength and aclone

21.7.5  amap and areduce

21.7.6  set-int and other types setters

21.7.7  ints and other typed array casting