程式扎記: [ Java 常見問題 ] Make Java runtime ignore serialVersionUIDs?

標籤

2014年1月13日 星期一

[ Java 常見問題 ] Make Java runtime ignore serialVersionUIDs?

來源自 這裡
Question:
I have to work with a large number of compiled Java classes which didn't specify explicitly specify a serialVersionUID. Because their UIDs were arbitrarily generated by the compiler, many of the classes which need to be serialized and deserialized end up causing exceptions, even though the actual class definitions match up. (This is all expected behavior, of course.)

It is impractical for me to go back and fix all of this 3rd-party code.

Therefore, my question is: Is there any way to make the Java runtime ignore differences in serialVersionUIDs, and only fail to deserialize when there are actual differences in structure?

Answer:
If you have access to the code base, you could use the SerialVer task for Ant to insert and to modify the serialVersionUID in the source code of a serializable class and fix the problem once for all.

If you can't, or if this is not an option (e.g. if you have already serialized some objects that you need to deserialize), one solution would be to extend ObjectInputStream.Augment its behavior to compare the serialVersionUID of the stream descriptor with the serialVersionUID of the class in the local JVM that this descriptor represents and to use the local class descriptor in case of mismatch. Then, just use this custom class for the deserialization. Something like this (credits to this message):
  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.io.InvalidClassException;  
  4. import java.io.ObjectInputStream;  
  5. import java.io.ObjectStreamClass;  
  6. import org.slf4j.Logger;  
  7. import org.slf4j.LoggerFactory;  
  8.   
  9.   
  10. public class DecompressibleInputStream extends ObjectInputStream {  
  11.   
  12.     private static Logger logger = LoggerFactory.getLogger(DecompressibleInputStream.class);  
  13.   
  14.     public DecompressibleInputStream(InputStream in) throws IOException {  
  15.         super(in);  
  16.     }  
  17.   
  18.     protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {  
  19.         ObjectStreamClass resultClassDescriptor = super.readClassDescriptor(); // initially streams descriptor  
  20.         Class localClass; // the class in the local JVM that this descriptor represents.  
  21.         try {  
  22.             localClass = Class.forName(resultClassDescriptor.getName());   
  23.         } catch (ClassNotFoundException e) {  
  24.             logger.error("No local class for " + resultClassDescriptor.getName(), e);  
  25.             return resultClassDescriptor;  
  26.         }  
  27.         ObjectStreamClass localClassDescriptor = ObjectStreamClass.lookup(localClass);  
  28.         if (localClassDescriptor != null) { // only if class implements serializable  
  29.             final long localSUID = localClassDescriptor.getSerialVersionUID();  
  30.             final long streamSUID = resultClassDescriptor.getSerialVersionUID();  
  31.             if (streamSUID != localSUID) { // check for serialVersionUID mismatch.  
  32.                 final StringBuffer s = new StringBuffer("Overriding serialized class version mismatch: ");  
  33.                 s.append("local serialVersionUID = ").append(localSUID);  
  34.                 s.append(" stream serialVersionUID = ").append(streamSUID);  
  35.                 Exception e = new InvalidClassException(s.toString());  
  36.                 logger.error("Potentially Fatal Deserialization Operation.", e);  
  37.                 resultClassDescriptor = localClassDescriptor; // Use local class descriptor for deserialization  
  38.             }  
  39.         }  
  40.         return resultClassDescriptor;  
  41.     }  
  42. }  


沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!