Core Java

Java’s @Serial Annotation

The JDK may be getting another standard (predefined) annotation with JDK 11: @Serial. JDK-8202385 [“Annotation to mark serial-related fields and methods”] aims to add “some kind of ‘SerialRelated’ annotation to facilitate automated checking of the declarations of serial fields and methods.” The idea is to better indicate to a developer when a serialization-related field or method is misspelled similar to the way that “the java.lang.Override annotation type is used to signal the compiler should verify the method is in fact overridden.”

Joe Darcy recently requested review of the “webrev” (proposed code addition). This provides a peek at what the new @Serial might look like. The current proposal is for this annotation definition to reside in the java.io package, to be targeted at specific methods or fields, and to have SOURCE retention.

The Javadoc comments for the proposed definition of @Serial currently provide significant detail on how to use this annotation. This Javadoc also explicitly specifies which methods and fields are anticipated to be annotated with @Serial: writeObject(), readObject(), readObjectNoData(), writeReplace(), readResolve(), ObjectStreamField[], and serialVersionUID.

The proposed @Serial annotation will be checked when the javacserial” lint check is executed. This is described in Darcy’s e-mail request for review:

The proposed java.io.Serial annotation type is intended to be used along with an augmented implementation of javac’s “serial” lint check; that work will be done separately as part of JDK-8202056: “Expand serial warning to check for bad overloads of serial-related methods”.

It’s interesting to note that the name of this annotation is not necessarily finalized, though it seems likely to stick. Darcy’s e-mail message points out that alternate names such as @Serialize and @SerialRelated could also be used.

An interesting distinction is that the @Serial annotation cannot or should not be used with certain methods and certain fields of the Externalizable interface (extends Serializable) because those methods and fields are not used in Externalizable. More details on this distinction are available in the core-libs-dev messages 053060, 053061, 053064, and 053067.

The @Serial annotation is not officially scheduled for JDK 11 as of this writing, but it appears likely that it could be available in time for the JDK 11 release given the recent progress of JDK-8202385. Besides the potential usefulness of this annotation to those implementing custom serialization, this annotation’s definition will provide another example of how any custom annotation can be documented to allow it to be used correctly.

Published on Java Code Geeks with permission by Dustin Marx, partner at our JCG program. See the original article here: Java’s @Serial Annotation

Opinions expressed by Java Code Geeks contributors are their own.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button