public final class SerializabilityTester extends Object
Text taken from Effective Java by Joshua Bloch
The object Serialization API provides a framework for encoding (Serializing) objects as byte streams and reconstructing (Deserializing) objects from their byte-stream encodings. Once an object has been serialized, its encoding can be transmitted from one running virtual machine to another or stored on disk for later deserialization.
private void readObjectNoData() throws InvalidObjectException {
throw new InvalidObjectException("Stream data required");
}
InvalidClassException at runtime. This
number must be changed when backwards compatibility is broken.
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
// Ensure invariants and security
// ...
}
null for reference fields, zero for numeric
primitive fields, and false for boolean fields. If these values are unaceptable for any transient
fields, provide a readObject method (like the one above) that restore transient fields to
acceptable values. Alternatively, these fields can be lazily initialized the first time they are
used.
// synchronized method if other methods use synchronization to read the entire state of the object
private synchronized void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
}
Serializable class. This eliminates the serial version UID as potential source if
incompatibility. There is also a small performance benefit: If no serial version UID is provided,
an expensive computation is required to generate one at runtime.readObject method is effectively another public constructor, and it demands all of
the same care as any other constructor. Check its arguments for validity, make defensive copies,
etc. Loosely speaking, readObject is a constructor that takes a byte stream as its sole
parameter. Throw InvalidObjectException if an invariant is broken.writeUnshared and
readUnshared methods from ObjectOutputStream and ObjectInputStream; they
are typically faster that defensive copying, but they don't provide the necessary safety
guarantee.readObject method must not invoke an overridable method, directly or
indirectly. Overriding methods will run before the subclass state is deserialized.readResolve. If you depend on
readResolve for instance control, all instance fields with object reference types must be
declared transient. Otherwise it is possible for a determined attacker to secure a reference to
the deserialized object before its readResolve method runs. The use of
readResolve is not obsolete. If you have to write a serializable
instance-controlled (singleton) class whose instances are not known at compile time, you will not
be able to represent the class as an enum type.readResolve is significant.. If it is placed in
a final class it should be private. For nonfinal classes, if it is private it will not apply to
any subclasses. If it is package-private, it will apply only to subclasses in the same package.
If it is protected or public, it will apply to all subclasses that do not override it. If
readResolve is protected or public and a subclass does not override it, deserializing a
serialized subclass instance will produce a superclass instance, which is likely to case
ClassCastException.| Modifier and Type | Class and Description |
|---|---|
static interface |
SerializabilityTester.SemanticCompatibilityVerifier<T extends Serializable>
Semantic compatibility verifier.
|
| Modifier and Type | Method and Description |
|---|---|
static <T extends Serializable> |
testSerialization(T serializable,
SerializabilityTester.SemanticCompatibilityVerifier<T> semanticCompatibilityVerifier)
Test serialization's binary and semantic compatibility.
|
public static <T extends Serializable> void testSerialization(T serializable, SerializabilityTester.SemanticCompatibilityVerifier<T> semanticCompatibilityVerifier)
T - type of the serializable entityserializable - serializable objectsemanticCompatibilityVerifier - semantic compatibility verifier to assert that
original and its replica are semantically compatible. If null is
provided just binary compatibility will be tested (The only errors that will be
caught via this test is situations where an non-serializable object reference with
a non-null value is used - NotSerializableException). Thus it is highly
recommended to pass a non-null semanticCompatibilityVerifier.Copyright © 2015. All rights reserved.