title: Java generics and overloading with Groovy
body: <p>I write my unit tests for my Java application with Groovy, JUnit and EasyMock.
  In EasyMock there are several overloaded methods <code>capture()</code> which have been deprecated with the note "Because of harder erasure enforcement, doesn't compile in Java 7". The methods take as parameter an object of type <code>Capture&lt;T&gt;</code>. There exist, among others, the following methods:</p>
  
  <ul>
  <li><code>static boolean capture(Capture&lt;Boolean&gt; captured)</code> </li>
  <li><code>static boolean capture(Capture&lt;Integer&gt; captured)</code></li>
  <li>...</li>
  <li><code>static &lt;T&gt; T capture(Capture&lt;T&gt; captured)</code></li>
  </ul>
  
  <p>This is not allowed any more in Java but if you invoke that code directly from Java the right method gets invoked. E.g. when you execute this code</p>
  
  <pre><code>Capture&lt;MyClass&gt; myClassCapture = new Capture&lt;MyClass&gt;();
  mockObject.someMethod(capture(myClassCapture));
  </code></pre>
  
  <p>The right method (the last in the list) gets invoked.</p>
  
  <p>On the other hand, if you invoke the same code from inside Groovy, the first method in the list is invoked and gives an error in my test. I think that this has do to with how Java and Groovy resolve the methods. My assumption is that Java binds the method at compile time while Groovy tries to find the method at runtime and takes any method it can find (maybe the first).</p>
  
  <p>Can anybody explain exactly what happens here? This would be nice to understand the different behaviour between Java and Groovy more precisely.</p>
  
  <p>I fixed it by delegating the call inside Groovy to a Java method which will do the job for me:</p>
  
  <pre><code>public class EasyMockUtils {
  
      public static &lt;T&gt; T captureObject(Capture&lt;T&gt; captureForObject) {
          return EasyMock.capture(captureForObject);
      }
  }
  </code></pre>
  
  <p>Is there maybe a better way?</p>