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

Better exception message from GetField.get

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 6
    • 5.0
    • core-libs

      A DESCRIPTION OF THE REQUEST :
      The attached program attempts to read a double field using a default value that's an int literal. This fails because ObjectInputStream uses the type of the default value to determine the type of the field to look for. That's all fine. However, I would like a clearer exception message here that more precisely identifies the field and type being searched for. This problem was a real pain to debug.

      JUSTIFICATION :
      Hard to debug with current exception message

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Exception in thread "main" java.lang.IllegalArgumentException: no such field x with type int

      ACTUAL -
      Exception in thread "main" java.lang.IllegalArgumentException: no such field


      ---------- BEGIN SOURCE ----------
      import java.io.*;

      public class TwoDPoint implements Serializable {
        
        /** @serial the X-coordinate of the point;
         * any double value except NaN */
        private double radius;
        private double angle;
       
        private static final ObjectStreamField[] serialPersistentFields = {
          new ObjectStreamField("x", double.class),
          new ObjectStreamField("y", double.class),
        };
        
        
        public TwoDPoint(double x, double y) {
          this.radius = Math.sqrt(x*x+y*y);
          this.angle = Math.atan2(y, x);
        }

        public double getX() {
          return radius * Math.cos(angle);
        }

        public double getY() {
          return radius * Math.sin(angle);
        }
        
        public String toString() {
          return "[TwoDPoint:x=" + this.getX() + ", y=" + this.getY() +"]";
        }
        
        public static void main(String[] args) throws IOException, ClassNotFoundException {
           TwoDPoint point = new TwoDPoint(7, 67);
           ByteArrayOutputStream bout = new ByteArrayOutputStream();
           ObjectOutputStream oout = new ObjectOutputStream(bout);
           oout.writeObject(point);
           oout.close();
           byte[] ser = bout.toByteArray();
           ByteArrayInputStream bin = new ByteArrayInputStream(ser);
           ObjectInputStream oin = new ObjectInputStream(bin);
           point = (TwoDPoint) oin.readObject();
           System.out.println(point.getY());
           
        }
        
      private void writeObject(ObjectOutputStream out) throws IOException {
        // Convert to Cartesian coordinates
        ObjectOutputStream.PutField fields = out.putFields();
        fields.put("x", radius * Math.cos(angle));
        fields.put("y", radius * Math.sin(angle));
        out.writeFields();
      }
        
      private void readObject(ObjectInputStream in)
       throws ClassNotFoundException, IOException {
        ObjectInputStream.GetField fields = in.readFields();
        double x = fields.get("x", 0);
        double y = fields.get("y", 0.0);

        // Convert to polar coordinates
        radius = Math.sqrt(x*x + y*y);
        angle = Math.atan2(y, x);
      }


      }

      ---------- END SOURCE ----------

            aozerov Andrey Ozerov
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: