classCarimplementsGroovyInterceptable{ def check(){ System.out.println("check called ...") } def start(){ System.out.println("start called ...") } def drive(){ System.out.println "drive called" } def invokeMethod(String name, args){ System.out.println "Called to $name intercepted" if (name != 'check'){ System.out.print('running filter') Car.metaClass.getMetaMethod('check').invoke(this,null) } def validMethod = Car.metaClass.getMetaMethod(name,args) if (validMethod != null){ validMethod.invoke(this,args) }else{ Car.metaClass.invokeMethod(this,name,args) } } }
car = new Car() car.start() car.drive() car.check() try { car.speed() }catch (e){ println e }
1 2 3 4 5 6 7 8 9 10 11 12 13
Called to start intercepted running filtercheck called ... start called ... Called to drive intercepted running filtercheck called ... drive called Called to check intercepted check called ... Called to speed intercepted running filtercheck called ... groovy.lang.MissingMethodException: No signature of method: Car.speed() is applicable for argument types: () values: [] Possible solutions: sleep(long), sleep(long, groovy.lang.Closure), split(groovy.lang.Closure), check(), start(), inspect()