变量明确存在时,Javassist没有此类字段 [英] Javassist no such field when variable clearly exists

查看:88
本文介绍了变量明确存在时,Javassist没有此类字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用javassist将代码注入minecraft 1.8 jar中. insertBefore & insertAfter 方法可以很好地工作.但是方法中的insert不能按预期工作.我收到此错误:

解决方案

好的,我知道了.事实证明,javassist不知道是否定义了局部变量,并且假定 var1 是一个字段.我通过创建自己的变量并将其设置为与 var1 相同的值来解决了该问题.

来源: http://jboss-javassist.github.io/javassist/tutorial/tutorial2.html

I am trying to inject code into the minecraft 1.8 jar using javassist. The insertBefore & insertAfter methods work perfectly fine. But the insert at method does not work as expected. I am getting this error: https://gist.github.com/czaarek99/dda36426318f331ce6b0

Here is the code that handles the injection:

if (className.equals(mappingManager.getMapping(CommonMappings.MINECRAFT_CLASS))) {

    CtClass ctClass = classPool.get(mappingManager.getMapping(CommonMappings.MINECRAFT_CLASS, true)); //returns "bsu"

    CtMethod tickMethod = ctClass.getDeclaredMethod(mappingManager.getMapping(CommonMappings.RUN_TICK_METHOD)); //returns "r"
    tickMethod.insertBefore("EventManager.call(new TickEvent(TickEvent.PRE_UPDATE));");
    tickMethod.insertAfter("EventManager.call(new TickEvent(TickEvent.POST_UPDATE));");

    String varName = mappingManager.getMapping(CommonMappings.KEYBOARD_KEYCODE_VARIABLE); //returns "var1"

    int lineToInsertAt = Integer.valueOf(mappingManager.getMapping(CommonMappings.KEYBOARD_NEXT_LINE)); //returns "1372"
    tickMethod.insertAt(lineToInsertAt, true, "KeyPressEvent keyPressEvent = new KeyPressEvent("+ varName +");EventManager.call(keyPressEvent);");;

    CtMethod runGameMethod = ctClass.getDeclaredMethod(mappingManager.getMapping(CommonMappings.START_GAME_METHOD)); //returns "aj"
    runGameMethod.insertAfter("InjectClient.getInstance().loadModules();");

    byte[] newCode = ctClass.toBytecode(); //line that throws the error
    ctClass.detach();

    return newCode;
} 

I have commented the lines where it grabs a mapping, essentially these are minecraft obfuscated variable & function names since this is what I will be inserting to.

Alright, so the logical explanation is that var1 does not exist? That's not true. If we have a look at the code for the obfuscated bsu class we can see this:

解决方案

Alright I figured it out. Turns out javassist does not know if a local variable is defined and it assumed var1 was a field. I solved it by just creating my own variable and setting the it to the same value as var1 has.

Source: http://jboss-javassist.github.io/javassist/tutorial/tutorial2.html

这篇关于变量明确存在时,Javassist没有此类字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