对象的序列化 "that has fields reverenced by a SWT class"
Serialization of an object "that has fields reverenced by a SWT class"
我遇到了一个问题。我确实理解序列化的概念。然而,当我尝试 serialize/deserialize (deepCopy) 对象时出现错误:
我有一个保存信息的基本域对象(两个文件):
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;
}
}
我还有一个框架,因此用户可以填写此域对象的所有必要信息。由于我有多个具有不同字段的域对象(此示例仅显示一个),因此我为每个域对象设置了不同的框架。这些框架中的每一个都包含一个 "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;
}
}
我通过这种方法深度复制对象:
...
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$
}
所以现在错误:
当我尝试做这样的事情时:
MultiLanguageFrame frame = new MultiLanguageFrame(parent, SWT.NONE);
DomainObject dom = new DomainObject();
dom.setDefaultDescription("Testobject");
dom.setTranslations(frame.getTranslations())
deepCopy(dom);
我收到一条错误消息,告诉我 MultiLanguageFrame 不可序列化。当我只想要那个 DomainObject 时,为什么 Java 会尝试序列化框架?
我想可能是因为框架中的引用。因此,当我将 Serializable-Interface 添加到 MultiLanguageFrame 并将 SWT-Components 标记为 transient 时,它告诉我没有找到有效的构造函数。我不能添加一个无参数的构造函数,因为它在逻辑上没有意义,而且 SWT-Components 需要一个父存在。
我真的被这个问题困住了,因为我不知道如何解决这个问题。提前感谢您的回答!
我自己找到了解决方案。我只是 post 这样其他人就可以看到它,这可能会有所帮助。
感谢带路的@greg-449。我确实有一个内部 class TranslationHelper
,它在 MultiLanguageFrame
中扩展了 Translation
。这样做的目的是让我可以在不更改 Translation
本身的情况下为翻译保存一些标志(删除、更改、新)。当我调用 frame.getTranslations()
时,我将元素从 TranslationsHelper
转换为 Translation
。尽管对象的实例仍然是 TranslationHelper
。
现在 MultiLanguageFrame 参与了所有这一切是有道理的。
我遇到了一个问题。我确实理解序列化的概念。然而,当我尝试 serialize/deserialize (deepCopy) 对象时出现错误:
我有一个保存信息的基本域对象(两个文件):
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;
}
}
我还有一个框架,因此用户可以填写此域对象的所有必要信息。由于我有多个具有不同字段的域对象(此示例仅显示一个),因此我为每个域对象设置了不同的框架。这些框架中的每一个都包含一个 "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;
}
}
我通过这种方法深度复制对象:
...
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$
}
所以现在错误: 当我尝试做这样的事情时:
MultiLanguageFrame frame = new MultiLanguageFrame(parent, SWT.NONE);
DomainObject dom = new DomainObject();
dom.setDefaultDescription("Testobject");
dom.setTranslations(frame.getTranslations())
deepCopy(dom);
我收到一条错误消息,告诉我 MultiLanguageFrame 不可序列化。当我只想要那个 DomainObject 时,为什么 Java 会尝试序列化框架?
我想可能是因为框架中的引用。因此,当我将 Serializable-Interface 添加到 MultiLanguageFrame 并将 SWT-Components 标记为 transient 时,它告诉我没有找到有效的构造函数。我不能添加一个无参数的构造函数,因为它在逻辑上没有意义,而且 SWT-Components 需要一个父存在。
我真的被这个问题困住了,因为我不知道如何解决这个问题。提前感谢您的回答!
我自己找到了解决方案。我只是 post 这样其他人就可以看到它,这可能会有所帮助。
感谢带路的@greg-449。我确实有一个内部 class TranslationHelper
,它在 MultiLanguageFrame
中扩展了 Translation
。这样做的目的是让我可以在不更改 Translation
本身的情况下为翻译保存一些标志(删除、更改、新)。当我调用 frame.getTranslations()
时,我将元素从 TranslationsHelper
转换为 Translation
。尽管对象的实例仍然是 TranslationHelper
。
现在 MultiLanguageFrame 参与了所有这一切是有道理的。