Skip to content

Instantly share code, notes, and snippets.

@LauJensen
Forked from Chouser/proxytest.clj
Created April 1, 2010 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LauJensen/352360 to your computer and use it in GitHub Desktop.
Save LauJensen/352360 to your computer and use it in GitHub Desktop.
(ns joy.proxytest)
;;== evilbase
(gen-class
:name joy.proxytest.evilbase
:prefix "evilbase-"
:post-init ctor
:methods [[tragedy [] void]]
:impl-ns joy.proxytest)
(defn evilbase-ctor [this]
(println "evilctor before tragedy")
(.tragedy this)
(println "evilctor after tragedy"))
(defn evilbase-tragedy [this]
(println "this should never happen"))
;;== tryit using a proxy
(defn tryit []
(let [p (proxy [joy.proxytest.evilbase] []
(tragedy [] (println "my own tragedy")))]
; We wish that "my own tragedy" were printed above instead of
; "should never happen", but the ctor called .tragedy too early.
; Our proxy has caught up now:
(.tragedy p)))
;;== try it in another gen-class
(gen-class
:name joy.proxytest.hope
:prefix "hope-"
:extends joy.proxytest.evilbase
:impl-ns joy.proxytest)
(defn hope-tragedy [this]
(println "gen-class gives us the power"))
(defn tryit2 []
(joy.proxytest.hope.))
;;-- proxy does not behave as desired
; user=> (joy.proxytest/tryit)
; evilctor before tragedy
; this should never happen
; evilctor after tragedy
; my own tragedy
; nil
;;-- gen-class however gives us the power
; user=> (joy.proxytest/tryit2)
; evilctor before tragedy
; gen-class gives us the power
; evilctor after tragedy
; #<hope joy.proxytest.hope@5c061cd2>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment