File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Unchecked cast warning Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Unchecked cast warning" Watch "Unchecked cast warning" New topic
Author

Unchecked cast warning

Sverre Moe
Ranch Hand

Joined: Jul 10, 2007
Posts: 109
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++).
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24041
    
  13

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.


[Jess in Action][AskingGoodQuestions]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
Sverre Moe
Ranch Hand

Joined: Jul 10, 2007
Posts: 109
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 agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Unchecked cast warning
 
Similar Threads
How do you create an array of typesafe-collections?
generics compiler warning
generic collection - avoid unchecked cast?
Writeing / Reading ArrayList
please help I Need RAD7