Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Tuesday, January 7, 2020

Java 14 @Serial Annotation

1. Introduction


In this tutorial, we will learn a new annotation @Serial introduced in Java 14. This annotation is similar to the @Override annotation, @Serial annotation is used in combination with the serial lint flag to perform compile-time checks for the serialization-related members of a class.


Already this feature is available in build 25.

Java 14 @Serial Annotation



2. Usage and Example


Let us create an example program on @Serial annotation. This should be used as part of the fields and methods in Serialization.

public class ProductSerial implements Serializable {
 
    @Serial
    private static final ObjectStreamField[] serialPersistentFields = null;
     
    @Serial
    private static final long serialVersionUID = 1;
     
    @Serial
    private void writeObject(ObjectOutputStream stream) throws IOException {
        // ...
    }
     
    @Serial
    private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
        // ...
    }
 
    @Serial
    private void readObjectNoData() throws ObjectStreamException {
        // ...
    }
 
    @Serial
    private Object writeReplace() throws ObjectStreamException {
        // ...
        return null;
    }
 
    @Serial
    private Object readResolve() throws ObjectStreamException {
        // ...
        return null;
    }
 
}

Once, creating a new class then we need to compile using a serial lint flag.

javac -Xlint:serial ProductSerial.java

3. Ineffective and compile-time error places


This is legal to use in serialization. Let us see what is its behavior in normal classes and enum.

The lint compiler will check for the annotated member signatures and types. If do not match then it will throw compile-time errors.

3.1 If any class not implementing Serializable interface


public class NotSerializeClass {

    @Serial 
    private static final long serialVersionUID = 1; // Compilation error
 
}

Reason: class NotSerializeClass is not implemented Serializable interface.

3.2 In Enums


public enum MyEnum { 
    @Serial
    private void readObjectNoData() throws ObjectStreamException {} // Compilation error 
}

This is also an ineffective way to use in Enums

3.3 Using Externalizable Interface


Apart from the Serializable interface, java api provided another way using Externalizable interface.



public class MyExternalizableClass implements Externalizable {
    @Serial
    private void writeObject(ObjectOutputStream stream) throws IOException {} // Compilation error 
}

To writeObject(), readObject(), readObjectNoData() and serial Persistent Fields in an Externalizable since those classes use different serialization methods.


4. Conclusion


In this article, we have seen how to use new @Serial annotation and good guidelines. What are the illegal places to not use this annotation?

Note: Just remember, this annotation should be applied only in Serializable context.



No comments:

Post a Comment

Please do not add any spam links in the comments section.