Use java annotations in clojure

Viewed 609
a.b(new C() {
  @Handler
  public void d() {
    System.out.println("hello world");
  }
});

I tried to translated it to clojure:

(.b a (proxy [C] []
  (^{Handler {}}
    d [] (println "hello world"))))

But unfortunately it doesn't work.

Is this correct? How should I add java annotation to a method?

2 Answers

I generally suggest writing your interop code in Java: present an API that fits what Java wants, and implement it with calls to your Clojure functions. The Clojure->Java translation layer is just not that rich for creating types that are not just reify or defrecord, and I find it a big burden to do anything fancy, like handle annotations, or extend classes.

There's documentation for this here

Based on that I'd try something along the lines of

(deftype ^{Handler true} CC [] ...)
(.b a (CC.))

There's no mention of proxy/reify support, that's why I'm creating a new class explicitly.

Related