Spread the love

Abstract interface ObjectInputValidation in Java

Spread the love

Abstract interface ObjectInputValidation in Java

A class implements the ObjectInputValidation interface and defines the validateObject() method in order to be able to validate itself when it and all the objects it depends on have been completely deserialized from an objectInputStream. The ObjectInputValidation interface defines a callback for object validation.

The validateObject() method is only invoked, however, if the object is passed to ObjectInputStream.registerValidation();this must be done from the private readObject() method of the object.

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.

Note If an object is deserialized as part of a larger object graph, it’s validateObject() method is not invoked until the entire graph is read, and the original call to ObjectInputStream.readObject() is about to return.

validateObject() should throw an InvalidobjectException if the object fails validation. This stops object serialization, and causes the original call to ObjectInputStream.readObject() to terminate with the InvalidobjectException exception.

The structure of the ObjectInputValidation interface is given as

public abstract interface java.io.ObjectInputValidation{
// Public Instance Methods () 
public abstract void validateObject() throws InvalidobjectException;
}

The details of the method is given below:

public abstract void validateObject() throws InvalidobjectException 

public abstract void validateObject() throws InvalidobjectException method allows an object to check its own validity. An InvalidObjectException should be thrown if anything is invalid.

READ  Abstract Interface Member in Java
READ  Error 1723 in Java? Here Are Steps How to Resolve.

A snippet to use this interface ObjectInputValidation or how to use ObjectInputValidation? or how to implement ObjectInputValidation interface?


public class validator 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!");
}
...
}




Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

Learn How to Use Jacoco Java code Coverage

Introduction to Jacoco Jacoco provides a wide range of coverage(analysis of instructions,...

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent Posts

Learn Quickly About DevOps Tools in a Fast and Efficient Manner

Introduction to DevOps tools DevOps tools are a collection of...

How to Collaborate between Development and Operations Team Fast and Effectively

Collaborate between Development and Operations Teams are the key...

Learn How to Use Jacoco Java code Coverage

Introduction to Jacoco Jacoco provides a wide range of coverage(analysis...

EDITOR PICKS


Spread the love