Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending an java abstract class with clojure (java.lang.Classloader)

Tags:

clojure

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?

like image 479
zcaudate Avatar asked Mar 21 '14 10:03

zcaudate


2 Answers

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.

like image 93
ez121sl Avatar answered Sep 18 '22 12:09

ez121sl


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"]
like image 37
jbouwman Avatar answered Sep 21 '22 12:09

jbouwman