Created
January 22, 2009 19:41
-
-
Save rrees/50684 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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