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

generics: separate compilation versus raw type variables (Collections.EMPTY_LIST

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P4
    • None
    • 5.0
    • tools
    • generic
    • solaris_7

    Description

      Users are having lots of problems using Collections.EMPTY_LIST.
      Some of these problems seem to have something to do with the compiler
      behaving differently in the presence of separate compilation.
      I'm not sure which of these should be allowed and which not, but
      at the very least the compiler should behave the same when the
      classes are compiled together as when they are compiled separately.

      Offhand, I suspect there should be no hard errors here, but
      I haven't studied the specification carefully to know for sure.


      Here's the together case:

      frog:~/workspaces/test/GenericsCollections $ cat -n EmptyList2.java
           1 class List<E> {}
           2 class Collections {
           3 static List EMPTY_LIST;
           4 }
           5 public class EmptyList2
           6 {
           7 public static void main(String[] ps)
           8 {
           9 List<String> list = Collections.EMPTY_LIST;
          10 List<String> list2 = (List<String>) Collections.EMPTY_LIST;
          11 }
          12 }
      frog:~/workspaces/test/GenericsCollections $ newjavac -gj -warnunchecked EmptyList2.java
      EmptyList2.java:9: warning: unchecked assignment: List to List<java.lang.String>
             List<String> list = Collections.EMPTY_LIST;
                                             ^
      EmptyList2.java:10: inconvertible types
      found : List
      required: List<java.lang.String>
             List<String> list2 = (List<String>) Collections.EMPTY_LIST;
                                                            ^
      1 error
      1 warning
      frog:~/workspaces/test/GenericsCollections $

      Here's the separate case:

      frog:~/workspaces/test/GenericsCollections $ cat -n Collections.java
           1 class Collections {
           2 static List EMPTY_LIST;
           3 }
      frog:~/workspaces/test/GenericsCollections $ cat -n List.java
           1 class List<E> {}
      frog:~/workspaces/test/GenericsCollections $ cat -n EmptyList.java
           1 public class EmptyList
           2 {
           3 public static void main(String[] ps)
           4 {
           5 List<String> list = Collections.EMPTY_LIST;
           6 List<String> list2 = (List<String>) Collections.EMPTY_LIST;
           7 }
           8 }
      frog:~/workspaces/test/GenericsCollections $ newjavac -gj -warnunchecked List.java Collections.java
      frog:~/workspaces/test/GenericsCollections $ newjavac -gj -warnunchecked EmptyList.java
      EmptyList.java:5: incompatible types
      found : List<E>
      required: List<java.lang.String>
             List<String> list = Collections.EMPTY_LIST;
                                             ^
      EmptyList.java:6: inconvertible types
      found : List<E>
      required: List<java.lang.String>
             List<String> list2 = (List<String>) Collections.EMPTY_LIST;
                                                            ^
      2 errors
      frog:~/workspaces/test/GenericsCollections $

      Attachments

        Issue Links

          Activity

            People

              gafter Neal Gafter
              gafter Neal Gafter
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: