2014-10-02 54 views
1

我使用的序列化代理模式爲一堆我的課,最近的FindBugs加入到我的構建過程,現在我想知道的FindBugs是否正確......使用序列化代理模式,writeObject()不是必需的嗎?

這是有問題的類:

public class Block implements Serializable { 
    private static final long serialVersionUID = 584958030434385L; 

    private final float confidence; 
    private final Rectangle boundingBox; 
    private final Rectangle baseline; 
    private final BufferedImage binaryImage; 
    private final List<Paragraph> paragraphs; 

    private TessResult parentTessResult; 

    private Block(final float confidence, final Rectangle boundingBox, final Rectangle baseline, final BufferedImage binaryImage, final List<Paragraph> paragraphs) { 
     this.confidence = confidence; 
     this.boundingBox = Objects.requireNonNull(boundingBox, "boundingBox"); 
     this.baseline = Objects.requireNonNull(baseline, "baseline"); 
     this.binaryImage = binaryImage; 
     this.paragraphs = Objects.requireNonNull(paragraphs, "paragraphs"); 
    } 

    void setParentTessResult(final TessResult parentTessResult) { 
     this.parentTessResult = Objects.requireNonNull(parentTessResult, "parentTessResult"); 
    } 

    public float getConfidence() { 
     return confidence; 
    } 

    public Rectangle getBoundingBox() { 
     return boundingBox; 
    } 

    public Rectangle getBaseline() { 
     return baseline; 
    } 

    public BufferedImage getBinaryImage() { 
     return binaryImage; 
    } 

    public List<Paragraph> getParagraphs() { 
     return paragraphs; 
    } 

    public TessResult getParentTessResult() { 
     return parentTessResult; 
    } 

    public static class BlockBuilder { 
     private final float confidence; 
     private final Rectangle boundingBox; 
     private final Rectangle baseline; 
     private final BufferedImage binaryImage; 
     private final List<Paragraph> paragraphs = new ArrayList<>(); 

     public BlockBuilder(final float confidence, final Rectangle boundingBox, final Rectangle baseline, final BufferedImage binaryImage) { 
      this.confidence = confidence; 
      this.boundingBox = boundingBox; 
      this.baseline = baseline; 
      this.binaryImage = binaryImage; 
     } 

     public BlockBuilder addParagraph(final Paragraph paragraph) { 
      paragraphs.add(Objects.requireNonNull(paragraph, "paragraph")); 
      return this; 
     } 

     public Block build() { 
      return new Block(confidence, boundingBox, baseline, binaryImage, paragraphs); 
     } 
    } 

    private Object writeReplace() throws IOException { 
     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 = 12321313232553L; 

     private final float confidence; 
     private final Rectangle boundingBox; 
     private final Rectangle baseline; 
     private final byte[] binaryImageBytes; 
     private final List<Paragraph> paragraphs; 

     private SerializationProxy(final Block block) throws IOException { 
      this.confidence = block.confidence; 
      this.boundingBox = block.boundingBox; 
      this.baseline = block.baseline; 
      this.binaryImageBytes = bufferedImageToBytes(block.binaryImage); 
      this.paragraphs = block.paragraphs; 
     } 

     private Object readResolve() throws IOException { 
      BufferedImage binaryImage = bytesToBufferedImage(binaryImageBytes); 
      Block block = new Block(confidence, boundingBox, baseline, binaryImage, paragraphs); 
      for (Paragraph paragraph : paragraphs) { 
       paragraph.setParentBlock(block); 
      } 
      return block; 
     } 
    } 
} 

要注意的關鍵是它存儲BufferedImage,但在內部它存儲BufferedImage作爲序列化的byte[]

現在我得到的FindBugs這樣的警告:

類com.yob.dpc2.ocr.utils.data.Block定義的非暫時性非序列化的實例字段binaryImage [「com.yob.dpc2。 ocr.utils.data.Block「]在Block.java:[lines 18-95]

所以,我一頭扎進問題一點,我看到,我不提供private void writeObject(ObjectOutputStream oos) throws IOException ...

所以我的問題是:我應該提供writeObject()方法嗎?如果是這樣,有沒有辦法可以重用writeReplace()方法或做其他事情來防止邏輯重複?

回答

2

我應該提供writeObject()方法嗎?

不,因爲它不能被稱爲writeReplace()方法。

您在FindBugs中遇到了限制。讓你的班級成爲一個特例。

相關問題