我需要在方法中注入一些字节码,但我对此有点陌生。我以前只需找到RETURN操作码并在那里注入就可以实现它,但我的新需求更具体:
public void onCollideWithPlayer(EntityPlayer par1EntityPlayer)
{
if (!this.worldObj.isRemote)
{
if (this.field_70532_c == 0 && par1EntityPlayer.xpCooldown == 0)
{
par1EntityPlayer.xpCooldown = 2;
this.playSound("random.orb", 0.1F, 0.5F * ((this.rand.nextFloat() - this.rand.nextFloat()) * 0.7F + 1.8F));
par1EntityPlayer.onItemPickup(this, 1);
par1EntityPlayer.addExperience(this.xpValue);
//NEED TO INJECT HERE
this.setDead();
}
}
}
我已经评论了需要注入字节码的地方。我应该为注射寻找什么操作码?
假设setDead
是一个非私有虚拟方法,则需要查找invokevirtual
操作码*。但是,由于函数中有多个方法调用,您需要按调用的函数来消除歧义,这意味着要查找相关的常量池索引。希望您使用的任何工具或框架都有帮助程序代码来为您完成这项工作。
如果setDead
是私有的,那么您将需要invokespecial
。如果出于某种原因它是静态的(是的,如果你想的话,你可以通过this
调用静态方法),那么你需要寻找invokestatic
。