JDK-4264920 : Bug in reflection/serialization combination with primitive types
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io:serialization
  • Affected Version: 1.2.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 1999-08-23
  • Updated: 1999-08-23
  • Resolved: 1999-08-23
Related Reports
Duplicate :  
Description

Name: rlT66838			Date: 08/23/99


When a primitive type's class is serialized, this is not 
received properly upon reading (only tested this to/from file
serialization).

Steps:
serialization:
1. Class c = Integer.TYPE // get the class of the primitive
2. objectOutputStream.writeObject (c); // serizalize

deserialization:
1. Class c = (Class) objectInputStream.readObject ();
output:
java.lang.ClassNotFoundException: int
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:347)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:232)
        at sr.firm.welch.brian.Tester.main(Tester.java:23)

Code that demonstrates this:
> Begin code example
package sr.firm.welch.brian;

import java.io.*;
import java.lang.reflect.*;

public class Tester
{
   public static void main (String args[])
   {
      try
      {
         // serialize
         FileOutputStream fos = new FileOutputStream ("intClass.ser");
         ObjectOutputStream oos = new ObjectOutputStream (fos);
         Class c = Integer.TYPE;
         oos.writeObject (c);
         oos.close ();
         fos.close ();

         // deserialize
         FileInputStream fis = new FileInputStream ("intClass.ser");
         ObjectInputStream ois = new ObjectInputStream (fis);
         c = (Class) ois.readObject (); // This is where the exception is thrown
         System.out.println ("This is never reached");
         ois.close ();
         fis.close ();
      }
      catch (Exception except)
      {
         except.printStackTrace ();
      }
   }
}
> End code example


version info:
java -version
java version "1.2.1"
Classic VM (build JDK-1.2.1-A, native threads)

java -fullversion
java full version "JDK-1.2.1-A"

relevant configuration (though I do not believe this has impact
on the bug):
- Intel Pentium II 400
- 128 MB RAM
- OS=Windows NT 4.0 SP4
(Review ID: 93724) 
======================================================================

Comments
WORK AROUND Name: rlT66838 Date: 08/23/99 The only thing I can think of is temporarily write a flag to the stream indicating if the serialization concerns serializing a primitive or a true class, and (for the classes that have a primitive) test for this before serialization. EG: objectOutputStream.writeObject (new Boolean (true)); // a primitive objectOutputStream.writeObject (new Integer (1999)); Boolean primitive = (Boolean) objectInputStream.readObject (); if (primitive.booleanValue ()) { Object object = ois.readObject (); if (object instanceOf Integer) { Integer intObject = (Integer) object; intvalue = intObject.intValue (); } else if (object instanceof Long) { // ... etc. etc. etc for every typed object } } Quite bulky, but effective... ======================================================================
11-06-2004

EVALUATION This is a duplicate of 4171142 naveen.sanjeeva@eng 1999-08-23
23-08-1999