Java泛型和Groovy重载 [英] Java generics and overloading with Groovy

查看:92
本文介绍了Java泛型和Groovy重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Groovy,JUnit和EasyMock为我的Java应用程序编写单元测试。
在EasyMock中,有几个重载的方法 capture(),这些方法已经被注意到由于更强的擦除强制执行,不能在Java 7中编译。这些方法将 Capture< T> 类型的对象作为参数。其中包括以下方法:

$ ul

  • 静态布尔值捕获(Capture< Boolean> capture) code $ $ $ b $ li $ code>静态布尔值捕获(Capture< Integer>捕获)

  • ...

  • static< T> T捕获(Capture< T>捕获)



  • Java中不允许这样做,但如果你直接从Java调用该代码,调用正确的方法。例如。当您执行此代码时

      Capture< MyClass> myClassCapture =新的Capture< MyClass>(); 
    mockObject.someMethod(capture(myClassCapture));

    正确的方法(列表中的最后一个)被调用。


    另一方面,如果您从Groovy中调用相同的代码,那么将调用列表中的第一个方法,并在测试中给出错误。我认为这与Java和Groovy如何解决方法有关。我的假设是Java在编译时绑定了方法,而Groovy试图在运行时找到该方法,并采用它可以找到的任何方法(也许是第一个方法)。



    任何人都可以解释到底发生了什么?这更好地理解Java和Groovy之间的不同行为。



    我通过将Groovy中的调用委托给Java方法来解决它,我:

      public class EasyMockUtils {

    public static< T> T captureObject(Capture< T> captureForObject){
    return EasyMock.capture(captureForObject);
    }
    }

    有没有更好的方法?

    解决方案

    只需使用EasyMock 3.0自己点击此问题即可。然而,它看起来像是从EasyMock 3.2开始解决的,它通过重命名所有包装原语并保留一个捕获方法的方法。



    查看3.2文档以获取更多信息: http:// easymock。 org / api / easymock / 3.2 / org / easymock / EasyMock.html#capture%28org.easymock.Capture%29

    I write my unit tests for my Java application with Groovy, JUnit and EasyMock. In EasyMock there are several overloaded methods capture() 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 Capture<T>. There exist, among others, the following methods:

    • static boolean capture(Capture<Boolean> captured)
    • static boolean capture(Capture<Integer> captured)
    • ...
    • static <T> T capture(Capture<T> captured)

    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

    Capture<MyClass> myClassCapture = new Capture<MyClass>();
    mockObject.someMethod(capture(myClassCapture));
    

    The right method (the last in the list) gets invoked.

    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).

    Can anybody explain exactly what happens here? This would be nice to understand the different behaviour between Java and Groovy more precisely.

    I fixed it by delegating the call inside Groovy to a Java method which will do the job for me:

    public class EasyMockUtils {
    
        public static <T> T captureObject(Capture<T> captureForObject) {
            return EasyMock.capture(captureForObject);
        }
    }
    

    Is there maybe a better way?

    解决方案

    Just hit this issue myself using EasyMock 3.0. However it looks like it's been resolved as of EasyMock 3.2 by renaming all the methods that took wrapped primitives and leaving just one remaining capture method.

    Check the 3.2 docs for more info: http://easymock.org/api/easymock/3.2/org/easymock/EasyMock.html#capture%28org.easymock.Capture%29

    这篇关于Java泛型和Groovy重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