title: Groovy - How do I use metaprogramming to add tracing to all static methods of a class?
body: <p>This is what I have so far:</p>
  
  <pre><code>static def traceMethods(Class clazz) {
      def mc = clazz.metaClass
  
      mc.static.invokeMethod = { String name, args -&gt;
          List types = args.collect { it == null ? null : it.getClass() }
          MetaMethod metmeth = mc.getStaticMetaMethod(name, *types) //throws exception sometimes when it shouldnt
  
          println "Starting method $name"
          def result = metmeth.doMethodInvoke(delegate, args)         
          println "Finished method $name"
  
          return result
      }
  }
  </code></pre>
  
  <p>This works perfectly most of the time.  However, sometimes the call to <code>getStaticMetaMethod</code> throws an exception when it shouldn't.</p>
  
  <p>EDIT: The exception it throws is:</p>
  
  <pre><code> groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method org.codehaus.groovy.runtime.HandleMetaClass#getStaticMetaMethod.|Cannot resolve which method to invoke for [class java.lang.String, null] due to overlapping prototypes between:|?[class java.lang.String, class [Ljava.lang.Object;]|?[class java.lang.String, class [Ljava.lang.Class;]
      at groovy.lang.MetaClassImpl.chooseMostSpecificParams(MetaClassImpl.java:2906)
      at groovy.lang.MetaClassImpl.chooseMethodInternal(MetaClassImpl.java:2859)
      at groovy.lang.MetaClassImpl.chooseMethod(MetaClassImpl.java:2800)
      at groovy.lang.MetaClassImpl.getMethodWithCachingInternal(MetaClassImpl.java:1181)
      at groovy.lang.MetaClassImpl.createPogoCallSite(MetaClassImpl.java:3022)
  </code></pre>