简体   繁体   English

是否有可能在java中使用反射创建没有arg构造函数的类的“空白”实例?

[英]Is it possible in java to create 'blank' instance of class without no-arg constructor using reflection?

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. 尝试序列化这样的结构可能不是一个好主意,但另一种方法是将其转换为更容易序列化的东西。

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). 注意:在这些情况下可能不会调用构造函数(但这可能是您想要的)。

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 http://www.javaspecialists.eu/archive/Issue175.html中所述

Your solution will be JVM specific. 您的解决方案将特定于JVM。

If you need a portable solution use a 3rd party library . 如果您需要便携式解决方案,请使用第三方库

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.converters.reflection.PureJavaReflectionProvider
  • com.thoughtworks.xstream.core.JVM; com.thoughtworks.xstream.core.JVM;

我能想到的唯一解决方案是使用字节码操作库(如javassist)来添加默认构造函数。

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. 你可能有一个no-arg构造函数而没有意识到它。

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. 如果你不编写一个no-arg构造函数,并且你甚至包含一个带参数的构造函数,那么编译器就不会给你一个。 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? 反思也无济于事:如果你试图找到一个没有arg的构造函数但没有一个,你期望发生什么?

It doesn't sound like you can use Java object serialization using java.lang.Serializable, but that's not your only choice. 听起来你不能使用java.lang.Serializable来使用Java对象序列化,但这不是你唯一的选择。 You can also use XML, or JSON, or prototype buffers, or any other protocol that's convenient. 您还可以使用XML,JSON或原型缓冲区,或任何其他方便的协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java类如何没有no-arg构造函数? - How can a Java class have no no-arg constructor? Java - 使用Reflection创建新实例,而不需要知道构造函数参数 - Java - Create new Instance using Reflection without knowing constructor params Java反射创建未知类和构造函数的实例 - Java reflection create instance of unknown class and constructor 是否可以使用Java Reflection创建嵌套类的实例? - Is it possible to create an instance of nested class using Java Reflection? Hibernate和no-arg构造函数 - Hibernate and no-arg constructor 如何在没有公共no-arg构造函数的情况下获取pojo类的属性名称? - How to get property names of a pojo class without public no-arg constructor? Java中无参数构造函数和默认构造函数的区别 - Difference between a no-arg constructor and a default constructor in Java java.lang.RuntimeException:无法实例化@Form类。 没有no-arg构造函数 - java.lang.RuntimeException: Unable to instantiate @Form class. No no-arg constructor 你能编写一个需要Java中的无参数构造函数或工厂的接口吗? - Can you write an interface that requires a no-arg constructor or factory in Java? 如何使用零参数构造函数创建具有通过Java反射绑定的上下文的Scala类的新实例? - How to create new instance of Scala class with context bound via Java reflection using only zero argument constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM