src/java.base/share/classes/java/io/ObjectStreamField.java
author jboes
Tue, 24 Sep 2019 09:43:43 +0100
changeset 58288 48e480e56aad
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base Summary: Minor coding style update of javadoc tag in any file in java.base Reviewed-by: bchristi, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 47216
diff changeset
     2
 * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4171
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4171
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4171
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4171
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4171
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.Field;
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34357
diff changeset
    29
import jdk.internal.reflect.CallerSensitive;
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34357
diff changeset
    30
import jdk.internal.reflect.Reflection;
18237
54af2a0e06da 8008132: Better serialization support
smarks
parents: 5506
diff changeset
    31
import sun.reflect.misc.ReflectUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A description of a Serializable field from a Serializable class.  An array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * of ObjectStreamFields is used to declare the Serializable fields of a class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author      Mike Warres
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author      Roger Riggs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see ObjectStreamClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class ObjectStreamField
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    implements Comparable<Object>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /** field name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private final String name;
45892
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
    48
    /** canonical JVM signature of field type, if given */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final String signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /** field type (Object.class if unknown non-primitive type) */
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
    51
    private final Class<?> type;
45892
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
    52
    /** lazily constructed signature for the type, if no explicit signature */
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
    53
    private String typeSignature;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /** whether or not to (de)serialize field values as unshared */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private final boolean unshared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /** corresponding reflective field object, if any */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final Field field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /** offset of field value in enclosing field group */
