• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need some clarification about generics code in org.mokito.Matchers  RSS feed

 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking at source code here: http://book2s.com/java/src/package/org/mockito/matchers.html#21ab27366c5ee05b1b45dc38f1fdffd7 and came across some code that I don't quite know how to interpret.

Can anyone shed some light on what's happening with the .<T> returnNull(); part on line 3? I have never seen this construct before and am pretty as to how that even works.

Thanks for any clarification and illumination you can give.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe the returnNull() method takes a type parameter and that has to be passed to it. I have never seen that construct either. What does the returnNull() method look like?
I can see some casts to (T) in the page you linked, but not the returnNull method. There was another returnNull() call with a similar <T>.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Maybe the returnNull() method takes a type parameter and that has to be passed to it. I have never seen that construct either. What does the returnNull() method look like?


From what I can tell, returnNull() comes from this: http://book2s.com/java/src/package/org/mockito/internal/progress/handyreturnvalues.html and it looks like...

wait for it...

I can see some casts to (T) in the page you linked, but not the returnNull method. There was another returnNull() call with a similar <T>.

The cast (T) I think I have an inkling of what's going on there and it's related to generics type erasure. That's about as much inkling I have about it right now though. More like a smudge of an inkling, really.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a feature of generic methods. You can force the generic type (with the method call code), instead of letting the compiler figure out the type.

And of course, this is done here to ensure that the generic used by the returnNull() generic method, is the same that was assigned by the refEq() generic method. It actually looks a bit weird in this example, because the T implies that it is declaring a generic -- it is actually using the generic type that was assigned to the refEq() method call.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an another example, let's say I want to call the refEq() method, with a null value, and I want to use Integer as the generic type ... then this ...


will have issues as the compiler can't determine the generic type correctly. Alternatively, I can do this...

Henry
Staff note (Henry Wong):

This code in this post is for demonstration purposes only, and have *not* been compiled and/or tested.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Henry. I'll have to look into this stuff some more. Very interesting.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!