将给定的类(比如说 java.lang.Object)转换为字节数组。是否可以?

发布于 2024-12-13 01:55:53 字数 141 浏览 3 评论 0原文

鉴于类加载器接受给定类的字节数组作为输入,返回一个 Class,我想知道是否可以执行相反的操作,即传递一个 < code>Class 并获取其字节数组?

请记住,我不是在谈论序列化!

Given that class loaders accept to take as input a byte array of a given class, returning a Class<?>, I wonder whether it is possible to do the reverse, that is, to pass a Class<?> and get its byte array?

Keep in mind that I'm not talking about serialization!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜宝宝 2024-12-20 01:55:53

您可以使用 ClassLoader.getResourceAsInputStream() 但不能保证这与加载的字节相同。我不相信加载的实际字节存储在任何地方。

You can use ClassLoader.getResourceAsInputStream() however it is not guaranteed this will be the same bytes as that which was loaded. I don't believe the actual bytes loaded are stored anywhere.

流心雨 2024-12-20 01:55:53

据我所知,没有标准方法。但是您可以实现自己的委托类加载器,根据名称和字节码跟踪加载的类。

您的用例是什么? - 将代码传输到远程应用程序?

No standard way I know of. But you might implement your own delegating classloader, keeping track of classes loaded in terms of name and bytecode.

What would be your use case? - Transferring code to a remote application?

牵你的手,一向走下去 2024-12-20 01:55:53

ClassLoader 实现必须理解它支持的 Java 语言规范版本的类的表示形式。因此它必须包含获取字节数组并将其转换为 Class 对象的逻辑,或者扩展一些这样做的 ClassLoader。然而,Java 定义的抽象 ClassLoader 类没有任何可以从 Class 返回 byte[] 的方法。某些实现可能有这个(可能是引导程序、系统或其他类加载器),但这充其量是特定于实现的,而不是依赖的东西。

如果您需要这种功能,一种方法是创建您自己的 ClassLoader 实现并重写 defineClass 方法,以便输入字节数组映射到 Class 对象,以便以后可以检索它们。

由于编译器将源代码转换为类文件(基本上是 byte[]),并且类加载器将类文件转换为 Class 对象,因此链中没有任何内容直接适合从 Class< /code> 到一个字节[]。但是,有了执行字节码编织、运行时字节码生成等功能的东西,您可能会找到可以做到这一点的东西。

A ClassLoader implementation must grok the representation of a class for the Java Language Specification version it supports. So it has to contain the logic to take a byte array and turn this into a Class object, or extend some ClassLoader that does. However, the abstract ClassLoader class that Java defines doesn't have any method that can return a byte[] from a Class. It is possible that some implementations have this (perhaps the bootstrap, system or other class loader), but that'd be implementation-specific at best and not something to rely on.

If you require this kind of functionality, one way to do it would be to create your own ClassLoader implementation and override the defineClass methods in such a way that the input byte arrays are mapped to Class objects so they can later be retrieved.

Since a compiler turns source code into class files (basically the byte[]) and a class loader turns class files into Class objects, there's nothing in the chain that's directly suitable to go from Class to a byte[]. But with the stuff out there that does byte code weaving, runtime byte code generation and the like, you might find something that can do this.

秋千易 2024-12-20 01:55:53

这是 BCEL 的一些尝试:

JavaClass clazz = Repository.lookupClass("foo.bar.YourClass");
byte[] bytes = clazz.getBytes();

在阅读您的问题之前我不知道答案,但我知道我应该检查以下库,因为它们支持 Java类文件格式

  • BCEL
  • javassist
  • asm

如果上述方法不起作用,请在这些库中查找其他内容。

Here's something to try with BCEL:

JavaClass clazz = Repository.lookupClass("foo.bar.YourClass");
byte[] bytes = clazz.getBytes();

I didn't know the answer prior to reading your question, but I knew I should check the following libraries, because they support the Java class file format:

  • BCEL
  • javassist
  • asm

If the above doesn't work, look for something else in these libraries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文