What is SuppressWarnings (“unchecked”) in Java?

前端 未结 11 587
轮回少年
轮回少年 2020-11-22 12:48

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

@SuppressWarnings(\"unchecked\")

What does this mean?

相关标签:
11条回答
  • 2020-11-22 13:21

    @SuppressWarnings annotation is one of the three built-in annotations available in JDK and added alongside @Override and @Deprecated in Java 1.5.

    @SuppressWarnings instruct the compiler to ignore or suppress, specified compiler warning in annotated element and all program elements inside that element. For example, if a class is annotated to suppress a particular warning, then a warning generated in a method inside that class will also be separated.

    You might have seen @SuppressWarnings("unchecked") and @SuppressWarnings("serial"), two of most popular examples of @SuppressWarnings annotation. Former is used to suppress warning generated due to unchecked casting while the later warning is used to remind about adding SerialVersionUID in a Serializable class.

    Read more: https://javarevisited.blogspot.com/2015/09/what-is-suppresswarnings-annotation-in-java-unchecked-raw-serial.html#ixzz5rqQaOLUa

    0 讨论(0)
  • 2020-11-22 13:23

    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.

    0 讨论(0)
  • 2020-11-22 13:28

    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.

    0 讨论(0)
  • 2020-11-22 13:28

    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;
    
    0 讨论(0)
  • 2020-11-22 13:34

    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;
    }
    
    0 讨论(0)
提交回复
热议问题