|
Groovy example source code file (GroovyInterceptableTest.groovy)
The Groovy GroovyInterceptableTest.groovy source codepackage groovy import org.codehaus.groovy.runtime.ReflectionMethodInvoker class GroovyInterceptableTest extends GroovyTestCase { void testMethodInterception() { def g = new GI() assert g.someInt() == 2806 assert g.someUnexistingMethod() == 1 assert g.toString() == "invokeMethodToString" } void testProperties() { def g = new GI() assert g.foo == 89 g.foo = 90 assert g.foo == 90 // should this be 1 or 90? assert g.getFoo() == 1 } void testCallMissingMethod() { def obj = new GI2() shouldFail { obj.notAMethod() } assert 'missing' == obj.result } void testCallMissingMethodFromInstance() { def obj = new GI2() shouldFail { obj.method() } assert 'missing' == obj.result } } class GI implements GroovyInterceptable { def foo = 89 int someInt() { 2806 } String toString() { "originalToString" } Object invokeMethod(String name, Object args) { if ("toString" == name) return "invokeMethodToString" else if ("someInt" == name) return ReflectionMethodInvoker.invoke(this, name, args) else return 1 } } class GI2 implements GroovyInterceptable { def result = "" def invokeMethod(String name, args) { def metaMethod = Foo.metaClass.getMetaMethod(name, args) if (metaMethod != null) return metaMethod.invoke(this, args) result += "missing" throw new MissingMethodException(name, Foo.class, args) } def method() { notAMethod() } } Other Groovy examples (source code examples)Here is a short list of links related to this Groovy GroovyInterceptableTest.groovy source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.