从内存中获取Java类字节码(经过多次转换) [英] Get Java class bytecode from memory(After multiple transforms)

查看:241
本文介绍了从内存中获取Java类字节码(经过多次转换)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Minecraft开发coremod,并在加载许多类时对其进行转换。
但是,问题是,有多个coremod也会转换我自己的相同类,并且出现了一些我想研究的奇怪行为。



然后是问题,在多次转换后如何检查字节码?
对其进行转换时,我只得到了一个字节[]输入,并通过ASM运行,然后返回修改后的字节码。类加载后,将类字节码保存到.class文件中,然后从那里进行检查。但是我似乎找不到任何方法在加载类后实际获取字节码。
我能找到的最接近的是getResource,但是它返回的是转换前的字节码,不是我想要的。



TLDR:
在修改并加载类之后,如何获取该类的字节码?
-无法使用ClassLoader.getResource,因为它会返回未修改的版本。
-在加载期间无法获取它,因为我想捕捉我自己之后发生的转换。



是否有一些外部程序可以转储内置的-内存字节码还是什么?



希望有人可以帮助我解决这个问题)

解决方案

据我所知,Java代理提供了用于运行时访问字节码的唯一接口。这也是首先创建类文件转换器的方式,因此您应该已经在使用它。只需修改它以转储类文件即可。



编辑:根据文档的转换顺序,

 无重新转换能力的变压器
无重新转换能力的原始变压器
有重新转换能力的变压器
有重新转换能力的原始变压器

因此理想情况下,您需要具有可重新转换的本机转换器。但是编写本机代码很痛苦。将您的转换器设置为启用重新转换会大有帮助,但是稍后注册的另一个转换器仍然可以在您运行之后运行。我能想到的最好的选择是再次注册您的代理商。或查找所有注册变压器的电话,然后插入您的电话。


I'm working on a coremod for Minecraft, and transform a lot of the classes as they are loaded. The problem however is that there are multiple coremods which also transform the same classes that I am, and I am getting some strange behavior that I want to look into.

Then comes the problem, how do I inspect the bytecode after it has been transformed multiple times? When I transform it I just get an byte[] input that I run trough ASM and then return my modified bytecode.

My idea was to just to dump the class bytecode to a .class file after the class was loaded, and inspect it from there. But I can't seem to find any way to actually get the bytecode after the class is loaded. The closest I can find is getResource, but that returns the bytecode as it was BEFORE it was transformed, not what I want.

TLDR: How do I get the bytecode of a class AFTER it has been modified and loaded? -Can't use ClassLoader.getResource as it returns unmodified version. -Can't get it during load time as I want to catch transforms happening after my own.

Is there some external program that can dump the in-memory bytecode or something?

Hoping someone can help me with this =)

解决方案

As far as I know, the only interface for runtime access to bytecode is provided by Java Agents. This is also how you create classfile transformers in the first place, so you should already be using one. Just modify it to dump the classfile.

Edit: As far as the order of transformation, according to the docs, it is

Retransformation incapable transformers
Retransformation incapable native transformers
Retransformation capable transformers
Retransformation capable native transformers

So ideally you'd want a retransformation capable native transformer. But writing native code is a pain. Setting your transformer to enable retransformation will help a lot, but it's still possible for another retransformer registered later to run after you. The best option I can think of is to register your agent again. Or find all calls to register a transformer and insert yours afterwards.

这篇关于从内存中获取Java类字节码(经过多次转换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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