PDA

View Full Version : Hessian deserializer problem by inherited Fields wihr same name


Habib
03-07-2013, 12:58 PM
Class B inheritance class A and both classes have a Field “id”. Hessian deserializer overwrite “id” from class B.

public class A implements java.io.Serializable{

protected long id = 100;

public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}

}

public class B extends A implements java.io.Serializable {

private long id = 200;

@Override
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}

public long getSuperId() {
return super.getId();
}

}

public class Serializertest
{

/**
* @param args
*/
public static void main(String[] args)
{

ByteArrayOutputStream bos = new ByteArrayOutputStream();

Hessian2Output out = new Hessian2Output(bos);
try
{
out.startMessage();

out.writeInt(1);
B b = new B();
out.writeObject(b);
out.close();

byte[] data = bos.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(data);
Hessian2Input in = new Hessian2Input(bin);
in.startMessage();
ArrayList list = new ArrayList();
int length = in.readInt();

for (int i = 0; i < length; i++)
{
list.add(in.readObject());
}

in.completeMessage();
in.close();
bin.close();

b = (B) list.get(0);
System.out.println("id from class B =" + b.getId());
System.out.println("id from class A =" + b.getSuperId());
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

---- Output---
id from class B =100
id from class A =0