java 如何从 EasyMock.anyObject(List.class) 调用中删除警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3875480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:48:03  来源:igfitidea点击:

How do I remove the warning from a EasyMock.anyObject(List.class) call

javagenericswarningseasymock

提问by Drahakar

Compiler can't stop complaining with this call :

编译器不能停止抱怨这个电话:

EasyMock.anyObject(List.class) 

I tried to specify list's type

我试图指定列表的类型

EasyMock.anyObject(List<MyType>.class)

but it doesn't seems to be an option (anyway, it is stupid since java will erase the type during the compilation)

但它似乎不是一个选项(无论如何,这是愚蠢的,因为java会在编译期间擦除类型)

Is there a clean way (@SuppressWarning is not a clean way IMO) to remove this warning?

有没有干净的方法(@SuppressWarning 不是 IMO 的干净方法)来删除此警告?

Thank you

谢谢

回答by Henri Tremblay

It's not possible. I'll call it a generic limitation. Sadly it's not always possible to remove a warning for some perfectly normal usage (like using the class of a generic class).

这是不可能的。我将其称为通用限制。遗憾的是,对于某些完全正常的使用(例如使用泛型类的类),并不总是可以删除警告。

However, with EasyMock you can do the following:

但是,使用 EasyMock,您可以执行以下操作:

EasyMock.<List<MyType>> anyObject()

which will do the same thing but without warning. The anyObject you used exist because it's a bit more readable and allows static imports.

这将做同样的事情,但没有警告。您使用的 anyObject 存在是因为它更具可读性并且允许静态导入。

回答by Stan Kurilin

Only as suggestion :

仅作为建议:

interface A extends List<MyType> {};
EasyMock.anyObject(A.class) 

回答by A. Mazur

You can also try to use the Hamcrest matcher isA(), instead of anyObject(). The difference between them is that isA-matcher checks the values on null, unlike anyObject. You can learn more about those matchers here

您也可以尝试使用 Hamcrest 匹配器isA(),而不是anyObject(). 它们之间的区别在于isA-matcher 检查 上的值null,与anyObject. 您可以在此处了解有关这些匹配器的更多信息