45892
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
    59
    private int offset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Create a Serializable field with the specified type.  This field should
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 47216
diff changeset
    63
     * be documented with a {@code serialField} tag.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param   name the name of the serializable field
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 47216
diff changeset
    66
     * @param   type the {@code Class} object of the serializable field
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public ObjectStreamField(String name, Class<?> type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this(name, type, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Creates an ObjectStreamField representing a serializable field with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * given name and type.  If unshared is false, values of the represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * field are serialized and deserialized in the default manner--if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * field is non-primitive, object values are serialized and deserialized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * if they had been written and read by calls to writeObject and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * readObject.  If unshared is true, values of the represented field are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * serialized and deserialized as if they had been written and read by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * calls to writeUnshared and readUnshared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param   name field name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param   type field type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param   unshared if false, write/read field values in the same manner
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *          as writeObject/readObject; if true, write/read in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *          manner as writeUnshared/readUnshared
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public ObjectStreamField(String name, Class<?> type, boolean unshared) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.unshared = unshared;
45892
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
    96
        this.field = null;
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
    97
        this.signature = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Creates an ObjectStreamField representing a field with the given name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * signature and unshared setting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    ObjectStreamField(String name, String signature, boolean unshared) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this.signature = signature.intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.unshared = unshared;
45892
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
   111
        this.field = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        switch (signature.charAt(0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            case 'Z': type = Boolean.TYPE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            case 'B': type = Byte.TYPE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            case 'C': type = Character.TYPE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            case 'S': type = Short.TYPE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            case 'I': type = Integer.TYPE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            case 'J': type = Long.TYPE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            case 'F': type = Float.TYPE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            case 'D': type = Double.TYPE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            case 'L':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            case '[': type = Object.class; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            default: throw new IllegalArgumentException("illegal signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
34357
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   129
     * Returns JVM type signature for given primitive.
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   130
     */
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   131
    private static String getPrimitiveSignature(Class<?> cl) {
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   132
        if (cl == Integer.TYPE)
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   133
            return "I";
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   134
        else if (cl == Byte.TYPE)
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   135
            return "B";
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   136
        else if (cl == Long.TYPE)
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   137
            return "J";
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   138
        else if (cl == Float.TYPE)
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   139
            return "F";
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   140
        else if (cl == Double.TYPE)
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   141
            return "D";
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   142
        else if (cl == Short.TYPE)
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   143
            return "S";
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   144
        else if (cl == Character.TYPE)
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   145
            return "C";
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   146
        else if (cl == Boolean.TYPE)
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   147
            return "Z";
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   148
        else if (cl == Void.TYPE)
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   149
            return "V";
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   150
        else
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   151
            throw new InternalError();
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   152
    }
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   153
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   154
    /**
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   155
     * Returns JVM type signature for given class.
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   156
     */
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   157
    static String getClassSignature(Class<?> cl) {
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   158
        if (cl.isPrimitive()) {
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   159
            return getPrimitiveSignature(cl);
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   160
        } else {
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   161
            return appendClassSignature(new StringBuilder(), cl).toString();
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   162
        }
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   163
    }
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   164
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   165
    static StringBuilder appendClassSignature(StringBuilder sbuf, Class<?> cl) {
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   166
        while (cl.isArray()) {
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   167
            sbuf.append('[');
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   168
            cl = cl.getComponentType();
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   169
        }
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   170
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   171
        if (cl.isPrimitive()) {
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   172
            sbuf.append(getPrimitiveSignature(cl));
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   173
        } else {
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   174
            sbuf.append('L').append(cl.getName().replace('.', '/')).append(';');
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   175
        }
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   176
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   177
        return sbuf;
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   178
    }
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   179
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   180
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Creates an ObjectStreamField representing the given field with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * specified unshared setting.  For compatibility with the behavior of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * earlier serialization implementations, a "showType" parameter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * necessary to govern whether or not a getType() call on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * ObjectStreamField (if non-primitive) will return Object.class (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * opposed to a more specific reference type).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    ObjectStreamField(Field field, boolean unshared, boolean showType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        this.field = field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        this.unshared = unshared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        name = field.getName();
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   192
        Class<?> ftype = field.getType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        type = (showType || ftype.isPrimitive()) ? ftype : Object.class;
34357
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 25859
diff changeset
   194
        signature = getClassSignature(ftype).intern();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Get the name of this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 47216
diff changeset
   200
     * @return  a {@code String} representing the name of the serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *          field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Get the type of the field.  If the type is non-primitive and this
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 47216
diff changeset
   209
     * {@code ObjectStreamField} was obtained from a deserialized {@link
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 47216
diff changeset
   210
     * ObjectStreamClass} instance, then {@code Object.class} is returned.
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 47216
diff changeset
   211
     * Otherwise, the {@code Class} object for the type of the field is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 47216
diff changeset
   214
     * @return  a {@code Class} object representing the type of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *          serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
18238
d1ed51b53296 8012917: ObjectStreamClass and ObjectStreamField should be CallerSensitive aware
chegar
parents: 18237
diff changeset
   217
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public Class<?> getType() {
18260
7ef05ae0bd19 8012243: about 30% regression on specjvm2008.serial on 7u25 comparing 7u21
dfuchs
parents: 18238
diff changeset
   219
        if (System.getSecurityManager() != null) {
7ef05ae0bd19 8012243: about 30% regression on specjvm2008.serial on 7u25 comparing 7u21
dfuchs
parents: 18238
diff changeset
   220
            Class<?> caller = Reflection.getCallerClass();
7ef05ae0bd19 8012243: about 30% regression on specjvm2008.serial on 7u25 comparing 7u21
dfuchs
parents: 18238
diff changeset
   221
            if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(), type.getClassLoader())) {
7ef05ae0bd19 8012243: about 30% regression on specjvm2008.serial on 7u25 comparing 7u21
dfuchs
parents: 18238
diff changeset
   222
                ReflectUtil.checkPackageAccess(type);
7ef05ae0bd19 8012243: about 30% regression on specjvm2008.serial on 7u25 comparing 7u21
dfuchs
parents: 18238
diff changeset
   223
            }
18237
54af2a0e06da 8008132: Better serialization support
smarks
parents: 5506
diff changeset
   224
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Returns character encoding of field type.  The encoding is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * B            byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * C            char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * D            double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * F            float
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * I            int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * J            long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * L            class or interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * S            short
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Z            boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * [            array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @return  the typecode of the serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    // REMIND: deprecate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public char getTypeCode() {
45892
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
   247
        return getSignature().charAt(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Return the JVM type signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @return  null if this field has a primitive type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    // REMIND: deprecate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public String getTypeString() {
45892
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
   257
        return isPrimitive() ? null : getSignature();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Offset of field within instance data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @return  the offset of this field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @see #setOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    // REMIND: deprecate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public int getOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Offset within instance data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param   offset the offset of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @see #getOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    // REMIND: deprecate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    protected void setOffset(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Return true if this field has a primitive type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @return  true if and only if this field corresponds to a primitive type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    // REMIND: deprecate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public boolean isPrimitive() {
45892
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
   289
        char tcode = getTypeCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return ((tcode != 'L') && (tcode != '['));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Returns boolean value indicating whether or not the serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * represented by this ObjectStreamField instance is unshared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
19029
30c64a024c86 8020426: Fix doclint accessibility issues in java.io
juh
parents: 18260
diff changeset
   297
     * @return {@code true} if this field is unshared
30c64a024c86 8020426: Fix doclint accessibility issues in java.io
juh
parents: 18260
diff changeset
   298
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public boolean isUnshared() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return unshared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 47216
diff changeset
   306
     * Compare this field with another {@code ObjectStreamField}.  Return
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * -1 if this is smaller, 0 if equal, 1 if greater.  Types that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * primitives are "smaller" than object types.  If equal, the field names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * are compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    // REMIND: deprecate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public int compareTo(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        ObjectStreamField other = (ObjectStreamField) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        boolean isPrim = isPrimitive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (isPrim != other.isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            return isPrim ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return name.compareTo(other.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Return a string that describes this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public String toString() {
45892
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
   325
        return getSignature() + ' ' + name;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Returns field represented by this ObjectStreamField, or null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * ObjectStreamField is not associated with an actual field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    Field getField() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Returns JVM type signature of field (similar to getTypeString, except
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * that signature strings are returned for primitive fields as well).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    String getSignature() {
45892
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
   341
        if (signature != null) {
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
   342
            return signature;
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
   343
        }
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
   344
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
   345
        String sig = typeSignature;
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
   346
        // This lazy calculation is safe since signature can be null iff one
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
   347
        // of the public constructors are used, in which case type is always
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
   348
        // initialized to the exact type we want the signature to represent.
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
   349
        if (sig == null) {
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
   350
            typeSignature = sig = getClassSignature(type).intern();
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
   351
        }
0b9499e86ee6 8184603: Create ObjectStreamField signature lazily when possible
redestad
parents: 37363
diff changeset
   352
        return sig;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
}