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 ---------------------
======================================================================
- duplicates
-
JDK-4332690 ObjectStreamField.getType() broken for incoming ObjectStreamFields
- Closed
- relates to
-
JDK-4427881 ObjectInputStream.GetField.get() broken for primitive fields
- Closed