Make Java runtime ignore serialVersionUIDs?

前端 未结 5 1007
暗喜
暗喜 2020-11-29 05:13

I have to work with a large number of compiled Java classes which didn\'t explicitly specify a serialVersionUID. Because their UIDs were arbitrarily generated by the compile

5条回答
  •  [愿得一人]
    2020-11-29 05:49

    You could possibly use Aspectj to 'introduce' the field into each serializable class as it is loaded. I would first introduce a marker interface into each class using the package and then introduce the field using a Hash of the class file for the serialVersionUID

    public aspect SerializationIntroducerAspect {
        
       // introduce marker into each class in the org.simple package
       declare parents: (org.simple.*) implements SerialIdIntroduced;
    
       public interface SerialIdIntroduced{}
       
       // add the field to each class marked with the interface above.
       private long SerialIdIntroduced.serialVersionUID = createIdFromHash(); 
       
       private long SerialIdIntroduced.createIdFromHash()
       {
           if(serialVersionUID == 0)
           {
               serialVersionUID = getClass().hashCode();
           }
           return serialVersionUID;
       }
    }
    

    You will need to add the aspectj load time weaver agent to the VM so it can weave the advice into your existing 3rd party classes. Its funny though once you get around to setting Aspectj up, its remarkable the number of uses that you will put to.

    HTH

    ste

提交回复
热议问题