java 是否可以在java中使用反射创建没有无参数构造函数的类的“空白”实例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4133709/
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
Is it possible in java to create 'blank' instance of class without no-arg constructor using reflection?
提问by Vladimir
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.
我有一个没有默认构造函数的类。我需要一种方法来获取此类的“空白”实例。'blank' 意味着在实例化后所有类字段都应该具有默认值,如 null、0 等。
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.
我问是因为我需要能够序列化/反序列化对象的大树。而且我无法访问此对象类的源,并且类既没有默认构造函数也没有实现可序列化。尝试序列化此类结构可能不是一个好主意,但替代方法是将其转换为更易于序列化的内容。
采纳答案by Brad Cupit
With standard reflection, no, but there is a library that can do it for you: objenesis.
使用标准反射,不,但有一个库可以为您完成:objenesis。
It's specifically designed to instantiate classes without default constructors, and it's used by other serialization libraries like xstream.
它专门设计用于在没有默认构造函数的情况下实例化类,并且它被其他序列化库(如xstream )使用。
Note: the constructor might not be called in these cases (but that's presumably what you want).
注意:在这些情况下可能不会调用构造函数(但这可能是您想要的)。
回答by mcveat
Having Class instance provided as variable clazz:
将 Class 实例作为变量 clazz 提供:
ReflectionFactory rf = ReflectionFactory.getReflectionFactory();
Constructor objDef = parent.getDeclaredConstructor();
Constructor intConstr = rf.newConstructorForSerialization(clazz, objDef);
clazz.cast(intConstr.newInstance());
as described in http://www.javaspecialists.eu/archive/Issue175.html
回答by johnstok
Your solution will be JVM specific.
您的解决方案将特定于 JVM。
If you need a portable solution use a 3rd party library.
如果您需要便携式解决方案,请使用 3rd 方库。
For Sun's JVM v1.5 you can do this:
对于 Sun 的 JVM v1.5,您可以这样做:
final Class<?> myClass = MyClass.class;
final ReflectionFactory reflection = ReflectionFactory.getReflectionFactory();
final Constructor<Object> constructor =
reflection.newConstructorForSerialization(
myClass, Object.class.getDeclaredConstructor(new Class[0]));
final Object o = constructor.newInstance(new Object[0]);
System.out.print(o.getClass());
The relevant classes in XStream are:
XStream 中的相关类有:
- com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider
- com.thoughtworks.xstream.core.JVM;
- com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider
- com.thoughtworks.xstream.core.JVM;
回答by Maurice Perry
The only solution I can think of would be to use a bytecode manipulation library such as javassistto add a default constructor.
我能想到的唯一解决方案是使用字节码操作库(例如javassist)来添加默认构造函数。
回答by duffymo
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 oneconstructor 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.lang.Serializable 使用 Java 对象序列化,但这不是您唯一的选择。您还可以使用 XML、JSON、原型缓冲区或任何其他方便的协议。