# V6083. Serialization order of fields should be preserved during deserialization.

This diagnostic rule detects mismatching orders of serialization and deserialization of an object's fields.

When using the 'java.io.Serializable' interface, the JVM is in total control over serialization. Convenient as this approach may be, it is often not flexible or fast enough.

An alternative approach to serialization provided by the JVM is to use the 'java.io.Externalizable' interface with the methods 'writeExternal' and 'readExternal' overridden. The downside of this technique, however, is a high risk of breaking the order of writing and reading the fields, which could result in an elusive bug.

Consider the following example:

    public class ExternalizableTest implements Externalizable
    {
      public String name;
      public String host;
      public int port;
      ....
      @Override
      public void writeExternal(ObjectOutput out) throws IOException
      {
        out.writeInt(port);          // <=
        out.writeUTF(name);
        out.writeUTF(host);
      }

      @Override
      public void readExternal(ObjectInput in) throws IOException
      {
        this.name = in.readUTF();    // <=
        this.host = in.readUTF();
        this.port = in.readInt();
      }
    }

In this code, the object's fields are serialized in the following order: port, name, host, type. But they are deserialized in the order: name, host, port, type. The first field to be serialized is an integer, and the first field to be deserialized is a string. This mismatch leads to a 'java.io.EOFException'. You could call it "luck" because this bug will show up at the very first attempt to deserialize the object.

But what if we are not that "lucky" – like in this example:

    public class ExternalizableTest implements Externalizable
    {
      public String name;
      public String host;
      public int port;
      ....
      @Override
      public void writeExternal(ObjectOutput out) throws IOException
      {
        out.writeInt(port);
        out.writeUTF(name);          // <=
        out.writeUTF(host);
      }

      @Override
      public void readExternal(ObjectInput in) throws IOException
      {
        this.port = in.readInt();
        this.host = in.readUTF();    // <=
        this.name = in.readUTF();
      }
    }

The deserialization order is again different from the serialization order: the string fields 'name' and 'host' are swapped. In this case, the program will keep running without crashing, with the object successfully restored, but the fields will have their values swapped. A defect like that is not as easily detected.

Fixed version:

    public class ExternalizableTest implements Externalizable
    {
      public String name;
      public String host;
      public int port;
      ....
      @Override
      public void writeExternal(ObjectOutput out) throws IOException
      {
        out.writeInt(port);
        out.writeUTF(name);
        out.writeUTF(host);
      }

      @Override
      public void readExternal(ObjectInput in) throws IOException
      {
        this.port = in.readInt();
        this.name = in.readUTF();
        this.host = in.readUTF();
      }
    }