views:

9252

answers:

8

Sometime when looking through code, I see many methods specify an annotation:

@SuppressWarnings("unchecked")

What does this mean?

A: 

Without this the IDE (I use Eclipse) will give a warning saying function unused. This is a bit annoying.

yinyueyouge
+5  A: 

It is an annotation to suppress compile warnings about unchecked generic operations (not exceptions - edited thanks to Skeet), such as casts. It essentially implies that the programmer did not wish to be notified about these which he is already aware of when compiling a particular bit of code.

You can read more on this specific annotation here:

SuppressWarnings

Additionally, Sun provides some tutorial documentation on the usage of annotations here:

Annotations

As they put it,

"The 'unchecked' warning can occur when interfacing with legacy code written before the advent of generics (discussed in the lesson titled Generics)."

byte
+7  A: 

Sometimes Java generics just doesn't let you do what you want to, and you need to effectively tell the compiler that what you're doing really will be legal at execution time.

I usually find this a pain when I'm mocking a generic interface, but there are other examples too. It's usually worth trying to work out a way of avoiding the warning rather than suppressing it (the Java Generics FAQ helps here) but sometimes even if it is possible, it bends the code out of shape so much that suppressing the warning is neater. Always add an explanatory comment in that case!

The same generics FAQ has several sections on this topic, starting with "What is an "unchecked" warning?" - it's well worth a read.

Jon Skeet
In some cases, you can avoid it by using YourClazz.class.cast(). Works for single generic element containers but not for collections.
kd304
+1  A: 

The SuppressWarning annotation is used to suppress compiler warnings for the annotated element. Specifically, the unchecked category allows suppression of compiler warnings generated as a result of unchecked type casts.

Brandon E Taylor
+1  A: 

As far I know, for now it has to do with suppressing warnings about generics; generics are a new programming construct not supported in JDK versions earlier than JDK 5, so any mixes of the old constructs with the new ones might pose some unexpected results.

The compiler warns the programmer about it, but if the programmer already knows, they can turn those dreaded warnings off using SuppressWarnings.

BakerTheHacker
JDK5 is new? It has completed most of its End of Service Life period.
Tom Hawtin - tackline
I know that JDK 5 is rather outdated, what I meant was that, it is new in the sense that it introduced new features to Java, previously not offered in JDK 4. Also note that there those still awaiting JDK 7 before leaving JDK 5 behind in order to embrace JDK 6, I cannot reason why!
BakerTheHacker
A: 

It could also mean that the current Java type system version isn't good enough for your case. There were several JSR propositions / hacks to fix this: Type tokens, Super Type Tokens, Class.cast().

If you really need this supression, narrow it down as much as possible (e.g. don't put it onto the class itself or onto a long method). An example:

public List<String> getALegacyListReversed() {
   @SuppressWarnings("unchecked") List<String> list =
       (List<String>)legacyLibrary.getStringList();

   Collections.reverse(list);
   return list;
}
kd304
A: 

you can suppress the compiler warnings and tell the generics that the code which you had written is legal according to it.

my example: @SuppressWarnings("unchecked") public List retreiveMealPlan() { List list=new ArrayList(); TestMenuService testMenuService=new TestMenuService(em, this.selectedInstance); list = testMenuService.getMeal(reservationMealPlan); return list; }

s.k.sumaprakash
A: 

One trick is to create an interface that extends a generic base interface...

    public interface LoadFutures extends Map<UUID, Future<LoadResult>> {}

Then you can check it with instanceof before the cast...

    Object obj = context.getAttribute(FUTURES);
    if (!(obj instanceof LoadFutures)) {
        String format = "Servlet context attribute \"%s\" is not of type "
                + "LoadFutures. Its type is %s.";
        String msg = String.format(format, FUTURES, obj.getClass());
        throw new RuntimeException(msg);
    }
    return (LoadFutures) obj;
Brian Edwards