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

ObjectStreamField.getType() returns Object.class for non-primitive types

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.4.2
    • core-libs
    • None
    • sparc
    • solaris_2.6



      Name: apR10229 Date: 01/29/2003



      ObjectStreamField.getType() returns Object.class for non-primitive types
      (i.e. java.lang.String). This bug is closely similar to a bug 4427881,
      that was fixed and integrated in merlin-beta.

      API Doc says:
      >public Class getType()
      >
      > Get the type of the field.
      >
      > Returns:
      > the Class object of the serializable field

      ObjectStreamField.getType() returns Object.class
      for non-primitive types of fields instead of correct class object,
      representing a given type, as pointed in the API Documentation.

      There is an example below for reproducing this bug.
      (Example.java and CSer.java)

      --------------- example output ------------------------

      <pav@libra(pts/8).262> javac Example.java CSer.java
      <pav@libra(pts/8).263> java -classpath . Example
      Results for ObjectStreamField of type String:
      Returned typeString: Ljava/lang/String;
      Returned type: class java.lang.Object
      Expected type: class java.lang.String
      <pav@libra(pts/8).264>


      ------------- end of output ---------------------------

      -------------------- Example.java ---------------------

      import java.io.*;
      import java.util.*;

      public class Example {

          public Example() {
              // Serialize object to a byte stream
              ByteArrayOutputStream arrOutStream = new ByteArrayOutputStream();
              CSer serObj = new CSer("TestString");
              try {
                  ObjectOutputStream os = new ObjectOutputStream(arrOutStream);
                  os.writeObject(serObj);
                  os.flush();
                  os.close();
              } catch(IOException e) {
                  System.out.println("Serialization has broken."+
                  " Unexpected exception: "+e);
                  e.printStackTrace();
              }

              // Deserialize object
              ByteArrayInputStream arrInStream = new
                  ByteArrayInputStream(arrOutStream.toByteArray());
              CSer o = null;
              try {
                  ObjectInputStream is = new ObjectInputStream(arrInStream);
                  o = (CSer) is.readObject();
                  is.close();
              } catch(IOException e) {
                  System.out.println("Deserialization has broken. "+
                  "Unexpected exception: "+e);
                  e.printStackTrace();
              } catch (ClassNotFoundException e) {
                  System.out.println("Deserialization has broken. "+
                  "Unexpected exception: "+e);
                  e.printStackTrace();
              }

              // Obtain ObjectStreamClass of deserialized object
              ObjectStreamClass osc = o.getObjectStreamClass();
              testField(osc.getField("strFieldShared"));
          }

          public void testField(ObjectStreamField osf) {
              System.out.println("Results for ObjectStreamField of "+
              "type String:");
              System.out.println("Returned typeString: "+osf.getTypeString());
              System.out.println("Returned type: "+osf.getType());
              System.out.println("Expected type: class java.lang.String");
          }

          public static void main(String[] args) {
              Example e = new Example();
          }

      }

      ------------------- end of Example.java ---------------

      ----------------- CSer.java ---------------------------

      import java.io.*;

      class CSer implements Serializable {
          String strFieldShared;
          ObjectInputStream.GetField readFields = null;

          public CSer(String s) {
              strFieldShared = s;
          }

          private static final ObjectStreamField[] serialPersistentFields = {
              new ObjectStreamField("strFieldShared", String.class, false),
          };

          private void readObject(ObjectInputStream s)
                      throws IOException, ClassNotFoundException {
              readFields = s.readFields();
              strFieldShared = (String) readFields.get("strFieldShared",
              "No such field");
          }

          private void writeObject(ObjectOutputStream s)
                      throws IOException {
              ObjectOutputStream.PutField wroteFields = s.putFields();
              wroteFields.put("strFieldShared", strFieldShared);
              s.writeFields();
          }

          public ObjectStreamClass getObjectStreamClass() {
              return readFields.getObjectStreamClass();
          }
      }

      ---------------- end of CSer.java ---------------------

      ======================================================================

            Unassigned Unassigned
            pavsunw Pav Pav (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: