ObjectInputValidationNameObjectInputValidationSynopsis
DescriptionThe ObjectInputValidation interface defines a callback for object validation. A class implements this interface if it needs to validate deserialized objects. A class that needs to perform object validation on deserialized instances should pass a validation object to ObjectInputStream.registerValidation() at the beginning of its private readObject() method. When an object of that class is deserialized, the validateObject() method in the validation object is called. If the method is satisfied with the state of the deserialized object, it returns quietly; otherwise it should throw an InvalidObjectException. The simplest case is to have a class do its own validation by implementing ObjectInputValidation itself and passing this to the registerValidation() method. For example, the following code fragment shows how to register for validation in readObject(). The validateObject() method always throws an exception:
public class ValidateMe implements Serializable, ObjectInputValidation { private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.registerValidation(this, 0); in.defaultReadObject(); } public void validateObject() throws InvalidObjectException { // if (this object is not valid) throw new InvalidObjectException("Object not valid!"); } ... } Interface Declaration
public abstract interface java.io.ObjectInputValidation { // Methods public abstract void validateObject(); } MethodsvalidateObjectpublic void validateObject() throws InvalidObjectException
See AlsoObjectInput, ObjectInputStream |
|