Will something like below do? Using invokeMethod to intercept calls for each method. The test is self-evident.
Explanation:
Below the metaClass implementation overrides invokeMethod from GroovyObject . Since all groovy objects inherit from GroovyObject, we get the flexibility to manipulate / intercept method calls or even to define our own implementation of methodMissing . We will need one override for static and one for non-static methods. Basically, invokeMethod intercepts calls for each method in the class on which it is defined. As a result, we get some before and after functions for each method. Using reflection, the implementation below can recognize a method by its name and argument and call it at run time.
Note: -
- Make sure that the return value from the method call also returns from closing
- Maybe an expensive class has several methods with a lot of implementation
- If selective execution is required, then check the method name and then intercept the call
Implementation:
class Dummy { def method1() { System.out.println "In method 1" } def method2(String str) { System.out.println "In method 2" } static def method3(int a, int b) { System.out.println "In static method 3" } } Dummy.metaClass.invokeMethod = {String name, args -> def result System.out.println( "Do something before $name is called with args $args") try { result = delegate.metaClass.getMetaMethod(name, args) .invoke(delegate, args) } catch(Exception e) { System.out.println "Handling exception for method $name" } System.out.println( "Do something after $name was called with args $args \n") result } Dummy.metaClass.'static'.invokeMethod = {String name, args -> def result System.out.println( "Do something before static method $name is called with args $args") try { result = delegate.metaClass.getMetaMethod(name, args) .invoke(delegate, args) } catch(Exception e) { System.out.println "Handling exception for method $name" } System.out.println( "Do something after static method $name was called with args $args \n") result } def dummy = new Dummy() dummy.method1() dummy.method2('Test') Dummy.method3(1, 2)
source share