[英]Serialization of an object “that has fields reverenced by a SWT class”
I'm kind of stuck with a problem. 我有点困扰。 I do understand the concept of serialization.
我确实了解序列化的概念。 Nevertheless I'm getting errors when I try to serialize/deserialize (deepCopy) an object:
但是,当我尝试对对象进行序列化/反序列化(deepCopy)时出现错误:
I have a basic domain objects that hold information (two files): 我有一个保存信息的基本域对象(两个文件):
public class DomainObject implements java.io.Serializable {
private String defaultDescription = "";
private List<Translation> translations;
public DomainObject() {
;
}
public void setTranslations(final List<Translation> newTranslations) {
this.translations = newTranslations;
}
public List<Translation> getTranslations() {
return this.translations;
}
public void setDefaultDescription(final String newDescription) {
this.defaultDescription = newDescription;
}
public String getDefaultDescription() {
return this.defaultDescription;
}
}
public class Translations implements java.io.Serializable {
private String description = "";
public Translation() {
;
}
public void setDescription(final String newDescription) {
this.description = newDescription;
}
public String getDescription() {
return this.description;
}
}
I also have a frame so the user can fill in all the necessary information for this domain object. 我也有一个框架,以便用户可以填写该域对象的所有必要信息。 Since I have multiple domain objects (this example only shows one) with different fields I have different frames for each domain object.
由于我有多个具有不同字段的域对象(此示例仅显示一个),因此每个域对象都有不同的框架。 Each of these frames includes a "MultiLanguageFrame" which gives the user the ability to add optional translations for this domain object's description.
这些框架中的每一个都包含一个“ MultiLanguageFrame”,使用户能够为该域对象的描述添加可选的翻译。
public class MultiLanguageFrame extends org.eclipse.swt.widgets.Composite {
private List<Translation> translations = new ArrayList<Translation>();
public MultiLanguageFrame(final Composite parent, final int style) {
super(parent, style);
...
}
public List<Translation> getTranslations() {
return translations;
}
}
I deepCopy objects via this method: 我通过这种方法来复制对象:
...
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
oos.flush();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception t) {
logger.error(deepCopy() error: " + t.getMessage()); //$NON-NLS-1$
throw new RuntimeException("deepCopy() error", t); //$NON-NLS-1$
}
So now to the error: When i try to do something like this: 因此,现在出现错误:当我尝试执行以下操作时:
MultiLanguageFrame frame = new MultiLanguageFrame(parent, SWT.NONE);
DomainObject dom = new DomainObject();
dom.setDefaultDescription("Testobject");
dom.setTranslations(frame.getTranslations())
deepCopy(dom);
I receive an error telling me that MultiLanguageFrame is not Serializable. 我收到一条错误消息,告诉我MultiLanguageFrame不可序列化。 Why would Java try to serialize the frame when I only want that DomainObject?
当我只想要DomainObject时,为什么Java会尝试序列化框架?
I thought maybe it is because of the reference in frame. 我认为可能是因为参考了框架。 So when I add the Serializable-Interface to MultiLanguageFrame and markt the SWT-Components as transient it tells me that no valid constructor was found.
因此,当我将Serializable-Interface添加到MultiLanguageFrame并将SWT-Components标记为瞬态时,它告诉我找不到有效的构造函数。 I can't add a parameterless constructor because it would logically make no sense and also SWT-Components need a parent to exist.
我无法添加无参数构造函数,因为从逻辑上讲它毫无意义,而且SWT-Components还需要一个父对象才能存在。
I'm really stuck with this problem because I do not know how to work around this. 我真的很困扰这个问题,因为我不知道如何解决这个问题。 Thanks for answers in advance!
预先感谢您的答复!
I found the solution myself. 我自己找到了解决方案。 I'll just post this so others can see it, it might help.
我将其发布,以便其他人可以看到它,这可能会有所帮助。
Thanks to @greg-449 who lead the way. 感谢@ greg-449带领我们前进。 I do have an inner class
TranslationHelper
which extends Translation
in MultiLanguageFrame
. 我确实有一个内部类
TranslationHelper
,它在MultiLanguageFrame
中扩展了Translation
。 The purpose of this is so I can save some flags (deleted, changed, new) for Translations without changing Translation
itself. 这样做的目的是使我可以为翻译保存一些标志(删除,更改,新),而无需更改
Translation
本身。 When I call frame.getTranslations()
I cast the elements from TranslationsHelper
to Translation
. 当我调用
frame.getTranslations()
我将元素从TranslationsHelper
为Translation
。 The instance of the object remains a TranslationHelper
though. 该对象的实例仍然是
TranslationHelper
。
Now it all makes sense that MultiLanguageFrame was involved in all of this. 现在,所有这些都涉及到了MultiLanguageFrame。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.