title: Extending Dropwizard Service class in Clojure
body: <p>I've been using Dropwizard to write RESTful APIs in Scala for a while now, and I'd really like to use it in Clojure.</p>
  
  <p>What I am trying to do is create a subclass of <code>com.yammer.dropwizard.Service</code>, which I can instantiate and run in my <code>-main</code>, and I am struggling to make this happen.</p>
  
  <p>So far as I know, my options are:</p>
  
  <ol>
  <li><code>gen-class</code> in <code>ns</code> declaration. Not feasible because I want to build macros (such as <code>defservice</code>), which I can't use in the <code>ns</code> declaration. Unless there's some way to generate a thing at runtime and pass it to <code>compile</code>, but that seems like a filthy hack.</li>
  <li><p><code>gen-class</code> outside <code>ns</code>. Despite <a href="http://kotka.de/blog/2010/02/gen-class_how_it_works_and_how_to_use_it.html" rel="nofollow">some code I found</a>, it seems that this doesn't work at all. Here's a simple example:</p>
  
  <pre><code>user&gt; (gen-class :name foo :extends Object)
  nil
  user&gt; (foo.)
  Unable to resolve classname: foo
   [Thrown class java.lang.IllegalArgumentException]
  </code></pre>
  
  <p>I am aware that <code>gen-class</code> only works at compile time. I dropped this into a file and put <code>(foo.</code> into my <code>-main</code>, and I got the same exception.</p></li>
  <li><p><code>proxy</code>. This seems to be the most promising, and it works for simple cases, but not when extending Dropwizard's <code>Service</code> class:</p>
  
  <pre><code>user&gt; (proxy [Object] [])
  #&lt;Object$0 user.proxy$java.lang.Object$0@249faafc&gt;
  user&gt; (import [com.yammer.dropwizard Service])
  com.yammer.dropwizard.Service
  user&gt; (proxy [Service] ["x"])
  java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
    [Thrown class java.lang.ClassCastException]
  </code></pre>
  
  <p>I think this may be due to <code>Service</code> taking a type parameter, but I could not find any information about dealing with this in Clojure. What I did find seemed to indicate that these were only meaningful to javac, and ignored by Clojure. So perhaps I am incorrect about why this is happening.</p></li>
  <li><p><code>deftype</code>. Doesn't work because <code>Service</code> is a class, and <code>deftype</code> only works on interfaces and protocols.</p></li>
  </ol>
  
  <p>I think I'm on the right track, but am missing some of the subtleties of how <code>gen-class</code> and/or <code>proxy</code> work, and the Clojure documentation is extremely terse. How can I extend this Java class in Clojure?</p>