Generic methods don't work with raw types

XMLWordPrintable

    • Type: Bug
    • Resolution: Not an Issue
    • Priority: P3
    • None
    • Affects Version/s: 1.4.0
    • Component/s: specification
    • generic
    • generic

      The problem (which may or may not be a bug) is demonstrated by the starred line of the following program:

      public class SillyInstantiator<T> {
          public <U> U newObj(Class<U> klass) throws Exception {
              return klass.newInstance();
          }

          public static void main(String[] args) throws Exception {
              SillyInstantiator<Integer> si = new SillyInstantiator<Integer>();
              StringBuffer i1 = si.newObj(StringBuffer.class);
              SillyInstantiator rawSi = si;
              StringBuffer i2 = rawSi.newObj(StringBuffer.class); // *
          }
      }

      SillyInstantiator.java:10: incompatible types
      found : java.lang.Object
      required: java.lang.StringBuffer
              StringBuffer i2 = rawSi.newObj(StringBuffer.class);
                                            ^
      Note: SillyInstantiator.java uses unchecked or unsafe operations.
      Note: Recompile with -Xlint:unchecked for details.
      1 error

            Assignee:
            Gilad Bracha (Inactive)
            Reporter:
            Josh Bloch (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: