What is SuppressWarnings (“unchecked”) in Java?

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

    In Java, generics are implemented by means of type erasure. For instance, the following code.

    List<String> hello = List.of("a", "b");
    String example = hello.get(0);
    

    Is compiled to the following.

    List hello = List.of("a", "b");
    String example = (String) hello.get(0);
    

    And List.of is defined as.

    static <E> List<E> of(E e1, E e2);
    

    Which after type erasure becomes.

    static List of(Object e1, Object e2);
    

    The compiler has no idea what are generic types at runtime, so if you write something like this.

    Object list = List.of("a", "b");
    List<Integer> actualList = (List<Integer>) list;
    

    Java Virtual Machine has no idea what generic types are while running a program, so this compiles and runs, as for Java Virtual Machine, this is a cast to List type (this is the only thing it can verify, so it verifies only that).

    But now add this line.

    Integer hello = actualList.get(0);
    

    And JVM will throw an unexpected ClassCastException, as Java compiler inserted an implicit cast.

    java.lang.ClassCastException: java.base/java.lang.String cannot be cast to java.base/java.lang.Integer
    

    An unchecked warning tells a programmer that a cast may cause a program to throw an exception somewhere else. Suppressing the warning with @SuppressWarnings("unchecked") tells the compiler that the programmer believes the code to be safe and won't cause unexpected exceptions.

    Why would you want to do that? Java type system isn't good enough to represent all possible type usage patterns. Sometimes you may know that a cast is safe, but Java doesn't provide a way to say so - to hide warnings like this, @SupressWarnings("unchecked") can be used, so that a programmer can focus on real warnings. For instance, Optional.empty() returns a singleton to avoid allocation of empty optionals that don't store a value.

    private static final Optional<?> EMPTY = new Optional<>();
    public static<T> Optional<T> empty() {
        @SuppressWarnings("unchecked")
        Optional<T> t = (Optional<T>) EMPTY;
        return t;
    }
    

    This cast is safe, as the value stored in an empty optional cannot be retrieved so there is no risk of unexpected class cast exceptions.

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

    Simply: It's a warning by which the compiler indicates that it cannot ensure type safety.

    JPA service method for example:

    @SuppressWarnings("unchecked")
    public List<User> findAllUsers(){
        Query query = entitymanager.createQuery("SELECT u FROM User u");
        return (List<User>)query.getResultList();
    }
    

    If I didn'n anotate the @SuppressWarnings("unchecked") here, it would have a problem with line, where I want to return my ResultList.

    In shortcut type-safety means: A program is considered type-safe if it compiles without errors and warnings and does not raise any unexpected ClassCastException s at runtime.

    I build on http://www.angelikalanger.com/GenericsFAQ/FAQSections/Fundamentals.html

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

    It is an annotation to suppress compile warnings about unchecked generic operations (not exceptions), 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, Oracle 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)."

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

    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.

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

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

    Example:

    @SuppressWarnings("unchecked")
    public List<ReservationMealPlan> retreiveMealPlan() {
         List<ReservationMealPlan> list=new ArrayList<ReservationMealPlan>();
        TestMenuService testMenuService=new TestMenuService(em, this.selectedInstance);
        list = testMenuService.getMeal(reservationMealPlan);
        return list;
     }
    
    0 讨论(0)
  • 2020-11-22 13:20

    A warning by which the compiler indicates that it cannot ensure type safety. The term "unchecked" warning is misleading. It does not mean that the warning is unchecked in any way. The term "unchecked" refers to the fact that the compiler and the runtime system do not have enough type information to perform all type checks that would be necessary to ensure type safety. In this sense, certain operations are "unchecked".

    The most common source of "unchecked" warnings is the use of raw types. "unchecked" warnings are issued when an object is accessed through a raw type variable, because the raw type does not provide enough type information to perform all necessary type checks.

    Example (of unchecked warning in conjunction with raw types):

    TreeSet set = new TreeSet(); 
    set.add("abc");        // unchecked warning 
    set.remove("abc");
    
    warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.TreeSet 
                   set.add("abc");  
                          ^
    

    When the add method is invoked the compiler does not know whether it is safe to add a String object to the collection. If the TreeSet is a collection that contains String s (or a supertype thereof), then it would be safe. But from the type information provided by the raw type TreeSet the compiler cannot tell. Hence the call is potentially unsafe and an "unchecked" warning is issued.

    "unchecked" warnings are also reported when the compiler finds a cast whose target type is either a parameterized type or a type parameter.

    Example (of an unchecked warning in conjunction with a cast to a parameterized type or type variable):

      class Wrapper<T> { 
      private T wrapped ; 
      public Wrapper (T arg) {wrapped = arg;} 
      ... 
      public Wrapper <T> clone() { 
        Wrapper<T> clon = null; 
         try {  
           clon = (Wrapper<T>) super.clone(); // unchecked warning 
         } catch (CloneNotSupportedException e) {  
           throw new InternalError();  
         } 
         try {  
           Class<?> clzz = this.wrapped.getClass(); 
           Method   meth = clzz.getMethod("clone", new Class[0]); 
           Object   dupl = meth.invoke(this.wrapped, new Object[0]); 
           clon.wrapped = (T) dupl; // unchecked warning 
         } catch (Exception e) {} 
         return clon; 
      } 
    } 
    
    warning: [unchecked] unchecked cast 
    found   : java.lang.Object 
    required: Wrapper <T> 
                      clon = ( Wrapper <T>)super.clone();  
                                                    ^ 
    warning: [unchecked] unchecked cast 
    found   : java.lang.Object 
    required: T 
                      clon. wrapped = (T)dupl;
    

    A cast whose target type is either a (concrete or bounded wildcard) parameterized type or a type parameter is unsafe, if a dynamic type check at runtime is involved. At runtime, only the type erasure is available, not the exact static type that is visible in the source code. As a result, the runtime part of the cast is performed based on the type erasure, not on the exact static type.

    In the example, the cast to Wrapper would check whether the object returned from super.clone is a Wrapper , not whether it is a wrapper with a particular type of members. Similarly, the casts to the type parameter T are cast to type Object at runtime, and probably optimized away altogether. Due to type erasure, the runtime system is unable to perform more useful type checks at runtime.

    In a way, the source code is misleading, because it suggests that a cast to the respective target type is performed, while in fact the dynamic part of the cast only checks against the type erasure of the target type. The "unchecked" warning is issued to draw the programmer's attention to this mismatch between the static and dynamic aspect of the cast.

    Please refer: What is an "unchecked" warning?

    0 讨论(0)
提交回复
热议问题