java中是否可以创建“空白”?使用反射没有无参数构造函数的类实例?
我有一个没有默认构造函数的类。我需要一种方法来获取此类的“空白”实例。 “空白”意味着实例化后所有类字段都应具有默认值 像 null、0 等。
我问是因为我需要能够序列化/反序列化大对象树。而且我无法访问该对象类的源,并且类既没有默认构造函数,也没有实现可序列化。尝试序列化此类结构可能不是一个好主意,但替代方案是将其转换为更容易序列化的结构。
I have a class which has not default constructor. And I need a way to get 'blank' instance of this class. 'blank' means that after instantiation all class fields should has default values
like null, 0 etc.
I'm asking because I need to be able serialize/desirialize big tree of objects. And I have no access to sources of this objects classes and classes has neither default constructors nor implements serializable. It is likely not very good idea to try to serialize such structure but the alternative is to convert it to something more easily serializable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于标准反射,不行,但是有一个库可以为您做到这一点:objenesis。
它专门设计用于实例化没有默认构造函数的类,并且由 xstream 等其他序列化库使用。
注意:在这些情况下可能不会调用构造函数(但这可能就是您想要的)。
With standard reflection, no, but there is a library that can do it for you: objenesis.
It's specifically designed to instantiate classes without default constructors, and it's used by other serialization libraries like xstream.
Note: the constructor might not be called in these cases (but that's presumably what you want).
将类实例作为变量 clazz 提供:
如 http://www.javaspecialists.eu/archive/Issue175.html
Having Class instance provided as variable clazz:
as described in http://www.javaspecialists.eu/archive/Issue175.html
您的解决方案将特定于 JVM。
如果您需要便携式解决方案,请使用第三方库。
对于 Sun 的 JVM v1.5,您可以这样做:
XStream 中的相关类是:
Your solution will be JVM specific.
If you need a portable solution use a 3rd party library.
For Sun's JVM v1.5 you can do this:
The relevant classes in XStream are:
我能想到的唯一解决方案是使用字节码操作库,例如 javassist 添加默认构造函数。
The only solution I can think of would be to use a bytecode manipulation library such as javassist to add a default constructor.
如果您的类没有其他构造函数,那么编译器将为您创建一个。您可能有一个无参数构造函数但没有意识到它。
如果您不编写无参数构造函数,并且包含甚至一个带有参数的构造函数,那么编译器将不会给您一个。反射也无济于事:如果你试图找到一个无参数的构造函数,但没有,你期望发生什么?
听起来您不能使用 java.lang.Serialized 来使用 Java 对象序列化,但这不是您唯一的选择。您还可以使用 XML、JSON、原型缓冲区或任何其他方便的协议。
If your class has no other constructor, then the compiler will create one for you. You might have a no-arg constructor and not realize it.
If you do not write a no-arg constructor, and you include even one constructor that takes an argument, then the compiler will not give you one. Reflection won't help, either: if you try to find a no-arg constructor and there isn't one, what do you expect to happen?
It doesn't sound like you can use Java object serialization using java.lang.Serializable, but that's not your only choice. You can also use XML, or JSON, or prototype buffers, or any other protocol that's convenient.
如果它是实现 java.io.Serialized 接口或 java.io.Externalizable 接口的类,则可以使用 Java 反序列化对象时使用的相同机制:
If it's a class that implements the
java.io.Serializable
interface orjava.io.Externalizable
interface, it's possible by using the same mechanism that Java uses when deserializing objects: