javajavassistserialversionuid

How modify a serialVersionUID at runtime


I'm using a third party library to connect to an external system, everything was working fine but suddenly i'm having serialization errors

java.io.InvalidClassException: com.essbase.api.base.EssException; local class incompatible: stream classdesc serialVersionUID = 9031463779199124957, local class serialVersionUID = -3889253203225845868

i'm pretty sure that my library is still compatible with the external system so i tried to use javassist to modify the serialVersionUID of my local com.essbase.api.base.EssException

CtClass cc = pool.get("com.essbase.api.base.EssException");
cc.defrost();
CtField serialField=new CtField(CtClass.longType, "serialVersionUID",cc);
serialField.setModifiers(Modifier.PRIVATE);
serialField.setModifiers(Modifier.STATIC);
serialField.setModifiers(Modifier.FINAL);        
cc.addField(serialField, CtField.Initializer.constant(9031463779199124957l));

The error persists but it says that my local serialVersionUID is now -7214799155395914399 so this is doing something.

Have ay ideas of how to turn my serialVersionUID to 9031463779199124957l ?


Solution

  • I finally make it work, changing the modifiers of the injected Field to this:

    serialField.setModifiers(Modifier.STATIC | Modifier.FINAL);
    

    so this is the working code, and the comunication with the server is now restored:

    ClassPool pool = ClassPool.getDefault();
    pool.insertClassPath(new ClassClassPath(MainTest.class));
    CtClass cc = pool.get("com.essbase.api.base.EssException");
    cc.defrost();
    CtField serialField=new CtField(CtClass.longType, "serialVersionUID",cc);
    serialField.setModifiers(Modifier.STATIC | Modifier.FINAL);
    cc.addField(serialField, CtField.Initializer.constant(9031463779199124957l));
    Class clazz=cc.toClass();