java - SerializationProxy 模式给出 ClassCastException : How to avoid?

标签 java serialization classcastexception circular-reference

我有以下两个类,它们使用Effective Java书中的序列化代理模式。我想我由于循环依赖而遇到了麻烦,我该如何解决它?

public class Symbol implements Serializable {
    private static final long serialVersionUID = 23829245030202L;

    private final String symbol;
    private final float confidence;
    private final boolean dropcap;
    private final boolean subscript;
    private final boolean superscript;
    private final Rectangle boundingBox;
    private final Rectangle baseline;
    private final List<SymbolChoice> symbolChoices;

    private Word parentWord;

    private Symbol(final String symbol, final float confidence, final boolean dropcap, final boolean subscript, final boolean superscript, final Rectangle boundingBox, final Rectangle baseline, final List<SymbolChoice> symbolChoices) {
        this.symbol = Objects.requireNonNull(symbol, "symbol");
        this.confidence = confidence;
        this.dropcap = dropcap;
        this.subscript = subscript;
        this.superscript = superscript;
        this.boundingBox = Objects.requireNonNull(boundingBox, "boundingBox");
        this.baseline = Objects.requireNonNull(baseline, "baseline");
        this.symbolChoices = Objects.requireNonNull(symbolChoices, "symbolChoices");
    }

    private void setParentWord(final Word parentWord) {
        this.parentWord = Objects.requireNonNull(parentWord, "parentWord");
    }

    public String getSymbol() {
        return symbol;
    }

    public float getConfidence() {
        return confidence;
    }

    public boolean isDropcap() {
        return dropcap;
    }

    public boolean isSubscript() {
        return subscript;
    }

    public boolean isSuperscript() {
        return superscript;
    }

    public Rectangle getBoundingBox() {
        return boundingBox;
    }

    public Rectangle getBaseline() {
        return baseline;
    }

    public List<SymbolChoice> getSymbolChoices() {
        return symbolChoices;
    }

    public Word getParentWord() {
        return parentWord;
    }

    public static class SymbolBuilder {
        private final String symbol;
        private final float confidence;
        private final boolean dropcap;
        private final boolean subscript;
        private final boolean superscript;
        private final Rectangle boundingBox;
        private final Rectangle baseline;
        private final List<SymbolChoice> symbolChoices = new ArrayList<SymbolChoice>();

        public SymbolBuilder(final String symbol, final float confidence, final boolean dropcap, final boolean subscript, final boolean superscript, final Rectangle boundingBox, final Rectangle baseline) {
            this.symbol = symbol;
            this.confidence = confidence;
            this.dropcap = dropcap;
            this.subscript = subscript;
            this.superscript = superscript;
            this.boundingBox = boundingBox;
            this.baseline = baseline;
        }

        public SymbolBuilder addSymbolChoice(final SymbolChoice symbolChoice) {
            symbolChoices.add(Objects.requireNonNull(symbolChoice, "symbolChoice"));
            return this;
        }

        public Symbol build() {
            return new Symbol(symbol, confidence, dropcap, subscript, superscript, boundingBox, baseline, symbolChoices);
        }
    }

    private Object writeReplace() {
        return new SerializationProxy(this);
    }

    private void readObject(final ObjectInputStream stream) throws InvalidObjectException {
        throw new InvalidObjectException("Proxy required");
    }

    private static class SerializationProxy implements Serializable {
        private static final long serialVersionUID = 49545459839839843L;

        private final String symbol;
        private final float confidence;
        private final boolean dropcap;
        private final boolean subscript;
        private final boolean superscript;
        private final Rectangle boundingBox;
        private final Rectangle baseline;
        private final List<SymbolChoice> symbolChoices;

        private final Word parentWord;

        private SerializationProxy(final Symbol symbol) {
            this.symbol = symbol.symbol;
            this.confidence = symbol.confidence;
            this.dropcap = symbol.dropcap;
            this.subscript = symbol.subscript;
            this.superscript = symbol.superscript;
            this.symbolChoices = symbol.symbolChoices;
            this.boundingBox = symbol.boundingBox;
            this.baseline = symbol.baseline;
            this.parentWord = symbol.parentWord;
        }

        private Object readResolve() {
            Symbol localSymbol = new Symbol(symbol, confidence, dropcap, subscript, superscript, boundingBox, baseline, symbolChoices);
            localSymbol.setParentWord(parentWord);
            return localSymbol;
        }
    }
}

public class Word implements Serializable {
    private static final long serialVersionUID = 9084633893292833L;

    private final String word;
    private final float confidence;
    private final FontAttributes fontAttributes;
    private final boolean fromDictionary;
    private final boolean numeric;
    private final Rectangle boundingBox;
    private final Rectangle baseline;
    private final List<Symbol> symbols;

    private Textline parentTextline;

    private Word(final String word, final float confidence, final FontAttributes fontAttributes, final boolean fromDictionary, final boolean numeric, final Rectangle boundingBox, final Rectangle baseline, final List<Symbol> symbols) {
        this.word = Objects.requireNonNull(word, "word");
        this.confidence = confidence;
        this.fontAttributes = Objects.requireNonNull(fontAttributes, "fontAttributes");
        this.fromDictionary = fromDictionary;
        this.numeric = numeric;
        this.boundingBox = Objects.requireNonNull(boundingBox, "boundingBox");
        this.baseline = Objects.requireNonNull(baseline, "baseline");
        this.symbols = Objects.requireNonNull(symbols, "symbols");
    }

