-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSerializeNullTest.java
More file actions
40 lines (33 loc) · 1.22 KB
/
SerializeNullTest.java
File metadata and controls
40 lines (33 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.*;
// Test serializing a class with non-serializable field of null value
// If the Person field in Animal class is non-serializable and its value is null,
// the Animal object can still be serialized without throwing NotSerializableException
// However if Person is not null(animal1.setOwner(..)) is called, then that exception will be thrown.
// To resolve it, just declare Person class to be Serializable.
public class SerializeNullTest {
public static void main (String[] args) {
Animal animal1 = new Animal(10, 30);
//animal1.setOwner(new Person("Yalun", 25));
byte[] animalBytes = null;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(animal1);
animalBytes = bos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
Animal animal2 = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(animalBytes);
ObjectInputStream in = new ObjectInputStream(bis);
animal2 = (Animal)in.readObject();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
System.out.println(animal2.getClass());
System.out.println(animal2.toString());
}
}