org.sonar.l10n.java.rules.squid.S2061.html Maven / Gradle / Ivy
Writers of Serializable
classes can choose to let Java's automatic mechanisms handle serialization and deserialization, or they can choose to handle it themselves by implementing specific methods. However, if the signatures of those methods are not exactly what is expected, they will be ignored and the default serialization mechanisms will kick back in.
Noncompliant Code Example
public class Watermelon implements Serializable {
// ...
void writeObject(java.io.ObjectOutputStream out)// Noncompliant; not private
throws IOException
{...}
private void readObject(java.io.ObjectInputStream in)
{...}
public void readObjectNoData() // Noncompliant; not private
{...}
static Object readResolve() throws ObjectStreamException // Noncompliant; this method may have any access modifier, may not be static
Watermelon writeReplace() throws ObjectStreamException // Noncompliant; this method may have any access modifier, but must return Object
{...}
}
Compliant Solution
public class Watermelon implements Serializable {
// ...
private void writeObject(java.io.ObjectOutputStream out)
throws IOException
{...}
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException
{...}
private void readObjectNoData()
throws ObjectStreamException
{...}
protected Object readResolve() throws ObjectStreamException
{...}
private Object writeReplace() throws ObjectStreamException
{...}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy