1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 import java.lang.annotation.*;
  29 
  30 /**
  31  * Indicates that a field or method is related to the {@linkplain
  32  * Serializable serialization mechanism}. This annotation type is
  33  * intended to allow compile-time checking of serialization-related
  34  * declarations, analogous to the checking enabled by the {@link
  35  * java.lang.Override} annotation type to validate method overriding.
  36  *
  37  * <p>Specifically, annotations of this type are intended to be
  38  * applied to serialization-related methods and fields in classes
  39  * declared to be {@code Serializable}. The five serialization-related
  40  * methods are:
  41  *
  42  * <ul>
  43  * <li>{@code private void writeObject(java.io.ObjectOutputStream stream) throws IOException}
  44  * <li>{@code private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException}
  45  * <li>{@code private void readObjectNoData() throws ObjectStreamException}
  46  * <li><i>ANY-ACCESS-MODIFIER</i> {@code Object writeReplace() throws ObjectStreamException}
  47  * <li><i>ANY-ACCESS-MODIFIER</i> {@code Object readResolve() throws ObjectStreamException} 
  48  * </ul>
  49  *
  50  * The two serialization-related fields are:
  51  *
  52  * <ul>
  53  * <li>{@code private static final ObjectStreamField[] serialPersistentFields}
  54  * <li>{@code private static final long serialVersionUID}
  55  * </ul>
  56  * 
  57  * A compiler can validate that a method or field marked with a 
  58  * <code>@Serial</code> annotation is one of the defined serialization-related
  59  * methods declared in a meaningful context.
  60  *
  61  * <p>It is a semantic error to apply this annotation to other fields or methods, including:
  62  * <ul>
  63  * <li>fields or methods in a class that is not {@code Serializable}
  64  *
  65  * <li>fields or methods of the proper structural declaration, but in
  66  * a type where they are ineffectual. For example, {@code enum} types
  67  * are defined to have a {@code serialVersionUID} of {@code 0L} so a
  68  * {@code serialVersionUID} field declared in an {@code enum} type is
  69  * ignored. The five serialization-related methods identified above
  70  * are likewise ignored for an {@code enum} type.
  71  *
  72  * </ul>
  73  */
  74 @Target({ElementType.METHOD, ElementType.FIELD})
  75 @Retention(RetentionPolicy.SOURCE) 
  76 public @interface Serial {
  77 }