Java - serialVersionUID

About

The serialVersionUID is an ID that must be unique in order to serialize and de-serialize an object.

Management

Generate

jdk/bin/serialver -classpath bin java.util.ArrayList

Algorithm

The whole algorithm is specified in the Object Serialization Specification, Section 4.6, Stream Unique Identifiers

Code: The SerialVer.java use ObjectStreamClass.getSerialVersionUID to generate the UID on Line 215.Extract of ObjectStreamClass.java

ByteArrayOutputStream bout = new ByteArrayOutputStream();
// bout collect fingerprint information of the class
MessageDigest md = MessageDigest.getInstance("SHA");
byte[] hashBytes = md.digest(bout.toByteArray());
long hash = 0;
for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) {
	hash = (hash << 8) | (hashBytes[i] & 0xFF);
}
return hash;

Powered by ComboStrap