• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to get rid of warning: [unchecked] unchecked cast

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Folks,

I keep getting warning : [unchecked] unchecked cast for below part of code:


How to resolve both of them?

[ May 06, 2008: Message edited by: amer seif el dine ]
[ May 06, 2008: Message edited by: amer seif el dine ]
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure, but I think your generic types might be too loose. Maybe more restrictive generic typing and losing the casts would help.

Have you read the Java Tutorials about generics? See what it says about wildcards and extends. I think the bit about ? extends Object is a bit loose; if you could specify the type you are going to use you could lose the cast.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@SuppressWarnings("unchecked")

At the top of the method has shutdown those warnings for me.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Calum Ludwig:
@SuppressWarnings("unchecked")

At the top of the method has shutdown those warnings for me.



That assumes that you want to suppress the warnings rather than fix the issue that's being warned about.

Sometimes, especially when using a non-genericized API, you have no choice. But when I can, I like to write fully genercized code that triggers no warnings.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
But when I can, I like to write fully genercized code that triggers no warnings.



Me too, and I tend to have both the Eclipse and FindBugs warnings available, and the same issue also makes me sad.



All objects and lists are immutable, so I guess I could change it to:


or even define my own typed "EMPTY_LIST", but that's not what I mean, is it?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You piqued my curiosity, so I checked the documentation. And when I looked at the details for the EMPTY_LIST constant, there was a link "See also: emptyList()". And when you follow that link it says "This example illustrates the type-safe way to obtain an empty list..."
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm looking at the 1.4 source, but of course it isn't there, is it?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So here is my current gripe:

Generics provide type-safe collections, as a by-product reduce the need (and cost?) of casting. The emptyList() method uses the static EMPTY_LIST instance, so all good there, but it just casts it to the required type.

It just feels like the old "we're OO, but we we also use primitives if we feel like it". We have generics for type safety and we are no longer required to cast for Collections, unless of course we do.

I'm just uppity.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or to paraphrase, Collections.EMPTY_LIST is a 'free' operation, Collections.emptyList() is not, due to the internal cast.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Back to the original question ...

As far as I got it will any cast to a generic type trigger the unchecked warning as the compiler cannot check if it is correct (so it remains unchecked ...).

So there is no other way to get rid of the warning than to use to @SuppressWarnings annotation.

But applying it to the method at a whole is usually not a good idea if the method is a bit longer than just a few lines - although this is the way Eclipse is doing it for you ...

Instead the annotation should be assigned to the location where the cast is performed.

Assume the following code sequence:

public List<String> method()
{
return (List<String> getList();
}

The return statement will cause the warning that can be suppressed by

@SuppressWarnings( "unchecked" )
public List<String> method()
{
return (List<String> getList();
}

But when later code will be added to the method (perhaps additional casts ...), no warning will be thrown. So my recommendation is to write it as follows:

public List<String> method()
{
@SuppressWarnings( "unchecked" )
List<String> retValue = (List<String> getList();
return retValue;
}

Now only the warning caused by the assignment to retValue is suppressed - for the price of an additional local variable. Unfortunately the annotation can only be assigned to declarations, so the code below does not work:

public List<String> method()
{
List<String> retValue = null;

@SuppressWarnings( "unchecked" )
retValue = (List<String> getList();//Does not work!!!

return retValue;
}

In this case another helper variable might be necessary.

When the coder assigns the @SuppressWarnings annotation somewhere she makes a statement: it documents that he knows that the code sequence is somehow problematic, AND that he has taken appropriate precautions against any error conditions (if necessary at all). This Collection.emptyList() method is exactly that: as the list is empty it is ensured that is compatible to any type of List (as the generic "type" of a list is in fact the type of the List's contained elements).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic