What is SuppressWarnings (“unchecked”) in Java?

前端 未结 11 817
轮回少年
轮回少年 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:28

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

    public interface LoadFutures extends Map> {}
    

    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;
    

提交回复
热议问题