    private void setParentTextline(final Textline parentTextline) {
        this.parentTextline = Objects.requireNonNull(parentTextline, "parentTextline");
    }

    public String getWord() {
        return word;
    }

    public float getConfidence() {
        return confidence;
    }

    public FontAttributes getFontAttributes() {
        return fontAttributes;
    }

    public boolean isFromDictionary() {
        return fromDictionary;
    }

    public boolean isNumeric() {
        return numeric;
    }

    public Rectangle getBoundingBox() {
        return boundingBox;
    }

    public Rectangle getBaseline() {
        return baseline;
    }

    public List<Symbol> getSymbols() {
        return symbols;
    }

    public Textline getParentTextline() {
        return parentTextline;
    }

    public static class WordBuilder {
        private final String word;
        private final float confidence;
        private final FontAttributes fontAttributes;
        private final boolean fromDictionary;
        private final boolean numeric;
        private final Rectangle boundingBox;
        private final Rectangle baseline;
        private final List<Symbol> symbols = new ArrayList<Symbol>();

        public WordBuilder(final String word, final float confidence, final FontAttributes fontAttributes, final boolean fromDictionary, final boolean numeric, final Rectangle boundingBox, final Rectangle baseline) {
            this.word = word;
            this.confidence = confidence;
            this.fontAttributes = fontAttributes;
            this.fromDictionary = fromDictionary;
            this.numeric = numeric;
            this.boundingBox = boundingBox;
            this.baseline = baseline;
        }

        public WordBuilder addSymbol(final Symbol symbol) {
            symbols.add(Objects.requireNonNull(symbol, "symbol"));
            return this;
        }

        public Word build() {
            return new Word(word, confidence, fontAttributes, fromDictionary, numeric, boundingBox, baseline, symbols);
        }
    }

    private Object writeReplace() {
        return new SerializationProxy(this);
    }

    private void readObject(final ObjectInputStream stream) throws InvalidObjectException {
        throw new InvalidObjectException("Proxy required");
    }

    private static class SerializationProxy implements Serializable {
        private static final long serialVersionUID = 794943938877393932L;

        private final String word;
        private final float confidence;
        private final FontAttributes fontAttributes;
        private final boolean fromDictionary;
        private final boolean numeric;
        private final Rectangle boundingBox;
        private final Rectangle baseline;
        private final List<Symbol> symbols;

        private final Textline parentTextline;

        private SerializationProxy(final Word word) {
            this.word = word.word;
            this.confidence = word.confidence;
            this.fontAttributes = word.fontAttributes;
            this.fromDictionary = word.fromDictionary;
            this.numeric = word.numeric;
            this.boundingBox = word.boundingBox;
            this.baseline = word.baseline;
            this.symbols = word.symbols;
            this.parentTextline = word.parentTextline;
        }

        private Object readResolve() {
            Word localWord = new Word(word, confidence, fontAttributes, fromDictionary, numeric, boundingBox, baseline, symbols);
            localWord.setParentTextline(parentTextline);
            return localWord;
        }
    }
}

给出异常(exception):

cannot assign instance of com.skiwi.tessutils4j.data.Word$SerializationProxy 
to field com.skiwi.tessutils4j.data.Symbol$SerializationProxy.parentWord 
of type com.skiwi.tessutils4j.data.Word in instance 
of com.skiwi.tessutils4j.data.Symbol$SerializationProxy

请注意,这两个类并不是唯一具有此类依赖性的类。

在这里评论我的设计:我有一个层次结构:

Block -> Paragraph -> Textline -> Word -> Symbol -> SymbolChoice

所有元素都需要有一个子元素列表,并且所有元素(SymbolChoice 除外)都需要知道它们的父元素。

如何避免这种异常(可能需要更改设计)?

最佳答案

这里的解决方案是认识到父级信息不值得在子级中序列化。

在您的层次结构中:

Block -> Paragraph -> Textline -> Word -> Symbol -> SymbolChoice

如果您要序列化这些类中的任何一个,则该类应该序列化自身及其子类,但不是其父类。反序列化后可以设置父级。

换句话说,例如,Symbol 的 SerializationProxy 中不应该有 parentWordthis.parentWord = symbol.parentWord;。相反,在 Word 的 SerializationProxy 中,代码应如下所示:

    private Object readResolve() {
        Word localWord = new Word(word, confidence, fontAttributes, fromDictionary, numeric, boundingBox, baseline, symbols);
        for (Symbol s : symbols) {
            s.setParentWord(localWord);
        }
        return localWord;
    }

这会更改引用的顺序以匹配反序列化顺序,并删除循环。

关于java - SerializationProxy 模式给出 ClassCastException : How to avoid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24038723/

相关文章:

java - 封装:java中为什么叫keySet

java - Spring数据-实体未更新

java - 无法恢复存储在 MySQL 数据库中的 URL

java - JMS性能: BytesMessage vs ObjectMessage

jquery - 如何序列化发布到 django 后端的数组

java - ServiceRegistry 构造函数在具体类上失败并出现 ClassCastException

Java Web服务 java.lang.ClassCastException : org. apache.cxf.jaxws.ServiceImpl

java - 如何将元素放入数组列表的特定位置

java - 序列化是否保留对象身份?

java - 奇怪的 classCastException hibernate 3.5 glassfish