0

I'm trying to serialize a subclass of Path2D.Double, the serialization works, but the deserialization doesn't because of the following Exception:

Exception in thread "main" java.io.InvalidClassException: CustomShape; no valid constructor
at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:147)
at java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:755)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1751)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at CustomShape.main(CustomShape.java:38)

Could this be due to the superclass Path2D not implementing Serializable? But then, Path2D.Double itself couldn't be deserialized either.

Here's my Code:

import java.awt.geom.Path2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class CustomShape extends Path2D.Double {
    private static final long serialVersionUID = 1L;

    public CustomShape() {
        super();
    }

    public static void main(String[] args) throws Exception {
        //Make Shape
        //Path2D shape = new Path2D.Double(); //works
        Path2D shape = new CustomShape(); //doesn't work 
        shape.moveTo(0, 0);
        shape.lineTo(0, 10);
        shape.lineTo(10, 10);
        shape.lineTo(10, 0);
        shape.closePath();

        File file = new File(System.getProperty("user.dir"), "shape");
        //Save Shape
        if (!file.exists()) {
            file.createNewFile();
        }
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
        out.writeObject(shape);
        out.flush();
        out.close();

        //Load Shape
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
        Object obj = in.readObject();
        in.close();

        System.out.println(obj);
    }
}
NCode
  • 2,758
  • 1
  • 16
  • 25
  • Show the declaration of Path2D and its inner class Double. – Marko Topolnik May 06 '12 at 19:43
  • @Marko Topolnik they are jre classes. `java.awt.geom.Path2D` has `private` `readObject` and `writeObject` implementations. It seems to handle the package-private `transient double[] doubleChords` – esej May 06 '12 at 19:48
  • Sorry, didn't see the imports. I'm looking at the code and fail to see the cause of problem. – Marko Topolnik May 06 '12 at 20:02
  • You don't need the file.exists()/file.createNewFile() code. new FileOutputStream() is sufficient. – user207421 May 06 '12 at 23:25

1 Answers1

4

I ran into this same problem last week, finally found the answer today. The Path2D object declares its constructor as package-private. Since java serialization requires the first non-serializable parent class has a no-arg constructor, and it's not accessible, any extension of Path2D that is not in Path2D's own package will not deserialize. Honestly I think this is a flub on Java's side, since all the other AWT abstract shapes using Float/Double subclasses declare their default constructors as protected.

If you're curious, I found the root cause by tracing through java.io.ObjectStreamClass.getSerializableConstructor(Class<?>). You'll find that it returns null when it finds a constructor that is not accessible.