Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将派生类复制/克隆到基类_Java_Serialization_Clone - Fatal编程技术网

Java 将派生类复制/克隆到基类

Java 将派生类复制/克隆到基类,java,serialization,clone,Java,Serialization,Clone,如何将派生类克隆(复制基类部分)到基类 在我的例子中,基类是一个JPA实体,派生类有一些swing/ui的东西。 我认为通过gson/json序列化进行克隆应该可以工作,但有不同的问题 Base d=new Derived(); Base b=(Base) SerializationUtils.clone(d); System.out.println(b.getClass().getSimpleName()); //-->Derived //hibernateSession.sa

如何将派生类克隆(复制基类部分)到基类

在我的例子中,基类是一个JPA实体,派生类有一些swing/ui的东西。 我认为通过gson/json序列化进行克隆应该可以工作,但有不同的问题

Base d=new Derived();
Base b=(Base) SerializationUtils.clone(d);
System.out.println(b.getClass().getSimpleName());   //-->Derived
   //hibernateSession.save(b) -> refers to derived class

除了手动将所有属性从派生复制到基之外,还有什么简单的方法吗?

确保继承树的所有级别都支持Java Beans API。现在您可以编写如下特定级别的克隆器:

public <T> T specialClone( T obj, Class<T> type ) {
    T result = type.newInstance();
    Class<?> current = type;
    while( current != null ) {
        BeanInfo info = Introspector.getBeanInfo( current );
        for( PropertyDescriptor pd: info.getPropertyDescriptors() ) {
            Object value = pd.getReadMethod().invoke( obj );
            pd.getWriteMethod().invoke( result, value );
        }
        current = current.getSuperClass();
    }
    return result;
}
for( Helper h : get( current ) ) {
    h.copy( obj, result );
}

public Helper[] get( Class<?> type ) {
    ... look in cache. If nothing there, create helper using  PropertyDescriptors.
}
public T specialClone(T对象,类类型){
T result=type.newInstance();
类别电流=类型;
while(当前!=null){
BeanInfo info=Introspector.getBeanInfo(当前);
对于(PropertyDescriptor pd:info.getPropertyDescriptors()){
对象值=pd.getReadMethod().invoke(obj);
pd.getWriteMethod().invoke(结果,值);
}
current=current.getSuperClass();
}
返回结果;
}
请注意,您可能希望缓存读/写方法,因为方法调用是同步的

当我做这样的事情时,我通常会检查bean一次,并创建包装这两个方法的助手对象,这样我就可以像这样工作:

public <T> T specialClone( T obj, Class<T> type ) {
    T result = type.newInstance();
    Class<?> current = type;
    while( current != null ) {
        BeanInfo info = Introspector.getBeanInfo( current );
        for( PropertyDescriptor pd: info.getPropertyDescriptors() ) {
            Object value = pd.getReadMethod().invoke( obj );
            pd.getWriteMethod().invoke( result, value );
        }
        current = current.getSuperClass();
    }
    return result;
}
for( Helper h : get( current ) ) {
    h.copy( obj, result );
}

public Helper[] get( Class<?> type ) {
    ... look in cache. If nothing there, create helper using  PropertyDescriptors.
}
for(Helper h:get(当前)){
h、 复印件(obj,结果);
}
公共助手[]获取(类类型){
…在缓存中查找。如果没有任何内容,请使用PropertyDescriptors创建帮助程序。
}

再多的升级也帮不了你,这纯粹是编译器的东西。另外,这通常是一个定义不清的问题,这意味着您可能无法找到现成的解决方案。手动操作会不会太痛苦?既然您的
Dervided
Base
类,为什么需要这样做?你想要达到什么样的效果,而强制转换或深度复制是做不到的?