Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Unchecked cast warning

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to cast from a Collection of AObject's to ArrayList<AObject>, but I get a warning. "Type safety: Unchecked cast from Collection to ArrayList<AObject>.

What I have read about this is that the JVM can't be sure that the Collection contains AObject's and thus give this warning.

However. I have first coded my application in IBM Rational Application Developer 7(Which is Eclipse 3.2) and here I do not get this warning. When I moved over to Eclipse and got rid of the IBM spesifics this warning popped up.

I could not find any reason why this would be fine in RAD7 but not in Eclipse 3.3(or 3.2 though RAD7 is eclipse 3.2++).
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because Eclipse has its own compiler, and it either chose not to warn about this, or simply reports the warning in some other way.

It's a warning, you understand why you get it, so just accept it and move on. Such is life. There are warnings related to Java generics that can't' be resolved without fundamental reorganizations of your code; so don't sweat it.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's an annotation - @SuppressWarning, I think - that can be used to notate code that would otherwise generate a warning.

I believe it is a good thing to use this in places where you're getting a warning, but you have carefully analysed the code and determined there is no bug. That way, you eliminate the spurious warning.

Spurious warnings get you used to ignoring all warnings, which isn't good. Some warnings will be telling you about real bugs.

One shouldn't get used to just suppressing warnings every time they appear, either. Only after careful checking of the correctness of the code and also checking that there isn't a simple way to modify the code to eliminate the warning properly.
 
Sverre Moe
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have checked in the code which gives me the Collection object that the objects in the Collection are allways AObject so that when I use ArrayList<AObject> that will/should work.

If the code which gives me the Collection object where designed with generics in mind I think this warning would go away. As it stands now it is simply a raw Collection. (That code is external)

However I do not like warnings: I try to fix them if fixing is possible. In this case it was not so I appended the @SuppressWarnings("unchecked")
 
I am going to test your electrical conductivity with this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic