Hi,

An update to this question. While Chouser gave a good explanation
about the details behind proxy, reify, and gen-class, I feel that the
explanation behind deftype is incomplete. It's not clear how clojure
is able to reload deftype defined classes. Calling deftype eventually
leads to a call to clojure.lang.DynamicClassLoader/defineClass.
Calling deftype again leads to another call to defineClass, but doing
this manually results in a Linkage Error. What is happening underneath
here that allows clojure to do this with deftypes?

-Brent

On Sep 19, 9:07 am, Brent Millare <brent.mill...@gmail.com> wrote:
> (Note: I've copied my question from stackoverflow to get more looks in
> case there are people in here that are not on 
> stack.http://stackoverflow.com/questions/7471316/how-does-clojure-class-rel...
> I will sync the good answers)
>
> I've been reading code and documentation to try to understand how
> class reloading works in clojure. According to many websites, such 
> ashttp://tutorials.jenkov.com/java-reflection/dynamic-class-loading-rel...
> , whenever you load a class essentially you obtain the bytecode (via
> any data mechanism), convert the bytecode into an instance of class
> Class (via defineClass), and then resolve (link) the class via
> resolveClass. (Does defineClass implicitly call resolveClass?). Any
> given classloader is only allowed to link a class once. If it attempts
> to link an existing class, it does nothing. This creates a problem
> since you cannot link a newly instantiated class, therefore you have
> to create a new instance of a classloader everytime you reload a
> class.
>
> Going back to clojure, I tried examining the paths to load classes.
>
> In clojure, you can define new classes in multiple ways depending on
> what you want:
>
> Anonymous Class: reify proxy
>
> Named Class: deftype defrecord (which uses deftype under the hood) gen-
> class
>
> Ultimately, those codes point to clojure/src/jvm/clojure/lang/
> DynamicClassLoader.java
>
> where DynamicClassLoader/defineClass creates an instance with super's
> defineClass and then caches the instance. When you want to retrieve
> the class, clojure load with a call to forName which calls the
> classloader and DynamicClassLoader/findClass, which first looks in the
> cache before delegating to the super class (which is contrary to the
> way most normal classloaders work, where they delegate first, than try
> it themselves.) *********The important point of confusion is the
> following: forName is documented to link the class before it returns
> but this would imply you can not reload a class from the existing
> DynamicClassLoader and instead need to create a new
> DynamicClassLoader, however I don't see this in the code.******** I
> understand that proxy and reify define anonymous classes, so their
> names are different thus can be treated as if its a different class.
> However, for the named classes, this breaks down. In real clojure
> code, you can have references to the old version of the classes and
> references to the new version of the classes simultaneously, but
> attempts to create new class instances will be of the new version.
>
> Please explain how clojure is able to reload classes without creating
> new instances of DynamicClassLoader, if I can understand the mechanism
> to reload classes, I would like to extend this reloading functionality
> to java's .class files I may create using javac.
>
> Notes: This question refers to class RELOADING, not simply dynamic
> loading. Reloading means that I have already interned a class but want
> to intern a new updated version of that instance.

-- 
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