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

Compiler doesn't catch static type error: usage unlike method signature

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.3.0
    • tools
    • generic
    • generic



      Name: tb29552 Date: 09/15/99


      /*

      The following code

        DateFormat df = DateFormat.getInstance();
        df.format(new Object()); // wrong!

      should cause a compile-time error, since the signature of DateFormat.format is
        public final String format(Date)
      javap java.text.DateFormat confirms this, as does the javadoc at:
      http://java.sun.com/products/jdk/1.3/docs/api/java/text/DateFormat.html#format(java.util.Date)

      However, it passes the compiler and generates an exception at run-time,
      as follows:

      % javac df.java
      % java df
      java.version = 1.3beta
      Operating System Name = SunOS
      Operating system architecture = sparc
      Operating system version = 5.7

      Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
              at java.text.DateFormat.format(DateFormat.java:277)
              at java.text.Format.format(Format.java:129)
              at df.main(df.java:40)


      The following byte codes are produced for the format() method call:
        12 invokevirtual #6 <Method java.lang.String format(java.lang.Object)>

      This is not the correct method signature, and no check is inserted
      in the code to make sure that a Date object is passed.
      */
      //
      import java.util.Date;
      import java.text.DateFormat;
      class df {
          public static void outputMessage() {
              System.out.println("java.version = " +
                                 System.getProperty("java.version"));
              System.out.println("Operating System Name = " +
                                 System.getProperty("os.name"));
              System.out.println("Operating system architecture = " +
                                 System.getProperty("os.arch"));
              System.out.println("Operating system version = " +
                                 System.getProperty("os.version") +
                                 System.getProperty("line.separator"));
          }

          public static void main(String[] args) {
              outputMessage();

              DateFormat df = DateFormat.getInstance();
              df.format(new Object());// wrong!
          }
      }

      (Review ID: 41807)
      ======================================================================

            wmaddoxsunw William Maddox (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: