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

Generic methods don't work with raw types

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.4.0
    • 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

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: