@ -69,7 +69,7 @@ public abstract class Serializer {
private static final Serializer serializableSerializer = new SerializableSerializer ( ) ;
private static final Serializer serializableSerializer = new SerializableSerializer ( ) ;
private static final Serializer arraySerializer = new ArraySerializer ( ) ;
private static final Serializer arraySerializer = new ArraySerializer ( ) ;
private static short nextId = - 1 ;
private static short nextAvailable Id = - 2 ; // historically the first ID was always -2
private static boolean strictRegistration = true ;
private static boolean strictRegistration = true ;
@ -139,6 +139,7 @@ public abstract class Serializer {
registerClass ( HashMap . class , new MapSerializer ( ) ) ;
registerClass ( HashMap . class , new MapSerializer ( ) ) ;
registerClass ( Hashtable . class , new MapSerializer ( ) ) ;
registerClass ( Hashtable . class , new MapSerializer ( ) ) ;
registerClass ( IdentityHashMap . class , new MapSerializer ( ) ) ;
registerClass ( IdentityHashMap . class , new MapSerializer ( ) ) ;
//registerClass(java.awt.RenderingHints.class, new MapSerializer());
registerClass ( TreeMap . class , new MapSerializer ( ) ) ;
registerClass ( TreeMap . class , new MapSerializer ( ) ) ;
registerClass ( WeakHashMap . class , new MapSerializer ( ) ) ;
registerClass ( WeakHashMap . class , new MapSerializer ( ) ) ;
@ -171,6 +172,33 @@ public abstract class Serializer {
}
}
}
}
private static short nextId ( ) {
// If the ID we are about to return is already in use
// then skip it.
while ( idRegistrations . containsKey ( nextAvailableId ) ) {
nextAvailableId - - ;
}
// Return the available ID and post-decrement to get
// ready for next time.
return nextAvailableId - - ;
}
protected static SerializerRegistration registerClassForId ( short id , Class cls , Serializer serializer ) {
SerializerRegistration reg = new SerializerRegistration ( serializer , cls , id ) ;
idRegistrations . put ( id , reg ) ;
classRegistrations . put ( cls , reg ) ;
log . log ( Level . INFO , "Registered class[" + id + "]:{0} to:" + serializer , cls ) ;
serializer . initialize ( cls ) ;
return reg ;
}
/ * *
/ * *
* Registers the specified class . The failOnMiss flag controls whether or
* Registers the specified class . The failOnMiss flag controls whether or
* not this method returns null for failed registration or throws an exception .
* not this method returns null for failed registration or throws an exception .
@ -182,7 +210,7 @@ public abstract class Serializer {
Class serializerClass = serializable . serializer ( ) ;
Class serializerClass = serializable . serializer ( ) ;
short classId = serializable . id ( ) ;
short classId = serializable . id ( ) ;
if ( classId = = 0 ) classId = - - nextId ;
if ( classId = = 0 ) classId = nextId ( ) ;
Serializer serializer = getSerializer ( serializerClass , false ) ;
Serializer serializer = getSerializer ( serializerClass , false ) ;
@ -191,15 +219,10 @@ public abstract class Serializer {
SerializerRegistration existingReg = getExactSerializerRegistration ( cls ) ;
SerializerRegistration existingReg = getExactSerializerRegistration ( cls ) ;
if ( existingReg ! = null ) classId = existingReg . getId ( ) ;
if ( existingReg ! = null ) classId = existingReg . getId ( ) ;
SerializerRegistration reg = new SerializerRegistration ( serializer , cls , classId ) ;
SerializerRegistration reg = new SerializerRegistration ( serializer , cls , classId ) ;
idRegistrations . put ( classId , reg ) ;
return registerClassForId ( classId , cls , serializer ) ;
classRegistrations . put ( cls , reg ) ;
serializer . initialize ( cls ) ;
log . log ( Level . INFO , "Registered class[" + classId + "]:{0}." , cls ) ;
return reg ;
}
}
if ( failOnMiss ) {
if ( failOnMiss ) {
throw new IllegalArgumentException ( "Class is not marked @Serializable:" + cls ) ;
throw new IllegalArgumentException ( "Class is not marked @Serializable:" + cls ) ;
@ -263,18 +286,9 @@ public abstract class Serializer {
if ( existingReg ! = null ) {
if ( existingReg ! = null ) {
id = existingReg . getId ( ) ;
id = existingReg . getId ( ) ;
} else {
} else {
id = - - nextId ;
id = nextId ( ) ;
}
}
SerializerRegistration reg = new SerializerRegistration ( serializer , cls , id ) ;
return registerClassForId ( id , cls , serializer ) ;
idRegistrations . put ( id , reg ) ;
classRegistrations . put ( cls , reg ) ;
log . log ( Level . INFO , "Registered class[" + id + "]:{0} to:" + serializer , cls ) ;
serializer . initialize ( cls ) ;
return reg ;
}
}
public static Serializer getExactSerializer ( Class cls ) {
public static Serializer getExactSerializer ( Class cls ) {