org.sonar.l10n.java.rules.squid.S2118.html Maven / Gradle / Ivy
The newest version!
Nothing in a non-serializable class will be written out to file, and attempting to serialize such a class will result in an exception being thrown. Only a class that implements Serializable
or one that extends such a class can successfully be serialized (or de-serialized).
Noncompliant Code Example
public class Vegetable { // neither implements Serializable nor extends a class that does
//...
}
public class Menu {
public void meal() throws IOException {
Vegetable veg;
//...
FileOutputStream fout = new FileOutputStream(veg.getName());
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(veg); // Noncompliant. Nothing will be written
}
}
Compliant Solution
public class Vegetable implements Serializable { // can now be serialized
//...
}
public class Menu {
public void meal() throws IOException {
Vegetable veg;
//...
FileOutputStream fout = new FileOutputStream(veg.getName());
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(veg);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy