On Mon, Feb 28, 2011 at 11:13 AM, Jules <jules.gosn...@gmail.com> wrote:
> I've had one last throw of the dice - a plan to intercept class
> definition and remember associated bytecode so that my URLClassLoader
> server could look it up when given the correspondong class name - with
> no luck :
>
> ;; UNFINISHED
>
> ;; let's maintain a map of class-name : byte-code
> (def class-name-to-byte-code (atom []))
>
> ;; lets provide a class loader that wraps an existing one and
> remembers the byte code that has been used
> (defn make-class-loader-wrapper [class-loader]
>  (proxy
>      [java.lang.ClassLoader]
>      []
>    (^Class defineClass [^String class-name ^"[B" byte-code ^Object
> src-form]
>            (.defineClass class-loader class-name byte-code src-form)
>            (swap! class-name-to-byte-code conj [class-name byte-code])
>            ))))
>
> ;; let's try to wrap-n-replace the existing class loader with our own
> (alter-var-root clojure.lang.Compiler/LOADER make-class-loader-
> wrapper)
> (var-set clojure.lang.Compiler/LOADER (make-class-loader-wrapper
> @clojure.lang.Compiler/LOADER))
>
> It looks like clojure.lang.Compiler/LOADER has been doctored to stop
> people like me playing with it :-) - I can't reset it to point to my
> wrapper - or else I have made a mistake somewhere ?
>
> The whole thing is pretty horrible because ClassLoader is a class not
> an interface and if I could install my wrapper i would actually have
> to proxy every method - but you get the idea...

That's quite interesting -- it looks like you can dynamically replace
the loader because it uses a Var.

Maybe if you proxied DynamicClassLoader rather than just ClassLoader,
and proxied all the methods to go through to DynamicClassLoader.
DynamicClassLoader is not final so this should be possible. Something
like:

(let [old-loader (deref clojure.lang.Compiler/LOADER)]
  (proxy
      [clojure.lang.DynamicClassLoader]
      []
    (^Class defineClass [^String class-name ^"[B" byte-code ^Object
 src-form]
            (.defineClass old-loader class-name byte-code src-form)
            (swap! class-name-to-byte-code conj [class-name byte-code])
            ))))

plus other methods that just punt to (.same-method old-loader args).

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