I have a class which acts as a wrapper for a byte array:
public class LegayWrapper{
private byte[] data;
public void setName(String name){
MarshalUtils.addName(data,15,30,name,"ENCODING");
//this util not only puts the data inside of the byte array
// it also converts it in a specific representation
}
public String getName(){
return MarshalUtils.getNameAsString(data,15,30,"ENCODING");
}
...
public byte[] getBytes(){
return data;
}
}
In my code there are multiple areas in which I do something like this for A LOT of values (about 50-70):
legacyWrapper1.setName(legacyWrapper2.getName()); //marshalling/unmarshalling, OMG!
legacyWrapper1.setP1(legacyWrapper2.getP1());
legacyWrapper1.setP2(legacyWrapper2.getP2());
//... a lot of assignements ...
Now, in reality I need the byte wrapper only for one of these instances, so I though of using a normal Pojo for the rest. My idea to reuse the code was to create another wrapper made like this one:
public class PojoWrapper extends LegacyWrapper{
private String name;
@Overrides
public String getName(){
return name;
}
@Overrides
public void setName(String name){
this.name = name;
}
//...this for every property p1,p2,etc
}
so that:
LegacyWrapper legacyWrapper1 = new LegacyWrapper(); //this one is a wrapper
legacyWrapper1.setBytes(bytes); //initialing it with byte data
LegacyWrapper legacyWrapper2 = new PojoWrapper(); //this is a pojo
legacyWrapper2.setProperty1("aProperty"); //initializing it with normal data
//...doing stuff....
legacyWrapper1.setName(legacyWrapper2.getName()); //only marshalling
legacyWrapper1.setP1(legacyWrapper2.getP1());
legacyWrapper1.setP2(legacyWrapper2.getP2());
//... a lot of assignements ...
I though that, with this new wrapper, the execution times would have been better...guess what? They got worse!!
How is that possibile if now, the same code as before, only does marshalling? Maybe it has something to do with the dynamic binding because the JVM take time to figure out which method to use between the overridden and the original one?