java 将 Class 对象转换为字节

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2036108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:03:11  来源:igfitidea点击:

convert Class object to bytes

javaclassloader

提问by Brian Harris

If I have a Classinstance at runtime, can I get its byte[] representation? The bytes I'm interested in would be in the Class file format, such that they'd be valid input to [ClassLoader.defineClass][3].

如果我在运行时有一个Class实例,我可以得到它的 byte[] 表示吗?我感兴趣的字节将采用Class 文件格式,这样它们就可以成为 [ClassLoader.defineClass][3] 的有效输入。

[3]: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ClassLoader.html#defineClass(java.lang.String, byte[], int, int)

[3]:http: //java.sun.com/j2se/1.5.0/docs/api/java/lang/ClassLoader.html#defineClass(java.lang.String, byte[], int, int)

EDIT: I've accepted a getResourceAsStream answer, because it's very simple and will work most of the time. ClassFileTransformer seems like a more robust solution because it doesn't require that classes be loaded from .class files; it would handle network-loaded classes for example. There's a few hoops to jump through with that approach, but I'll keep in in mind. Thanks all!

编辑:我已经接受了 getResourceAsStream 答案,因为它非常简单并且大部分时间都可以使用。ClassFileTransformer 似乎是一个更强大的解决方案,因为它不需要从 .class 文件加载类;例如,它将处理网络加载的类。使用这种方法需要克服一些困难,但我会牢记在心。谢谢大家!

采纳答案by Alex Miller

You can usually just load the class as a resource from the Classloader.

您通常可以将类作为资源从类加载器加载。

Class c = ...
String className = c.getName();
String classAsPath = className.replace('.', '/') + ".class";
InputStream stream = c.getClassLoader().getResourceAsStream(classAsPath);

I would probably recommend using something from Apache commons-io to read the InputStream into a byte[], but IOUtils.toByteArray()should do the trick. Writing that code is really easy to get wrong and/or make slow.

我可能会建议使用 Apache commons-io 中的一些东西将 InputStream 读入byte[],但IOUtils.toByteArray()应该可以解决问题。编写该代码真的很容易出错和/或变慢。

回答by Suraj Chandran

You can try java instrumentation! In particular ClassFileTransformermaybe of interest to you!!

你可以试试java检测!特别是ClassFileTransformer 您可能会感兴趣!!

You simply override the transform method (from ClassFileTransformer), and your transform method will be called before each class is loaded. So then you cna do whatever class bytes.

您只需覆盖转换方法(来自 ClassFileTransformer),并且您的转换方法将在每个类加载之前被调用。那么你可以做任何类字节。

回答by Tom Hawtin - tackline

In general you can't go back like that. However, for some class loaders you may be able to get the resource file be taking the fully qualified class name, replacing .with /and appending .class(so mypackage.MyClassbecomes mypackage/MyClass.class(remember, may be case sensitive)).

一般来说,你不能就那样回去。但是,对于某些类加载器,您可能能够使资源文件采用完全限定的类名,替换./并附加.class(因此mypackage.MyClass变为mypackage/MyClass.class(记住,可能区分大小写))。

回答by stacker