35

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)

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

Thank you

Drahakar
  • 5,986
  • 6
  • 43
  • 58

3 Answers3

64

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

Henri Tremblay
  • 666
  • 5
  • 2
  • +1 Although it seems like this is removing the check on the _type_ of the parameter, EasyMock doesn't do that check anyway. So to EasyMock, this is equivalent. – Gray Jan 31 '14 at 16:57
  • 1
    Is there an available reference or broader explanation of what the syntax above is actually doing? – Dave G Jul 30 '14 at 18:58
  • If you are on Java 8 and above, just using `EasyMock.anyObject()` will work. – Praveer Gupta Aug 23 '17 at 16:30
4

Only as suggestion :

interface A extends List<MyType> {};
EasyMock.anyObject(A.class) 
Gray
  • 115,027
  • 24
  • 293
  • 354
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
0

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

pushkin
  • 9,575
  • 15
  • 51
  • 95