Is there any way of extending an abstract class in clojure? I wish to extend the java.lang.Classloader in clojure. Is there a good way to get a subclass of the Classloader object without going back into java?
One way to extend an abstract class in Clojure is to use :gen-class directive in the ns form or (gen-class) macro. For example:
(ns example.core
  (:gen-class
     :extends ClassLoader
     :name example.CustomClassLoader))
(defn -findClass [this name]
  (println "example.findClass")
  nil)
AOT compilation must be used in order for (gen-class) to have any effect. See (gen-class) in Clojure API documentation.
Note: this approach has already been suggested by A. Webb in a comment to another answer.
Yes. Provide the abstract class to the proxy macro, like this:
(-> (proxy [ClassLoader] []
      (findResource [name]
        (java.net.URL. "http://somewhere")))
    (.findResource "doot"))
--> #object[java.net.URL 0x75dc3e1d "http://somewhere"]
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With