< prev index next >

src/java.base/share/classes/java/io/ObjectStreamField.java

Print this page
@@ -1,7 +1,7 @@
  /*
-  * Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
+  * Copyright (c) 1996, 2024, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Oracle designates this

@@ -55,10 +55,12 @@
      private final boolean unshared;
      /** corresponding reflective field object, if any */
      private final Field field;
      /** offset of field value in enclosing field group */
      private int offset;
+     /** index of the field in the class, retain the declaration order of serializable fields */
+     private final int argIndex;
  
      /**
       * Create a Serializable field with the specified type.  This field should
       * be documented with a {@code serialField} tag.
       *

@@ -85,32 +87,39 @@
       *          as writeObject/readObject; if true, write/read in the same
       *          manner as writeUnshared/readUnshared
       * @since   1.4
       */
      public ObjectStreamField(String name, Class<?> type, boolean unshared) {
+         this(name, type, unshared, -1);
+     }
+ 
+     /* package-private */
+     ObjectStreamField(String name, Class<?> type, boolean unshared, int argIndex) {
          if (name == null) {
              throw new NullPointerException();
          }
          this.name = name;
          this.type = type;
          this.unshared = unshared;
          this.field = null;
          this.signature = null;
+         this.argIndex = argIndex;
      }
  
      /**
       * Creates an ObjectStreamField representing a field with the given name,
       * signature and unshared setting.
       */
-     ObjectStreamField(String name, String signature, boolean unshared) {
+     ObjectStreamField(String name, String signature, boolean unshared, int argIndex) {
          if (name == null) {
              throw new NullPointerException();
          }
          this.name = name;
          this.signature = signature.intern();
          this.unshared = unshared;
          this.field = null;
+         this.argIndex = argIndex;
  
          type = switch (signature.charAt(0)) {
              case 'Z'      -> Boolean.TYPE;
              case 'B'      -> Byte.TYPE;
              case 'C'      -> Character.TYPE;

@@ -130,17 +139,18 @@
       * earlier serialization implementations, a "showType" parameter is
       * necessary to govern whether or not a getType() call on this
       * ObjectStreamField (if non-primitive) will return Object.class (as
       * opposed to a more specific reference type).
       */
-     ObjectStreamField(Field field, boolean unshared, boolean showType) {
+     ObjectStreamField(Field field, boolean unshared, boolean showType, int argIndex) {
          this.field = field;
          this.unshared = unshared;
          name = field.getName();
          Class<?> ftype = field.getType();
          type = (showType || ftype.isPrimitive()) ? ftype : Object.class;
          signature = ftype.descriptorString().intern();
+         this.argIndex = argIndex;
      }
  
      /**
       * Get the name of this field.
       *

@@ -225,10 +235,17 @@
      // REMIND: deprecate?
      protected void setOffset(int offset) {
          this.offset = offset;
      }
  
+     /**
+      * {@return Index of the field in the sequence of Serializable fields}
+      */
+     int getArgIndex() {
+         return argIndex;
+     }
+ 
      /**
       * Return true if this field has a primitive type.
       *
       * @return  true if and only if this field corresponds to a primitive type
       */
< prev index next >