Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6486604

Unchecked cast warning and ClassCastException handling

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Not an Issue
    • Icon: P5 P5
    • None
    • 6
    • tools
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      The java compiler should not issue an unchecked cast warning when the statement causing this warning is inside a try-catch block that explicitely handles ClassCastException.

      JUSTIFICATION :
      In many cases, such as when creating objects through reflection, it is not possible to write code that doesn't generate unchecked cast warnings. In these cases, the developper is well aware of the unchecked cast and wil have explicitely handled this case by catching ClassCastException and taking the appropriate action.

      The ultimate goal behind compiler warnings is to bring possible problems forward to make sure the developper is aware of them. In this particular case, it is clear that the developper is already aware of the caveat and has taken the appropriate steps to handle it. Issuing a warning in this case is just superfluous.

      On the otherhand, using the @SuppressWarnings annotation feels like "telling the compiler to shut up" while not really fixing the problem at all. It feels neither clean nor safe. Having to use this annotation feels like there is something wrong in the code, which is not necessarily the case.

      Additionally since the @SuppressWarnings annotation can only be used on a class or method level, it might be dangerous to use. If a developper codes a method for which he cannot avoid the unchecked cast warning and then annotates the method with @SuppressWarnings, all the generics code that he will write later on in the same method will not be appropriately checked by the compiler, thus introducing the possibility of additional silenced unchecked cast warning which the developper might not be aware of.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      code such as:

      Class<? extends MyObject> clazz = null;
      try{
      clazz = (Class<? extends MyObject>)Class.forName(className);

      }catch(ClassCastException cce){
      // handle the exception correctly
      log.warn("Invalid type");
      throw new SomeCheckedException();
      }

      should compile without warning, since it is clear that the programmer is aware of the unchecked cast and has taken appropriate measures to protect himself against it.


      ACTUAL -
      The code above makes the compiler issue an unchecked cast warning. This forces the user to "tell the compiler to shut up" using the @SuppressWarnings anotation, which feels like a clunky, unsafe workaround.

      CUSTOMER SUBMITTED WORKAROUND :
      Use the @SuppressWarnings annotation.

            ahe Peter Ahe
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: