0
votes

I want to fetch the variable(extra field) added to a serialized class at runtime. For example

Class A
{
  String name;

}

int travId=123;

"travId" is a extra field added to Class A using javassist,before the object of class A is serialized. While de-serializing , how can i fetch it? I tried to fetch from readObject () and others(like parsing the deserialized object to Json format) , but my attempt was not successful.

And directly implement through the Object throws an no such field found compilation error.

Is there any way i can fetch the travId field using javassist at runtime.

You cannot add fields to already loaded classes. If you do it at loading time or even earlier, the fact that and how you did it, becomes irrelevant. The class has that field then, so if serializing it work, what’s the problem deserializing it? It works exactly the same way. - Holger
yes , I added a field to the class at class loading time. and it is serialized i can clearly read the variable name in my file where the serialized Object is stored. But when i convert that to json (Using gson) , i am getting just the {"name":"name"}. I am not getting travId. But i can clearly read it in the file. - s s
So you are also adding the field at loading time, in the deserialization environment? Can you inspect the field via Reflection? - Holger
I don't know exactly how , but again i tried converting the object to string (using gson) from the scratch and it worked. Now i got the travId. :) - s s
@Holger . and yes we can use javassist to get the fileds. - s s