Skip to content

Instantly share code, notes, and snippets.

@rrees
Created January 22, 2009 19:41
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 rrees/50684 to your computer and use it in GitHub Desktop.
Save rrees/50684 to your computer and use it in GitHub Desktop.
class MyClass {
void tweeter() {
println "Tweet, tweet"
}
}
def my_object = new MyClass()
def my_replacement_method = { println "Honk, honk" }
class ReplacingInterceptor implements Interceptor {
ReplacingInterceptor(String aMethodName, Closure aReplacementMethod) {
replacementMethod = aReplacementMethod
methodName = aMethodName
}
final String methodName
final Closure replacementMethod
Object beforeInvoke(Object object, String methodName, Object[] arguments) {
if(this.methodName.equals(methodName)) {
replacementMethod.call(arguments)
}
return object
}
boolean doInvoke() { false }
Object afterInvoke(Object object, String methodName, Object[] arguments, Object result) { result}
}
proxy = ProxyMetaClass.getInstance(my_object.class)
proxy.interceptor = new ReplacingInterceptor('tweeter', my_replacement_method)
proxy.use {
new MyClass().tweeter()
my_object.tweeter()
}
new MyClass().tweeter()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment