This post originated from an RSS feed registered with Ruby Buzz
by Jay Fields.
Original Post: Clojure: Using Java Inner Classes
Feed Title: Jay Fields Thoughts
Feed URL: http://blog.jayfields.com/rss.xml
Feed Description: Thoughts on Software Development
Recently, I wanted to add some specific behavior to my Clojure application when any uncaught exception occurred. This is easy enough by calling the setDefaultUncaughtExceptionHandler (sDUEH); however, it's worth noting that the sDUEH takes a Thread.UncaughtExceptionHandler argument. Clojure has no problem giving you access to inner classes, but the syntax is slightly different: Outer$Inner in Clojure is the same as Outer.Inner in Java.
It's easy to use the REPL and determine if you have things correct.
This works without issue, because java.lang is (generally) available in Clojure.
If you want access to an inner class that is not in java.lang you'll want to import it directly. For example, the following REPL session shows how you can access the ThreadPoolExecutor.DiscardPolicy class.
user=> ThreadPoolExecutor$DiscardPolicy java.lang.Exception: Unable to resolve symbol: ThreadPoolExecutor$DiscardPolicy in this context (NO_SOURCE_FILE:0) user=> (ns my-ns (:import [java.util.concurrent ThreadPoolExecutor$DiscardPolicy])) java.util.concurrent.ThreadPoolExecutor$DiscardPolicy my-ns=> ThreadPoolExecutor$DiscardPolicy java.util.concurrent.ThreadPoolExecutor$DiscardPolicy
Returning the entire class in the REPL should suffice as proof that everything is working fine; however, the code below is a brief example of using Thread$UncaughtExceptionHandler in a larger context.