Wow, using what you just explained, I am now able to reload .class
files.

(defn reload-class [classname]
  (.defineClass (clojure.lang.DynamicClassLoader.)
                classname
                (to-byte-array (io/file 
"/home/user/dj/usr/src/scratch/src/scratch/
hello.class"))
                nil))
(def x (reload-class "scratch.hello"))
(def xo (.newInstance x))
(.hi xo) ;;=> "hello world"

;; modify hello.java and compile with javac

(def y (reload-class "scratch.hello"))
(def yo (.newInstance y))
(.hi yo) ;;=> "hillo wirld"

-Brent


On Sep 22, 5:19 pm, Chouser <chou...@gmail.com> wrote:
> On Thu, Sep 22, 2011 at 2:56 PM, Kevin Downey <redc...@gmail.com> wrote:
> > most likely the compiler is creating a new DynamicClassLoader, it uses
> > a var clojure.lang.Compiler/LOADER and pushes and pops class loaders
> > from there.
>
> You nailed it.
>
> It looks like each top-level form currently gets its own fresh
> DynamicClassLoader.
>
> (do
>   (deftype C [])
>   (def x1 (C.))
>   (deftype C [a])
>   (def x2 (C. 42)))
>
> (.getClassLoader (class x1))
> ;=> #<DynamicClassLoader clojure.lang.DynamicClassLoader@3e5b38d7>
>
> (.getClassLoader (class x2))
> ;=> #<DynamicClassLoader clojure.lang.DynamicClassLoader@727f3b8a>
>
> Note the numbers after the @s are different -- each got its own
> classloader.  This is not exclusive to deftype of course:
>
> (do (def a #()) (def b #()))
>
> (.getClassLoader (class a))
> ;=> #<DynamicClassLoader clojure.lang.DynamicClassLoader@638bd7f1>
>
> (.getClassLoader (class b))
> ;=> #<DynamicClassLoader clojure.lang.DynamicClassLoader@581de498>
>
> --Chouser

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to