# V6075. The signature of method 'X' does not conform to serialization requirements.

The analyzer has detected a user serialization method that does not meet the interface's requirements. If the user serialization fails to meet the requirements, the Serialization API ignores it.

If default serialization behavior is insufficient for the user class, you can change it by implementing the following methods:

    private void writeObject(java.io.ObjectOutputStream out)
       throws IOException
    private void readObject(java.io.ObjectInputStream in)
       throws IOException, ClassNotFoundException;
    private void readObjectNoData()
       throws ObjectStreamException;
    ANY-ACCESS-MODIFIER Object writeReplace()
       throws ObjectStreamException;
    ANY-ACCESS-MODIFIER Object readResolve()
       throws ObjectStreamException;

However, user implementations of these methods should strictly follow the requirements determined by their signatures; otherwise the default serialization will be preferred over the user serialization. This is explained in more detail here.

The problem is that 'java.io.Serializable' is an empty interface and is just a marker for the serialization mechanism. Therefore, when user-implemented logic is used, the compiler, for instance, cannot recognize incorrectly defined methods since they are just ordinary user methods.

Consider the following synthetic example, which you may well encounter in real-life software:

    class Base implements Serializable
    {
      ....
    }

    class Example extends Base
    {
      ....
      void writeObject(java.io.ObjectOutputStream out)
           throws IOException
      {
        throw new NotSerializableException("Serialization is not supported!");
      }

      void readObject(java.io.ObjectInputStream in)
           throws IOException, ClassNotFoundException
      {
        throw new NotSerializableException("Deserialization is not supported!");
      }
    }

Suppose we had a serializable base class. Then we needed to create a derived class but no longer wished it to be serializable. We wrote the necessary stub methods and went on writing the code. But now we discover that our derived class – despite our wish – is still serializable! This happens because our methods do not meet the interface's requirements. This defect can be fixed by changing the default modifier to private:

    class Base implements Serializable
    {
      ....
    }

    class Example extends Base
    {
      ....
      private void writeObject(java.io.ObjectOutputStream out)
           throws IOException
      {
        throw new NotSerializableException("Serialization is not supported!");
      }

      private void readObject(java.io.ObjectInputStream in)
           throws IOException, ClassNotFoundException
      {
        throw new NotSerializableException("Deserialization is not supported!");
      }
      ....
    }

This diagnostic is classified as:

CWE-684 CERT-SER01-J