1

Unsure how to frame the question. Here is a try :

I have two aerospike caches (sets) say X, Y.

there are two versions of a jar each has a class com.xyz.MyClass

jar1 :

com.xyz.MyClass{
    com.abc.Request request;
    ...
}

jar2 :

com.xyz.MyClass{
    com.pqr.Request request;
    ...
}

Now Some objects of MyClass from jar1 are stored in cache X and some objects of MyClass from jar2 are stored in cache Y.

I want to deserialize data from both the caches, perform some computation and store in a db.

I can't serialize as the fully qualified name of MyClass in both jars is same.

I will remove one of the caches (but that will take some time). So, Hoping for a temporary fix to handle this situation for now.

Can i handle this in a nice way?

Maybe deserialize into something intermediary(that doesn't care about packages, but only class structure) and then to MyClass.

Note : both the classes have same structure, just package name of a member variable is different.

P.S. : Don't want to use multiple class loaders.

Sachin Sharma
  • 1,446
  • 4
  • 18
  • 37

2 Answers2

0

It will take huge amount of magic to achive it, and disallowing classloader magic remove half of your options.

Solution that I can propose is editing serialized data. Format in which object are serialized is known and is part of Java spec. So you can edit this bytes to replace one class name to another, but I suggest you think twice before you start to implement this. It may be not so trivial task.

talex
  • 17,973
  • 3
  • 29
  • 66
  • i think i can get benefit from the fact that the structure of both the classes is same. Maybe this could work : java deserialization into hashmap and then serialize hashmap using jaskson and deserialize into a class from either jars. (assuming jaskson doesn't care about the package info, just the structure.). Will try and post if that works. – Sachin Sharma Sep 13 '16 at 13:34
  • You need to implemnt "java deserialization into hashmap". There is no library (AFAIK) that will help you with this. You need to implement it by yourself. – talex Sep 13 '16 at 13:38
-1

Generate two different serialVersionUID's and use them in each of the class.

private static final long serialVersionUID = -7000188057019447678L;// Add this in MyClass of jar1

private static final long serialVersionUID = -3944128180162145709L;// Add this in MyClass of jar2

You can generate your own serialVersionUID's using Eclipse IDE or JDK's serialver tool.

Note: If no serialVersionUID is declared, JVM will use its own algorithm to generate a default SerialVersionUID. you can check the algorithm here.

The default serialVersionUID computation is highly sensitive to class details and may vary from different JVM implementation, and result in an unexpected InvalidClassExceptions during the deserialization process.

Rohit Gaikwad
  • 3,677
  • 3
  • 17
  • 40
  • 1
    How it will help? It simply tells deserializer that it is wrong class and deserializer will throw exception. – talex Sep 13 '16 at 13:39
  • Since, different serialVersionUID is used for both the classes. The objects of jar1 are serialized with serialVersionUID = -7000188057019447678L and that of jar2 with serialVersionUID = -3944128180162145709L. At the time of deserialization, the deserializer will recognize the objects of jar1 & jar2 using serialVersionUID. – Rohit Gaikwad Sep 13 '16 at 13:56
  • 1
    It is sad but it is not working that way. You can check details here: http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it – talex Sep 13 '16 at 14:04