我有一个类作为字节数组的包装器:
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;
}
}
在我的代码中,有很多方面我会为很多值(大约50-70)做这样的事情:
legacyWrapper1.setName(legacyWrapper2.getName()); //marshalling/unmarshalling, OMG!
legacyWrapper1.setP1(legacyWrapper2.getP1());
legacyWrapper1.setP2(legacyWrapper2.getP2());
//... a lot of assignements ...
现在,实际上我只需要一个这些实例的字节包装器,所以我对其余的使用普通的Pojo。我重用代码的想法是创建另一个像这样的包装器:
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
}
这样:
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 ...
我认为,使用这个新的包装器,执行时间本来会更好......猜怎么着? 他们变得更糟!!
如果现在,与之前的相同的代码只进行编组,那怎么可能呢?也许它与动态绑定有关,因为JVM需要时间来确定在被覆盖的和原始的方法之间使用哪种方法?