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

Object.getClass() typing rule could be improved

    XMLWordPrintable

Details

    • Enhancement
    • Status: Open
    • P4
    • Resolution: Unresolved
    • 5.0, 6, 7
    • None
    • specification
    • Fix Understood
    • x86
    • linux, windows_xp, windows_7

    Description

      A DESCRIPTION OF THE REQUEST :
      The jls3 states that for a given t of type T,
      t.getClass() is typed Class<? extends erasure(T)>.

      I think the getClass() typing rule could be changed to
      Class<? extends wildcard(T)>

      The wildcard operation is defined by:
      if T is parametrized, wildcard(T)=erasure(T)<?>
      else , wildcard(T)=T
          

      JUSTIFICATION :
      1) This rule introduce a raw type.
           Raw type must ONLY be used to interact with legacy code.

      2) The new Rule introduce a wildcard.
           Relationship between parametrized type and wildcard are based
           on subtyping rules.
           Relationship between parametrized type and wildcard are based
           on raw type conversion.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      the program must not compile
      ACTUAL -
      the program compile

      ---------- BEGIN SOURCE ----------
      public void f(ArrayList<Integer> l1, ArrayList<String> l2)
         throws InstantiationException, IllegalAccessException {

          l1.getClass().newInstance().addAll(l2.getClass().newInstance());
        }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      public Class<? extends ArrayList<?>> wildcardGetClass(ArrayList<?> list) {
          return (Class<? extends ArrayList<?>>)list.getClass();
        }
        
        public void f(ArrayList<Integer> l1, ArrayList<String> l2)
           throws InstantiationException, IllegalAccessException {
            wildcardGetClass(l1).newInstance().addAll(
               wildcardGetClass(l2).newInstance());
        }
      ###@###.### 10/26/04 22:47 GMT

      Attachments

        Issue Links

          Activity

            People

              abuckley Alex Buckley
              rmandalasunw Ranjith Mandala (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Imported:
                Indexed: