JDK-6559178 : upcast can never yield an unchecked warning
  • Type: Bug
  • Component: specification
  • Sub-Component: language
  • Affected Version: 6
  • Priority: P5
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2007-05-18
  • Updated: 2010-04-04
  • Resolved: 2007-06-13
Related Reports
Relates :  
Description
I am tuning the JDT compiler near unchecked cast, and looking back at the
official words from JLS, I noticed that it isn't quite covering all
aspects.
According to the spec, an upcast can never yield an unchecked warning,
though javac emits one for this case:

import java.util.*;
public class X {

      public static class DatabaseObject {}
      public static class ObjectFormUI<T extends DatabaseObject> {}
      private static final Map<Class<? extends DatabaseObject>, Class<?
extends ObjectFormUI>> uiMap = new HashMap<Class<? extends DatabaseObject>,
Class<? extends ObjectFormUI>>();

      public static <T extends DatabaseObject> Class<? extends
ObjectFormUI<T>> getUI(
                  Class<T> persistentClass) {
            return null != null
                  ? uiMap.get(persistentClass)
                  : (Class<? extends ObjectFormUI<T>>)
uiMap.get(persistentClass); // unchecked
      }
}

Comments
EVALUATION - The return type of uiMap.get(persistentClass) is Class<? extends ObjectFormUI> - Class<? extends ObjectFormUI> is being cast to Class<? extends ObjectFormUI<T>> - The raw type ObjectFormUI is a supertype of the generic type ObjectFormUI<T> (4.10) - ? extends ObjectFormUI<T> <= ? extends ObjectFormUI since ObjectForm<T> <: ObjectForm (4.5.1.1) That is, ? extends ObjectFormUI contains ? extends ObjectFormUI<T>. - Class<? extends ObjectFormUI<T>> <: Class<? extends ObjectFormUI> (4.10) - The cast from Class<? extends ObjectFormUI> to Class<? extends ObjectFormUI<T>> is a DOWNcast. - Such a cast could avoid being unchecked if there is no subtype of Class<? extends ObjectFormUI> that has the same erasure as Class<? extends ObjectFormUI<T>>. That is, Class<? extends ObjectFormUI<T>> is the only possible subtype of Class<? extends ObjectFormUI>. However, there are an infinite number of subtypes of Class<? extends ObjectFormUI>. - The cast is unchecked and the warning is appropriate.
13-06-2007