groovyinterceptabletest.groovy
来自「Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业」· GROOVY 代码 · 共 39 行
GROOVY
39 行
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
}
}
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
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?