Java OW2 ASM 使用接口访问字节码类
我一直在尝试在网络上搜索指南/帮助,以通过字节码类在 ASM 中转换为的接口来访问字节码类。 我已经在名为 powerbot (RSBot) 的 RuneScape 机器人上看到了这一点,我也从 https://github.com 获得了该来源/powerbot/RSBot 但我无法理解它是如何完成的,因为当我将类转换为包含每个方法的“骨架”的接口时,我得到了类转换异常。我知道像你这样的想法一定是愚蠢的只是将一个类转换为接口.. 我知道必须有一些准备工作,但在 ASM 书籍/电子书(http://download.forge.objectweb.org/asm/asm-guide.pdf)中,据我所读,没有任何相关内容。我对此真的很感兴趣,不是为了制作一个新的 RS 机器人,而是为了学习这个很酷的库来替换我需要的 BCEL。我将给您一个简单的例子,以更好地理解这个问题!
这里我有一个名为“helloSO”的类
public class helloSO {
public void doSomething(){
System.out.println("I did something! :D");
}
}
,这里我有一个名为“helloSO”的接口,
public interface helloSO {
public void doSomething();
}
我也希望能够将我读取并定义为 ASM 的类的 .class 转换为该接口,以便能够从该实例调用方法和他们的名字..我希望我解释得足够好。如果您愿意,可以检查 RSBot 以更好地了解我的意思。 :) 所以我要问的是;我需要做什么准备,从哪里可以了解?我不是要求你为我创建类适配器或任何东西,只是建议和帮助:) 提前致谢!
I've been trying to search around web for guides/help for accessing byte code classes via interface that the byte code class is casted to in ASM.
I've seen this done on RuneScape bot called powerbot (RSBot) which source I also got from https://github.com/powerbot/RSBot but i cannot understand how is it done as I get class cast exception when casting a class to interface which contains the "skeleton" of every method.. I know it must be stupid to think like you could just cast a class to interface..
I know there must be some preparements or so but in ASM book/ebook (http://download.forge.objectweb.org/asm/asm-guide.pdf) theres nothing about it as far as i've readed. I am really interested in this, not to make a new RS bot but to learn this cool library to replace BCEL in my needs. I'll give you a quick example to understand this problem better!
Here i have class called "helloSO"
public class helloSO {
public void doSomething(){
System.out.println("I did something! :D");
}
}
and here i have interface called "helloSO" also
public interface helloSO {
public void doSomething();
}
i would like to be able to cast the .class i read and define to class with ASM to that interface to be able to call the methods from that instance with their names.. I hope i explained this well enought. If you want you can check the RSBot to see better what i mean. :) So what i am asking is; what preparations I have to do and where can i learn about them? I am not asking you to create me class adapter or anything just advice and help :)
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的类已经包含匹配的方法,那么使用
ClassAdapter
添加接口实际上非常简单。您只需重写visit
方法,该方法会传递一个接口名称数组,并在委托给超级实现之前添加另一个接口:我在这里使用
Set
来实现接口,如果该类已经实现了新接口。接口使用其内部名称来命名,这仅意味着包名称中的点被斜杠替换。Adding an interface using a
ClassAdapter
is actually pretty simple if your class already contains a matching method. You just have to override thevisit
method which gets passed an array of interface names and add another interface before delegating to the super implementation:I'm using a
Set
here for the interfaces in case the class already implements the new interface. The interfaces are named using their internal names, which just means that dots in the package name get replaced by slashes.