• Post Reply Bookmark Topic Watch Topic
  • New Topic

erasure...Generics  RSS feed

 
donny khosa
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


when I complile this file without generics everything is cool...but with Generics it complains about unchecked or unsafe operations..why is it like that
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Does it only complain about line 3? Somebody else has the same problem, and hasn't worked it out yet: here. Why do you think you are getting the warning?
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This article throws some light on a similar warning.

I quote this from the article:
Why is there a compiler warning? Because in order to pass a and b to the method the varargs implementation needs to create an array - Collection<String>[]. But the generics implementation can't create a generified array, yet thats what the method is demanding. Arrrgh!!

So, instead of being helpful, Java creates an ungenerified Collection[], and then the generics system warns us that it is an unchecked cast to make it a Collection<String>[]. But this conversion is all internal to the compiler! As far as the developer is concerned, there is nothing even vaguely type unsafe about what s/he is doing.

Now this is just headbangingly stupid. There is absolutely no risk of breaking the generics model here. No risk of a ClassCastException or ArrayStoreException. No, this is really just another case of generics being nearly completely broken with regards to arrays. And the varargs group didn't manage to realise/coordinate and get a decent implementation that works.

The problem for me is serious though. I am writing an API (commons-collections) that will be widely used, and will have to retain backwards binary compatibility. If I go with the varargs option (the logically correct choice), then every client of this method will get a useless java warning - not very friendly. The alternative is to go back to the original design and have three methods, an array, one-arg and two arg. And really thats just crap.


So, it seems it is basically because of the var-args method asList() trying to create a generics array in order to call the var-args method.
Since, the compiler generates this code, on compilation, we do not even get the line number for the warnings. Now that is something really ridiculous!!!
However, if you are really bothered about it, put this annotation at the top of your method:

 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, Nitesh Kant. I think it has something to do with Java generics using type erasure rather than reification, but other people will know a lot better than me.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell: I think it has something to do with Java generics using type erasure rather than reification, but other people will know a lot better than me.


can you explain a little as to what you perceive the problem is.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I am afraid I can't explain. I know that Java generics is done by type erasure (look in the Java Tutorials for "generics") which means that after the compiler has checked all the types, the types are deleted and the Lists etc are non-generic. Somehow that means it is impossible to create an array of generic objects; It has come up on these boards before, so doing a search for "type erasure generic array" might help. This thread which might be helpful, came up when I did exactly that search. See whether that helps. I am afraid that is as far as I can help.

[edit]Corrected misspelt hyperlink[/edit]
[ January 28, 2008: Message edited by: Campbell Ritchie ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!