Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, overriding <code>Object.metaClass.invokeMethod</code> doesn't work because when Groovy tries to resolve a method call for a type X, it checks the metaClass of X, but not the metaClass of its parent class(es). For example, the following code will print "method intValue intercepted"</p> <pre><code>Integer.metaClass.invokeMethod = {def name, def args -&gt; System.out.println("method $name intercepted") } 6.intValue() // Reset the metaClass Integer.metaClass = null </code></pre> <p>But this code will not:</p> <pre><code>Object.metaClass.invokeMethod = {def name, def args -&gt; System.out.println("method $name intercepted") } 6.intValue() // Reset the metaClass Object.metaClass = null </code></pre> <p>Your question was "Is it possible to intercept all the methods called in a application?", but could you be a bit more precise about whether you want to:</p> <ul> <li>Intercept calls to Groovy methods, Java methods, or both</li> <li>Intercept calls to only <em>your</em> Groovy/Java methods or also intercept calls to Groovy/Java library classes</li> </ul> <p>For example, if you only want to intercept calls to your Groovy classes, you could change your classes to implement <a href="http://groovy.codehaus.org/api/groovy/lang/GroovyInterceptable.html" rel="nofollow noreferrer">GroovyInterceptable</a>. This ensures that invokeMethod() is invoked for every method called on those classes. If the nature of the interception (i.e. the stuff you want to do before/after invoking the called method) is the same for all classes, you could define <code>invokeMethod()</code> in a separate class and use <code>@Mixin</code> to apply it to all your classes.</p> <p>Alternatively, if you also want to intercept calls to Java classes, you should check out the <a href="http://groovy.codehaus.org/Using+the+Delegating+Meta+Class" rel="nofollow noreferrer">DelegatingMetaClass</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload