src/java.base/share/classes/java/io/ObjectStreamClass.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58288 48e480e56aad
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53127
b561ea19a7b9 6996807: FieldReflectorKey hash code computation can be improved
igerasim
parents: 52427
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.ref.Reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.ref.ReferenceQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.Field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.InvocationTargetException;
47400
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
    35
import java.lang.reflect.UndeclaredThrowableException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.reflect.Member;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.lang.reflect.Proxy;
47400
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
    40
import java.security.AccessControlContext;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.NoSuchAlgorithmException;
47400
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
    44
import java.security.PermissionCollection;
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
    45
import java.security.Permissions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.PrivilegedAction;
47400
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
    47
import java.security.ProtectionDomain;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.util.concurrent.ConcurrentHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.concurrent.ConcurrentMap;
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 33302
diff changeset
    56
import jdk.internal.misc.Unsafe;
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34357
diff changeset
    57
import jdk.internal.reflect.CallerSensitive;
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34357
diff changeset
    58
import jdk.internal.reflect.Reflection;
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34357
diff changeset
    59
import jdk.internal.reflect.ReflectionFactory;
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 52220
diff changeset
    60
import jdk.internal.access.SharedSecrets;
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 52220
diff changeset
    61
import jdk.internal.access.JavaSecurityAccess;
18237
54af2a0e06da 8008132: Better serialization support
smarks
parents: 18186
diff changeset
    62
import sun.reflect.misc.ReflectUtil;
34357
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 33674
diff changeset
    63
import static java.io.ObjectStreamField.*;
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 33674
diff changeset
    64
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * Serialization's descriptor for classes.  It contains the name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * serialVersionUID of the class.  The ObjectStreamClass for a specific class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * loaded in this Java VM can be found/created using the lookup method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <p>The algorithm to compute the SerialVersionUID is described in
45138
ddcafe0d0ea3 8180082: Broken javadoc links
rriggs
parents: 41608
diff changeset
    71
 * <a href="{@docRoot}/../specs/serialization/class.html#stream-unique-identifiers">
ddcafe0d0ea3 8180082: Broken javadoc links
rriggs
parents: 41608
diff changeset
    72
 *     Object Serialization Specification, Section 4.6, Stream Unique Identifiers</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @author      Mike Warres
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @author      Roger Riggs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @see ObjectStreamField
45138
ddcafe0d0ea3 8180082: Broken javadoc links
rriggs
parents: 41608
diff changeset
    77
 * @see <a href="{@docRoot}/../specs/serialization/class.html">
ddcafe0d0ea3 8180082: Broken javadoc links
rriggs
parents: 41608
diff changeset
    78
 *     Object Serialization Specification, Section 4, Class Descriptors</a>
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 22936
diff changeset
    79
 * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
public class ObjectStreamClass implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /** serialPersistentFields value indicating no serializable fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static final ObjectStreamField[] NO_FIELDS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        new ObjectStreamField[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 53127
diff changeset
    87
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static final long serialVersionUID = -6120832682080437368L;
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 53127
diff changeset
    89
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static final ObjectStreamField[] serialPersistentFields =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        NO_FIELDS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /** reflection factory for obtaining serialization constructors */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    94
    private static final ReflectionFactory reflFactory =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            new ReflectionFactory.GetReflectionFactoryAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static class Caches {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        /** cache mapping local classes -> descriptors */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        static final ConcurrentMap<WeakClassKey,Reference<?>> localDescs =
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7031
diff changeset
   101
            new ConcurrentHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        /** cache mapping field group/local desc pairs -> field reflectors */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        static final ConcurrentMap<FieldReflectorKey,Reference<?>> reflectors =
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7031
diff changeset
   105
            new ConcurrentHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        /** queue for WeakReferences to local classes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        private static final ReferenceQueue<Class<?>> localDescsQueue =
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7031
diff changeset
   109
            new ReferenceQueue<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        /** queue for WeakReferences to field reflectors keys */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        private static final ReferenceQueue<Class<?>> reflectorsQueue =
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7031
diff changeset
   112
            new ReferenceQueue<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /** class associated with this descriptor (if any) */
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
   116
    private Class<?> cl;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /** name of class represented by this descriptor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /** serialVersionUID of represented class (null if not computed yet) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private volatile Long suid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /** true if represents dynamic proxy class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private boolean isProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /** true if represents enum type */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private boolean isEnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /** true if represented class implements Serializable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private boolean serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /** true if represented class implements Externalizable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private boolean externalizable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /** true if desc has data written by class-defined writeObject method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private boolean hasWriteObjectData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * true if desc has externalizable data written in block data format; this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * must be true by default to accommodate ObjectInputStream subclasses which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * override readClassDescriptor() to return class descriptors obtained from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * ObjectStreamClass.lookup() (see 4461737)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private boolean hasBlockExternalData = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
11894
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   140
    /**
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   141
     * Contains information about InvalidClassException instances to be thrown
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   142
     * when attempting operations on an invalid class. Note that instances of
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   143
     * this class are immutable and are potentially shared among
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   144
     * ObjectStreamClass instances.
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   145
     */
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   146
    private static class ExceptionInfo {
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   147
        private final String className;
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   148
        private final String message;
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   149
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   150
        ExceptionInfo(String cn, String msg) {
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   151
            className = cn;
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   152
            message = msg;
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   153
        }
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   154
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   155
        /**
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   156
         * Returns (does not throw) an InvalidClassException instance created
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   157
         * from the information in this object, suitable for being thrown by
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   158
         * the caller.
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   159
         */
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   160
        InvalidClassException newInvalidClassException() {
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   161
            return new InvalidClassException(className, message);
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   162
        }
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   163
    }
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   164
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /** exception (if any) thrown while attempting to resolve class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private ClassNotFoundException resolveEx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /** exception (if any) to throw if non-enum deserialization attempted */
11894
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   168
    private ExceptionInfo deserializeEx;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /** exception (if any) to throw if non-enum serialization attempted */
11894
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   170
    private ExceptionInfo serializeEx;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /** exception (if any) to throw if default serialization attempted */
11894
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   172
    private ExceptionInfo defaultSerializeEx;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /** serializable fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private ObjectStreamField[] fields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /** aggregate marshalled size of primitive fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    private int primDataSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /** number of non-primitive fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private int numObjFields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /** reflector for setting/getting serializable field values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private FieldReflector fieldRefl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /** data layout of serialized objects described by this class desc */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private volatile ClassDataSlot[] dataLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /** serialization-appropriate constructor, or null if none */
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10419
diff changeset
   186
    private Constructor<?> cons;
47400
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   187
    /** protection domains that need to be checked when calling the constructor */
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   188
    private ProtectionDomain[] domains;
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   189
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /** class-defined writeObject method, or null if none */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private Method writeObjectMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /** class-defined readObject method, or null if none */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private Method readObjectMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /** class-defined readObjectNoData method, or null if none */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private Method readObjectNoDataMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /** class-defined writeReplace method, or null if none */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private Method writeReplaceMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /** class-defined readResolve method, or null if none */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private Method readResolveMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /** local class descriptor for represented class (may point to self) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private ObjectStreamClass localDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /** superclass descriptor appearing in stream */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private ObjectStreamClass superDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   206
    /** true if, and only if, the object has been correctly initialized */
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   207
    private boolean initialized;
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   208
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Initializes native code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private static native void initNative();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        initNative();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Find the descriptor for a class that can be serialized.  Creates an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * ObjectStreamClass instance if one does not exist yet for class. Null is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * returned if the specified class does not implement java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * or java.io.Externalizable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param   cl class for which to get the descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @return  the class descriptor for the specified class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public static ObjectStreamClass lookup(Class<?> cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return lookup(cl, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Returns the descriptor for any class, regardless of whether it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * implements {@link Serializable}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param        cl class for which to get the descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @return       the class descriptor for the specified class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public static ObjectStreamClass lookupAny(Class<?> cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return lookup(cl, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Returns the name of the class described by this descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * This method returns the name of the class in the format that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * is used by the {@link Class#getName} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @return a string representing the name of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Return the serialVersionUID for this class.  The serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * defines a set of classes all with the same name that have evolved from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * common root class and agree to be serialized and deserialized using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * common format.  NonSerializable classes have a serialVersionUID of 0L.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @return  the SUID of the class described by this descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public long getSerialVersionUID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // REMIND: synchronize instead of relying on volatile?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (suid == null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   264
            suid = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   265
                new PrivilegedAction<Long>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   266
                    public Long run() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   267
                        return computeDefaultSUID(cl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    }
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
        return suid.longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Return the class in the local VM that this version is mapped to.  Null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * is returned if there is no corresponding local class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   279
     * @return  the {@code Class} instance that this descriptor represents
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
18238
d1ed51b53296 8012917: ObjectStreamClass and ObjectStreamField should be CallerSensitive aware
chegar
parents: 18237
diff changeset
   281
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public Class<?> forClass() {
18237
54af2a0e06da 8008132: Better serialization support
smarks
parents: 18186
diff changeset
   283
        if (cl == null) {
54af2a0e06da 8008132: Better serialization support
smarks
parents: 18186
diff changeset
   284
            return null;
54af2a0e06da 8008132: Better serialization support
smarks
parents: 18186
diff changeset
   285
        }
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   286
        requireInitialized();
18260
7ef05ae0bd19 8012243: about 30% regression on specjvm2008.serial on 7u25 comparing 7u21
dfuchs
parents: 18238
diff changeset
   287
        if (System.getSecurityManager() != null) {
7ef05ae0bd19 8012243: about 30% regression on specjvm2008.serial on 7u25 comparing 7u21
dfuchs
parents: 18238
diff changeset
   288
            Class<?> caller = Reflection.getCallerClass();
7ef05ae0bd19 8012243: about 30% regression on specjvm2008.serial on 7u25 comparing 7u21
dfuchs
parents: 18238
diff changeset
   289
            if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(), cl.getClassLoader())) {
7ef05ae0bd19 8012243: about 30% regression on specjvm2008.serial on 7u25 comparing 7u21
dfuchs
parents: 18238
diff changeset
   290
                ReflectUtil.checkPackageAccess(cl);
7ef05ae0bd19 8012243: about 30% regression on specjvm2008.serial on 7u25 comparing 7u21
dfuchs
parents: 18238
diff changeset
   291
            }
18237
54af2a0e06da 8008132: Better serialization support
smarks
parents: 18186
diff changeset
   292
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Return an array of the fields of this serializable class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @return  an array containing an element for each persistent field of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *          this class. Returns an array of length zero if there are no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *          fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public ObjectStreamField[] getFields() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return getFields(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Get the field of this class by name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param   name the name of the data field to look for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @return  The ObjectStreamField object of the named field or null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *          there is no such named field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public ObjectStreamField getField(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return getField(name, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Return a string describing this ObjectStreamClass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return name + ": static final long serialVersionUID = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            getSerialVersionUID() + "L;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Looks up and returns class descriptor for given class, or null if class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * is non-serializable and "all" is set to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @param   cl class to look up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param   all if true, return descriptors for all classes; if false, only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *          return descriptors for serializable classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
   335
    static ObjectStreamClass lookup(Class<?> cl, boolean all) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (!(all || Serializable.class.isAssignableFrom(cl))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        processQueue(Caches.localDescsQueue, Caches.localDescs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        WeakClassKey key = new WeakClassKey(cl, Caches.localDescsQueue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        Reference<?> ref = Caches.localDescs.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        Object entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (ref != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            entry = ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        EntryFuture future = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            EntryFuture newEntry = new EntryFuture();
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7031
diff changeset
   349
            Reference<?> newRef = new SoftReference<>(newEntry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                if (ref != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    Caches.localDescs.remove(key, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                ref = Caches.localDescs.putIfAbsent(key, newRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                if (ref != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    entry = ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            } while (ref != null && entry == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                future = newEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (entry instanceof ObjectStreamClass) {  // check common case first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            return (ObjectStreamClass) entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (entry instanceof EntryFuture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            future = (EntryFuture) entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (future.getOwner() == Thread.currentThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                 * Handle nested call situation described by 4803747: waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                 * for future value to be set by a lookup() call further up the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                 * stack will result in deadlock, so calculate and set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                 * future value here instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                entry = future.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                entry = new ObjectStreamClass(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            } catch (Throwable th) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                entry = th;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            if (future.set(entry)) {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   388
                Caches.localDescs.put(key, new SoftReference<>(entry));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                // nested lookup call already set future
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                entry = future.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (entry instanceof ObjectStreamClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            return (ObjectStreamClass) entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        } else if (entry instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            throw (RuntimeException) entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        } else if (entry instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            throw (Error) entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            throw new InternalError("unexpected entry: " + entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Placeholder used in class descriptor and field reflector lookup tables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * for an entry in the process of being initialized.  (Internal) callers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * which receive an EntryFuture belonging to another thread as the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * of a lookup should call the get() method of the EntryFuture; this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * return the actual entry once it is ready for use and has been set().  To
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * conserve objects, EntryFutures synchronize on themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    private static class EntryFuture {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        private static final Object unset = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        private final Thread owner = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        private Object entry = unset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
         * Attempts to set the value contained by this EntryFuture.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
         * EntryFuture's value has not been set already, then the value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
         * saved, any callers blocked in the get() method are notified, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
         * true is returned.  If the value has already been set, then no saving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
         * or notification occurs, and false is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        synchronized boolean set(Object entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            if (this.entry != unset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            this.entry = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
         * Returns the value contained by this EntryFuture, blocking if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
         * necessary until a value is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        synchronized Object get() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            boolean interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            while (entry == unset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    interrupted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            if (interrupted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   451
                    new PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   452
                        public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                            Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
         * Returns the thread that created this EntryFuture.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        Thread getOwner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            return owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * Creates local class descriptor representing given class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     */
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
   473
    private ObjectStreamClass(final Class<?> cl) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        this.cl = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        name = cl.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        isProxy = Proxy.isProxyClass(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        isEnum = Enum.class.isAssignableFrom(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        serializable = Serializable.class.isAssignableFrom(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        externalizable = Externalizable.class.isAssignableFrom(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
   481
        Class<?> superCl = cl.getSuperclass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        superDesc = (superCl != null) ? lookup(superCl, false) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        localDesc = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        if (serializable) {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   486
            AccessController.doPrivileged(new PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   487
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    if (isEnum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                        suid = Long.valueOf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                        fields = NO_FIELDS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    if (cl.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                        fields = NO_FIELDS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    suid = getDeclaredSUID(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                        fields = getSerialFields(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                        computeFieldOffsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    } catch (InvalidClassException e) {
11894
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   503
                        serializeEx = deserializeEx =
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   504
                            new ExceptionInfo(e.classname, e.getMessage());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                        fields = NO_FIELDS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    if (externalizable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                        cons = getExternalizableConstructor(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                        cons = getSerializableConstructor(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                        writeObjectMethod = getPrivateMethod(cl, "writeObject",
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
   513
                            new Class<?>[] { ObjectOutputStream.class },
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                            Void.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                        readObjectMethod = getPrivateMethod(cl, "readObject",
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
   516
                            new Class<?>[] { ObjectInputStream.class },
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                            Void.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                        readObjectNoDataMethod = getPrivateMethod(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                            cl, "readObjectNoData", null, Void.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                        hasWriteObjectData = (writeObjectMethod != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    }
47400
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   522
                    domains = getProtectionDomains(cons, cl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    writeReplaceMethod = getInheritableMethod(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        cl, "writeReplace", null, Object.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    readResolveMethod = getInheritableMethod(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                        cl, "readResolve", null, Object.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            suid = Long.valueOf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            fields = NO_FIELDS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            fieldRefl = getReflector(fields, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        } catch (InvalidClassException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            // field mismatches impossible when matching local fields vs. self
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
   539
            throw new InternalError(ex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        if (deserializeEx == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            if (isEnum) {
11894
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   544
                deserializeEx = new ExceptionInfo(name, "enum type");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            } else if (cons == null) {
11894
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   546
                deserializeEx = new ExceptionInfo(name, "no valid constructor");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        for (int i = 0; i < fields.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            if (fields[i].getField() == null) {
11894
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   551
                defaultSerializeEx = new ExceptionInfo(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    name, "unmatched serializable field(s) declared");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   555
        initialized = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * Creates blank class descriptor which should be initialized via a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * subsequent call to initProxy(), initNonProxy() or readNonProxy().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    ObjectStreamClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    /**
47400
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   566
     * Creates a PermissionDomain that grants no permission.
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   567
     */
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   568
    private ProtectionDomain noPermissionsDomain() {
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   569
        PermissionCollection perms = new Permissions();
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   570
        perms.setReadOnly();
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   571
        return new ProtectionDomain(null, perms);
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   572
    }
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   573
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   574
    /**
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   575
     * Aggregate the ProtectionDomains of all the classes that separate
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   576
     * a concrete class {@code cl} from its ancestor's class declaring
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   577
     * a constructor {@code cons}.
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   578
     *
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   579
     * If {@code cl} is defined by the boot loader, or the constructor
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   580
     * {@code cons} is declared by {@code cl}, or if there is no security
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   581
     * manager, then this method does nothing and {@code null} is returned.
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   582
     *
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   583
     * @param cons A constructor declared by {@code cl} or one of its
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   584
     *             ancestors.
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   585
     * @param cl A concrete class, which is either the class declaring
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   586
     *           the constructor {@code cons}, or a serializable subclass
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   587
     *           of that class.
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   588
     * @return An array of ProtectionDomain representing the set of
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   589
     *         ProtectionDomain that separate the concrete class {@code cl}
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   590
     *         from its ancestor's declaring {@code cons}, or {@code null}.
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   591
     */
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   592
    private ProtectionDomain[] getProtectionDomains(Constructor<?> cons,
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   593
                                                    Class<?> cl) {
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   594
        ProtectionDomain[] domains = null;
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   595
        if (cons != null && cl.getClassLoader() != null
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   596
                && System.getSecurityManager() != null) {
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   597
            Class<?> cls = cl;
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   598
            Class<?> fnscl = cons.getDeclaringClass();
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   599
            Set<ProtectionDomain> pds = null;
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   600
            while (cls != fnscl) {
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   601
                ProtectionDomain pd = cls.getProtectionDomain();
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   602
                if (pd != null) {
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   603
                    if (pds == null) pds = new HashSet<>();
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   604
                    pds.add(pd);
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   605
                }
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   606
                cls = cls.getSuperclass();
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   607
                if (cls == null) {
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   608
                    // that's not supposed to happen
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   609
                    // make a ProtectionDomain with no permission.
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   610
                    // should we throw instead?
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   611
                    if (pds == null) pds = new HashSet<>();
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   612
                    else pds.clear();
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   613
                    pds.add(noPermissionsDomain());
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   614
                    break;
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   615
                }
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   616
            }
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   617
            if (pds != null) {
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   618
                domains = pds.toArray(new ProtectionDomain[0]);
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   619
            }
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   620
        }
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   621
        return domains;
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   622
    }
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   623
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   624
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Initializes class descriptor representing a proxy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     */
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
   627
    void initProxy(Class<?> cl,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                   ClassNotFoundException resolveEx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                   ObjectStreamClass superDesc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        throws InvalidClassException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   632
        ObjectStreamClass osc = null;
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   633
        if (cl != null) {
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   634
            osc = lookup(cl, true);
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   635
            if (!osc.isProxy) {
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   636
                throw new InvalidClassException(
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   637
                    "cannot bind proxy descriptor to a non-proxy class");
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   638
            }
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   639
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        this.cl = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        this.resolveEx = resolveEx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        this.superDesc = superDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        isProxy = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        serializable = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        suid = Long.valueOf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        fields = NO_FIELDS;
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   647
        if (osc != null) {
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   648
            localDesc = osc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            name = localDesc.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            externalizable = localDesc.externalizable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            writeReplaceMethod = localDesc.writeReplaceMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            readResolveMethod = localDesc.readResolveMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            deserializeEx = localDesc.deserializeEx;
47400
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   654
            domains = localDesc.domains;
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   655
            cons = localDesc.cons;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        fieldRefl = getReflector(fields, localDesc);
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   658
        initialized = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * Initializes class descriptor representing a non-proxy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    void initNonProxy(ObjectStreamClass model,
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
   665
                      Class<?> cl,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                      ClassNotFoundException resolveEx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                      ObjectStreamClass superDesc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        throws InvalidClassException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   670
        long suid = Long.valueOf(model.getSerialVersionUID());
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   671
        ObjectStreamClass osc = null;
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   672
        if (cl != null) {
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   673
            osc = lookup(cl, true);
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   674
            if (osc.isProxy) {
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   675
                throw new InvalidClassException(
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   676
                        "cannot bind non-proxy descriptor to a proxy class");
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   677
            }
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   678
            if (model.isEnum != osc.isEnum) {
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   679
                throw new InvalidClassException(model.isEnum ?
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   680
                        "cannot bind enum descriptor to a non-enum class" :
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   681
                        "cannot bind non-enum descriptor to an enum class");
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   682
            }
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   683
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   684
            if (model.serializable == osc.serializable &&
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   685
                    !cl.isArray() &&
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   686
                    suid != osc.getSerialVersionUID()) {
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   687
                throw new InvalidClassException(osc.name,
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   688
                        "local class incompatible: " +
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   689
                                "stream classdesc serialVersionUID = " + suid +
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   690
                                ", local class serialVersionUID = " +
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   691
                                osc.getSerialVersionUID());
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   692
            }
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   693
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   694
            if (!classNamesEqual(model.name, osc.name)) {
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   695
                throw new InvalidClassException(osc.name,
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   696
                        "local class name incompatible with stream class " +
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   697
                                "name \"" + model.name + "\"");
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   698
            }
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   699
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   700
            if (!model.isEnum) {
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   701
                if ((model.serializable == osc.serializable) &&
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   702
                        (model.externalizable != osc.externalizable)) {
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   703
                    throw new InvalidClassException(osc.name,
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   704
                            "Serializable incompatible with Externalizable");
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   705
                }
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   706
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   707
                if ((model.serializable != osc.serializable) ||
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   708
                        (model.externalizable != osc.externalizable) ||
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   709
                        !(model.serializable || model.externalizable)) {
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   710
                    deserializeEx = new ExceptionInfo(
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   711
                            osc.name, "class invalid for deserialization");
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   712
                }
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   713
            }
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   714
        }
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   715
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        this.cl = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        this.resolveEx = resolveEx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        this.superDesc = superDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        name = model.name;
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   720
        this.suid = suid;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        isProxy = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        isEnum = model.isEnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        serializable = model.serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        externalizable = model.externalizable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        hasBlockExternalData = model.hasBlockExternalData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        hasWriteObjectData = model.hasWriteObjectData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        fields = model.fields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        primDataSize = model.primDataSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        numObjFields = model.numObjFields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   731
        if (osc != null) {
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   732
            localDesc = osc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            writeObjectMethod = localDesc.writeObjectMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            readObjectMethod = localDesc.readObjectMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            readObjectNoDataMethod = localDesc.readObjectNoDataMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            writeReplaceMethod = localDesc.writeReplaceMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            readResolveMethod = localDesc.readResolveMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            if (deserializeEx == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                deserializeEx = localDesc.deserializeEx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            }
47400
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
   741
            domains = localDesc.domains;
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   742
            cons = localDesc.cons;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   744
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        fieldRefl = getReflector(fields, localDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        // reassign to matched fields so as to reflect local unshared settings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        fields = fieldRefl.getFields();
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   748
        initialized = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * Reads non-proxy class descriptor information from given input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * The resulting class descriptor is not fully functional; it can only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * used as input to the ObjectInputStream.resolveClass() and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * ObjectStreamClass.initNonProxy() methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    void readNonProxy(ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        name = in.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        suid = Long.valueOf(in.readLong());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        isProxy = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        byte flags = in.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        hasWriteObjectData =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            ((flags & ObjectStreamConstants.SC_WRITE_METHOD) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        hasBlockExternalData =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            ((flags & ObjectStreamConstants.SC_BLOCK_DATA) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        externalizable =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            ((flags & ObjectStreamConstants.SC_EXTERNALIZABLE) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        boolean sflag =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            ((flags & ObjectStreamConstants.SC_SERIALIZABLE) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        if (externalizable && sflag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            throw new InvalidClassException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                name, "serializable and externalizable flags conflict");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        serializable = externalizable || sflag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        isEnum = ((flags & ObjectStreamConstants.SC_ENUM) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        if (isEnum && suid.longValue() != 0L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            throw new InvalidClassException(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                "enum descriptor has non-zero serialVersionUID: " + suid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        int numFields = in.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        if (isEnum && numFields != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            throw new InvalidClassException(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                "enum descriptor has non-zero field count: " + numFields);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        fields = (numFields > 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            new ObjectStreamField[numFields] : NO_FIELDS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        for (int i = 0; i < numFields; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            char tcode = (char) in.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            String fname = in.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            String signature = ((tcode == 'L') || (tcode == '[')) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                in.readTypeString() : new String(new char[] { tcode });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                fields[i] = new ObjectStreamField(fname, signature, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                throw (IOException) new InvalidClassException(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    "invalid descriptor for field " + fname).initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        computeFieldOffsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * Writes non-proxy class descriptor information to given output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    void writeNonProxy(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        out.writeUTF(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        out.writeLong(getSerialVersionUID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        byte flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        if (externalizable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            flags |= ObjectStreamConstants.SC_EXTERNALIZABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            int protocol = out.getProtocolVersion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            if (protocol != ObjectStreamConstants.PROTOCOL_VERSION_1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                flags |= ObjectStreamConstants.SC_BLOCK_DATA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        } else if (serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            flags |= ObjectStreamConstants.SC_SERIALIZABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        if (hasWriteObjectData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            flags |= ObjectStreamConstants.SC_WRITE_METHOD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        if (isEnum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            flags |= ObjectStreamConstants.SC_ENUM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        out.writeByte(flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        out.writeShort(fields.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        for (int i = 0; i < fields.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            ObjectStreamField f = fields[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            out.writeByte(f.getTypeCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            out.writeUTF(f.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            if (!f.isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                out.writeTypeString(f.getTypeString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * Returns ClassNotFoundException (if any) thrown while attempting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * resolve local class corresponding to this class descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    ClassNotFoundException getResolveException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        return resolveEx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    /**
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   851
     * Throws InternalError if not initialized.
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   852
     */
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   853
    private final void requireInitialized() {
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   854
        if (!initialized)
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   855
            throw new InternalError("Unexpected call when not initialized");
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   856
    }
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   857
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   858
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * Throws an InvalidClassException if object instances referencing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * class descriptor should not be allowed to deserialize.  This method does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * not apply to deserialization of enum constants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    void checkDeserialize() throws InvalidClassException {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   864
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        if (deserializeEx != null) {
11894
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   866
            throw deserializeEx.newInvalidClassException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * Throws an InvalidClassException if objects whose class is represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * this descriptor should not be allowed to serialize.  This method does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * not apply to serialization of enum constants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    void checkSerialize() throws InvalidClassException {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   876
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        if (serializeEx != null) {
11894
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   878
            throw serializeEx.newInvalidClassException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * Throws an InvalidClassException if objects whose class is represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * this descriptor should not be permitted to use default serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * (e.g., if the class declares serializable fields that do not correspond
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * to actual fields, and hence must use the GetField API).  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * does not apply to deserialization of enum constants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    void checkDefaultSerialize() throws InvalidClassException {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   890
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        if (defaultSerializeEx != null) {
11894
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 9035
diff changeset
   892
            throw defaultSerializeEx.newInvalidClassException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * Returns superclass descriptor.  Note that on the receiving side, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * superclass descriptor may be bound to a class that is not a superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * of the subclass descriptor's bound class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    ObjectStreamClass getSuperDesc() {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   902
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        return superDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * Returns the "local" class descriptor for the class associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * class descriptor (i.e., the result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * ObjectStreamClass.lookup(this.forClass())) or null if there is no class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * associated with this descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    ObjectStreamClass getLocalDesc() {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   913
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        return localDesc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * Returns arrays of ObjectStreamFields representing the serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * fields of the represented class.  If copy is true, a clone of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * descriptor's field array is returned, otherwise the array itself is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    ObjectStreamField[] getFields(boolean copy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        return copy ? fields.clone() : fields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * Looks up a serializable field of the represented class by name and type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * A specified type of null matches all types, Object.class matches all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * non-primitive types, and any other non-null type matches assignable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * types only.  Returns matching field, or null if no match found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   933
    ObjectStreamField getField(String name, Class<?> type) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        for (int i = 0; i < fields.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            ObjectStreamField f = fields[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            if (f.getName().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                if (type == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                    (type == Object.class && !f.isPrimitive()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                    return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   942
                Class<?> ftype = f.getType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                if (ftype != null && type.isAssignableFrom(ftype)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                    return f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * Returns true if class descriptor represents a dynamic proxy class, false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    boolean isProxy() {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   956
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        return isProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * Returns true if class descriptor represents an enum type, false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    boolean isEnum() {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   965
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        return isEnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * Returns true if represented class implements Externalizable, false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    boolean isExternalizable() {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   974
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        return externalizable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * Returns true if represented class implements Serializable, false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    boolean isSerializable() {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   983
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        return serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * Returns true if class descriptor represents externalizable class that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * has written its data in 1.2 (block data) format, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    boolean hasBlockExternalData() {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
   992
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        return hasBlockExternalData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * Returns true if class descriptor represents serializable (but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * externalizable) class which has written its data via a custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * writeObject() method, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    boolean hasWriteObjectData() {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
  1002
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        return hasWriteObjectData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * Returns true if represented class is serializable/externalizable and can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * be instantiated by the serialization runtime--i.e., if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * externalizable and defines a public no-arg constructor, or if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * non-externalizable and its first non-serializable superclass defines an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * accessible no-arg constructor.  Otherwise, returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    boolean isInstantiable() {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
  1014
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        return (cons != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * Returns true if represented class is serializable (but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * externalizable) and defines a conformant writeObject method.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    boolean hasWriteObjectMethod() {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
  1024
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        return (writeObjectMethod != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * Returns true if represented class is serializable (but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * externalizable) and defines a conformant readObject method.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    boolean hasReadObjectMethod() {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
  1034
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        return (readObjectMethod != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * Returns true if represented class is serializable (but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * externalizable) and defines a conformant readObjectNoData method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * Otherwise, returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    boolean hasReadObjectNoDataMethod() {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
  1044
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        return (readObjectNoDataMethod != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * Returns true if represented class is serializable or externalizable and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * defines a conformant writeReplace method.  Otherwise, returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    boolean hasWriteReplaceMethod() {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
  1053
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        return (writeReplaceMethod != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * Returns true if represented class is serializable or externalizable and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * defines a conformant readResolve method.  Otherwise, returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    boolean hasReadResolveMethod() {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
  1062
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        return (readResolveMethod != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * Creates a new instance of the represented class.  If the class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * externalizable, invokes its public no-arg constructor; otherwise, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * class is serializable, invokes the no-arg constructor of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * non-serializable superclass.  Throws UnsupportedOperationException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * this class descriptor is not associated with a class, if the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * class is non-serializable or if the appropriate no-arg constructor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * inaccessible/unavailable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    Object newInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        throws InstantiationException, InvocationTargetException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
               UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
  1079
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        if (cons != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            try {
47400
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1082
                if (domains == null || domains.length == 0) {
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1083
                    return cons.newInstance();
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1084
                } else {
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1085
                    JavaSecurityAccess jsa = SharedSecrets.getJavaSecurityAccess();
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1086
                    PrivilegedAction<?> pea = () -> {
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1087
                        try {
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1088
                            return cons.newInstance();
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1089
                        } catch (InstantiationException
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1090
                                 | InvocationTargetException
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1091
                                 | IllegalAccessException x) {
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1092
                            throw new UndeclaredThrowableException(x);
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1093
                        }
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1094
                    }; // Can't use PrivilegedExceptionAction with jsa
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1095
                    try {
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1096
                        return jsa.doIntersectionPrivilege(pea,
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1097
                                   AccessController.getContext(),
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1098
                                   new AccessControlContext(domains));
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1099
                    } catch (UndeclaredThrowableException x) {
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1100
                        Throwable cause = x.getCause();
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1101
                        if (cause instanceof InstantiationException)
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1102
                            throw (InstantiationException) cause;
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1103
                        if (cause instanceof InvocationTargetException)
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1104
                            throw (InvocationTargetException) cause;
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1105
                        if (cause instanceof IllegalAccessException)
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1106
                            throw (IllegalAccessException) cause;
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1107
                        // not supposed to happen
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1108
                        throw x;
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1109
                    }
f1721aa42d2c 8180024: Improve construction of objects during deserialization
dfuchs
parents: 47216
diff changeset
  1110
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            } catch (IllegalAccessException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                // should not occur, as access checks have been suppressed
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
  1113
                throw new InternalError(ex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * Invokes the writeObject method of the represented serializable class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * Throws UnsupportedOperationException if this class descriptor is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * associated with a class, or if the class is externalizable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * non-serializable or does not define writeObject.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    void invokeWriteObject(Object obj, ObjectOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        throws IOException, UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
  1129
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        if (writeObjectMethod != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                writeObjectMethod.invoke(obj, new Object[]{ out });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            } catch (InvocationTargetException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                Throwable th = ex.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                if (th instanceof IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                    throw (IOException) th;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                    throwMiscException(th);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            } catch (IllegalAccessException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                // should not occur, as access checks have been suppressed
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
  1142
                throw new InternalError(ex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * Invokes the readObject method of the represented serializable class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * Throws UnsupportedOperationException if this class descriptor is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * associated with a class, or if the class is externalizable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * non-serializable or does not define readObject.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    void invokeReadObject(Object obj, ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        throws ClassNotFoundException, IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
               UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
  1159
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        if (readObjectMethod != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                readObjectMethod.invoke(obj, new Object[]{ in });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            } catch (InvocationTargetException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                Throwable th = ex.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                if (th instanceof ClassNotFoundException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                    throw (ClassNotFoundException) th;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                } else if (th instanceof IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                    throw (IOException) th;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                    throwMiscException(th);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            } catch (IllegalAccessException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                // should not occur, as access checks have been suppressed
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
  1174
                throw new InternalError(ex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * Invokes the readObjectNoData method of the represented serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * class.  Throws UnsupportedOperationException if this class descriptor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * not associated with a class, or if the class is externalizable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * non-serializable or does not define readObjectNoData.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    void invokeReadObjectNoData(Object obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        throws IOException, UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
  1190
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        if (readObjectNoDataMethod != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                readObjectNoDataMethod.invoke(obj, (Object[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            } catch (InvocationTargetException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                Throwable th = ex.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                if (th instanceof ObjectStreamException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                    throw (ObjectStreamException) th;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                    throwMiscException(th);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            } catch (IllegalAccessException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                // should not occur, as access checks have been suppressed
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
  1203
                throw new InternalError(ex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * Invokes the writeReplace method of the represented serializable class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * returns the result.  Throws UnsupportedOperationException if this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * descriptor is not associated with a class, or if the class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * non-serializable or does not define writeReplace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    Object invokeWriteReplace(Object obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        throws IOException, UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
  1219
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        if (writeReplaceMethod != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                return writeReplaceMethod.invoke(obj, (Object[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            } catch (InvocationTargetException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                Throwable th = ex.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                if (th instanceof ObjectStreamException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                    throw (ObjectStreamException) th;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                    throwMiscException(th);
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
  1229
                    throw new InternalError(th);  // never reached
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            } catch (IllegalAccessException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                // should not occur, as access checks have been suppressed
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
  1233
                throw new InternalError(ex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * Invokes the readResolve method of the represented serializable class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * returns the result.  Throws UnsupportedOperationException if this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * descriptor is not associated with a class, or if the class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * non-serializable or does not define readResolve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    Object invokeReadResolve(Object obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        throws IOException, UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    {
33289
8d59b06d36c2 8103671: More objective stream classes
chegar
parents: 32649
diff changeset
  1249
        requireInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        if (readResolveMethod != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                return readResolveMethod.invoke(obj, (Object[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            } catch (InvocationTargetException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                Throwable th = ex.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                if (th instanceof ObjectStreamException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                    throw (ObjectStreamException) th;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                    throwMiscException(th);
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
  1259
                    throw new InternalError(th);  // never reached
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            } catch (IllegalAccessException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                // should not occur, as access checks have been suppressed
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
  1263
                throw new InternalError(ex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * Class representing the portion of an object's serialized form allotted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * to data described by a given class descriptor.  If "hasData" is false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * the object's serialized form does not contain data associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * class descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    static class ClassDataSlot {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        /** class descriptor "occupying" this slot */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        final ObjectStreamClass desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        /** true if serialized form includes data for this slot's descriptor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        final boolean hasData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        ClassDataSlot(ObjectStreamClass desc, boolean hasData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            this.desc = desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            this.hasData = hasData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * Returns array of ClassDataSlot instances representing the data layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * (including superclass data) for serialized objects described by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * class descriptor.  ClassDataSlots are ordered by inheritance with those
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * containing "higher" superclasses appearing first.  The final
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * ClassDataSlot contains a reference to this descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    ClassDataSlot[] getClassDataLayout() throws InvalidClassException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        // REMIND: synchronize instead of relying on volatile?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        if (dataLayout == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            dataLayout = getClassDataLayout0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        return dataLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    private ClassDataSlot[] getClassDataLayout0()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        throws InvalidClassException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7031
diff changeset
  1307
        ArrayList<ClassDataSlot> slots = new ArrayList<>();
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1308
        Class<?> start = cl, end = cl;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        // locate closest non-serializable superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        while (end != null && Serializable.class.isAssignableFrom(end)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            end = end.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
18186
482db5c3e9c0 8000638: Improve deserialization
dmocek
parents: 11902
diff changeset
  1315
        HashSet<String> oscNames = new HashSet<>(3);
482db5c3e9c0 8000638: Improve deserialization
dmocek
parents: 11902
diff changeset
  1316
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        for (ObjectStreamClass d = this; d != null; d = d.superDesc) {
18186
482db5c3e9c0 8000638: Improve deserialization
dmocek
parents: 11902
diff changeset
  1318
            if (oscNames.contains(d.name)) {
482db5c3e9c0 8000638: Improve deserialization
dmocek
parents: 11902
diff changeset
  1319
                throw new InvalidClassException("Circular reference.");
482db5c3e9c0 8000638: Improve deserialization
dmocek
parents: 11902
diff changeset
  1320
            } else {
482db5c3e9c0 8000638: Improve deserialization
dmocek
parents: 11902
diff changeset
  1321
                oscNames.add(d.name);
482db5c3e9c0 8000638: Improve deserialization
dmocek
parents: 11902
diff changeset
  1322
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            // search up inheritance hierarchy for class with matching name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            String searchName = (d.cl != null) ? d.cl.getName() : d.name;
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1326
            Class<?> match = null;
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1327
            for (Class<?> c = start; c != end; c = c.getSuperclass()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                if (searchName.equals(c.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                    match = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            // add "no data" slot for each unmatched class below match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            if (match != null) {
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1336
                for (Class<?> c = start; c != match; c = c.getSuperclass()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                    slots.add(new ClassDataSlot(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                        ObjectStreamClass.lookup(c, true), false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                start = match.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            // record descriptor/class pairing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            slots.add(new ClassDataSlot(d.getVariantFor(match), true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        // add "no data" slot for any leftover unmatched classes
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1348
        for (Class<?> c = start; c != end; c = c.getSuperclass()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            slots.add(new ClassDataSlot(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                ObjectStreamClass.lookup(c, true), false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        // order slots from superclass -> subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        Collections.reverse(slots);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1355
        return slots.toArray(new ClassDataSlot[slots.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * Returns aggregate size (in bytes) of marshalled primitive field values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * for represented class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    int getPrimDataSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        return primDataSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * Returns number of non-primitive serializable fields of represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    int getNumObjFields() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        return numObjFields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * Fetches the serializable primitive field values of object obj and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * marshals them into byte array buf starting at offset 0.  It is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * responsibility of the caller to ensure that obj is of the proper type if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * non-null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    void getPrimFieldValues(Object obj, byte[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        fieldRefl.getPrimFieldValues(obj, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * Sets the serializable primitive fields of object obj using values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * unmarshalled from byte array buf starting at offset 0.  It is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * responsibility of the caller to ensure that obj is of the proper type if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * non-null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    void setPrimFieldValues(Object obj, byte[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        fieldRefl.setPrimFieldValues(obj, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * Fetches the serializable object field values of object obj and stores
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * them in array vals starting at offset 0.  It is the responsibility of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * the caller to ensure that obj is of the proper type if non-null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    void getObjFieldValues(Object obj, Object[] vals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        fieldRefl.getObjFieldValues(obj, vals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    /**
30911
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  1404
     * Checks that the given values, from array vals starting at offset 0,
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  1405
     * are assignable to the given serializable object fields.
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  1406
     * @throws ClassCastException if any value is not assignable
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  1407
     */
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  1408
    void checkObjFieldValueTypes(Object obj, Object[] vals) {
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  1409
        fieldRefl.checkObjectFieldValueTypes(obj, vals);
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  1410
    }
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  1411
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  1412
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * Sets the serializable object fields of object obj using values from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     * array vals starting at offset 0.  It is the responsibility of the caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * to ensure that obj is of the proper type if non-null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    void setObjFieldValues(Object obj, Object[] vals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        fieldRefl.setObjFieldValues(obj, vals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * Calculates and sets serializable field offsets, as well as primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * data size and object field count totals.  Throws InvalidClassException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * if fields are illegally ordered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    private void computeFieldOffsets() throws InvalidClassException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        primDataSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        numObjFields = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        int firstObjIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        for (int i = 0; i < fields.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            ObjectStreamField f = fields[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            switch (f.getTypeCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                case 'Z':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                case 'B':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                    f.setOffset(primDataSize++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                case 'C':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                case 'S':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                    f.setOffset(primDataSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                    primDataSize += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                case 'I':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                    f.setOffset(primDataSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                    primDataSize += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                case 'J':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                case 'D':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                    f.setOffset(primDataSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                    primDataSize += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                case '[':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                case 'L':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                    f.setOffset(numObjFields++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                    if (firstObjIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                        firstObjIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                    throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        if (firstObjIndex != -1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            firstObjIndex + numObjFields != fields.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            throw new InvalidClassException(name, "illegal field order");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     * If given class is the same as the class associated with this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * descriptor, returns reference to this class descriptor.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * returns variant of this class descriptor bound to given class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     */
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1481
    private ObjectStreamClass getVariantFor(Class<?> cl)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        throws InvalidClassException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        if (this.cl == cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        ObjectStreamClass desc = new ObjectStreamClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        if (isProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            desc.initProxy(cl, null, superDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            desc.initNonProxy(this, cl, null, superDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        return desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * Returns public no-arg constructor of given class, or null if none found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * Access checks are disabled on the returned constructor (if any), since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * the defining class may still be non-public.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     */
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10419
diff changeset
  1501
    private static Constructor<?> getExternalizableConstructor(Class<?> cl) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        try {
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10419
diff changeset
  1503
            Constructor<?> cons = cl.getDeclaredConstructor((Class<?>[]) null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            cons.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            return ((cons.getModifiers() & Modifier.PUBLIC) != 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                cons : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        } catch (NoSuchMethodException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     * Returns subclass-accessible no-arg constructor of first non-serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * superclass, or null if none found.  Access checks are disabled on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * returned constructor (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     */
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10419
diff changeset
  1517
    private static Constructor<?> getSerializableConstructor(Class<?> cl) {
41608
bb724835848f 8164908: ReflectionFactory support for IIOP and custom serialization
rriggs
parents: 40410
diff changeset
  1518
        return reflFactory.newConstructorForSerialization(cl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     * Returns non-static, non-abstract method with given signature provided it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     * is defined by or accessible (via inheritance) by the given class, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     * null if no match found.  Access checks are disabled on the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     * method (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1527
    private static Method getInheritableMethod(Class<?> cl, String name,
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1528
                                               Class<?>[] argTypes,
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1529
                                               Class<?> returnType)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        Method meth = null;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1532
        Class<?> defCl = cl;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        while (defCl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                meth = defCl.getDeclaredMethod(name, argTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            } catch (NoSuchMethodException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                defCl = defCl.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        if ((meth == null) || (meth.getReturnType() != returnType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        meth.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        int mods = meth.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        if ((mods & (Modifier.STATIC | Modifier.ABSTRACT)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        } else if ((mods & (Modifier.PUBLIC | Modifier.PROTECTED)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            return meth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        } else if ((mods & Modifier.PRIVATE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
            return (cl == defCl) ? meth : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            return packageEquals(cl, defCl) ? meth : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * Returns non-static private method with given signature defined by given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     * class, or null if none found.  Access checks are disabled on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * returned method (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1563
    private static Method getPrivateMethod(Class<?> cl, String name,
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1564
                                           Class<?>[] argTypes,
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1565
                                           Class<?> returnType)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            Method meth = cl.getDeclaredMethod(name, argTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            meth.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            int mods = meth.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            return ((meth.getReturnType() == returnType) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                    ((mods & Modifier.STATIC) == 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                    ((mods & Modifier.PRIVATE) != 0)) ? meth : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        } catch (NoSuchMethodException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     * Returns true if classes are defined in the same runtime package, false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     */
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1583
    private static boolean packageEquals(Class<?> cl1, Class<?> cl2) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        return (cl1.getClassLoader() == cl2.getClassLoader() &&
47722
ce6ff74192fc 8190733: Use Class::getPackageName in java.base implementation
mchung
parents: 47400
diff changeset
  1585
                cl1.getPackageName().equals(cl2.getPackageName()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * Compares class names for equality, ignoring package names.  Returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * if class names equal, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
    private static boolean classNamesEqual(String name1, String name2) {
34357
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 33674
diff changeset
  1593
        int idx1 = name1.lastIndexOf('.') + 1;
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 33674
diff changeset
  1594
        int idx2 = name2.lastIndexOf('.') + 1;
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 33674
diff changeset
  1595
        int len1 = name1.length() - idx1;
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 33674
diff changeset
  1596
        int len2 = name2.length() - idx2;
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 33674
diff changeset
  1597
        return len1 == len2 &&
231fdaed751a 8143926: ObjectStreamField constructor eagerly load ObjectStreamClass
redestad
parents: 33674
diff changeset
  1598
                name1.regionMatches(idx1, name2, idx2, len1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     * Returns JVM type signature for given list of parameters and return type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     */
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1604
    private static String getMethodSignature(Class<?>[] paramTypes,
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1605
                                             Class<?> retType)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    {
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 37363
diff changeset
  1607
        StringBuilder sb = new StringBuilder();
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 37363
diff changeset
  1608
        sb.append('(');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        for (int i = 0; i < paramTypes.length; i++) {
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 37363
diff changeset
  1610
            appendClassSignature(sb, paramTypes[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        }
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 37363
diff changeset
  1612
        sb.append(')');
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 37363
diff changeset
  1613
        appendClassSignature(sb, retType);
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 37363
diff changeset
  1614
        return sb.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * Convenience method for throwing an exception that is either a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     * RuntimeException, Error, or of some unexpected type (in which case it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     * wrapped inside an IOException).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
    private static void throwMiscException(Throwable th) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        if (th instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            throw (RuntimeException) th;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        } else if (th instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            throw (Error) th;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
            IOException ex = new IOException("unexpected exception type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            ex.initCause(th);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     * Returns ObjectStreamField array describing the serializable fields of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * the given class.  Serializable fields backed by an actual field of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     * class are represented by ObjectStreamFields with corresponding non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * Field objects.  Throws InvalidClassException if the (explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * declared) serializable fields are invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     */
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1641
    private static ObjectStreamField[] getSerialFields(Class<?> cl)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        throws InvalidClassException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        ObjectStreamField[] fields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        if (Serializable.class.isAssignableFrom(cl) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            !Externalizable.class.isAssignableFrom(cl) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            !Proxy.isProxyClass(cl) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
            !cl.isInterface())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            if ((fields = getDeclaredSerialFields(cl)) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                fields = getDefaultSerialFields(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
            Arrays.sort(fields);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            fields = NO_FIELDS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        return fields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * Returns serializable fields of given class as defined explicitly by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * "serialPersistentFields" field, or null if no appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     * "serialPersistentFields" field is defined.  Serializable fields backed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     * by an actual field of the class are represented by ObjectStreamFields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * with corresponding non-null Field objects.  For compatibility with past
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     * releases, a "serialPersistentFields" field with a null value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     * considered equivalent to not declaring "serialPersistentFields".  Throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * InvalidClassException if the declared serializable fields are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * invalid--e.g., if multiple fields share the same name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     */
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1671
    private static ObjectStreamField[] getDeclaredSerialFields(Class<?> cl)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        throws InvalidClassException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        ObjectStreamField[] serialPersistentFields = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            Field f = cl.getDeclaredField("serialPersistentFields");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
            int mask = Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
            if ((f.getModifiers() & mask) == mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                f.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                serialPersistentFields = (ObjectStreamField[]) f.get(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        if (serialPersistentFields == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        } else if (serialPersistentFields.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            return NO_FIELDS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        ObjectStreamField[] boundFields =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            new ObjectStreamField[serialPersistentFields.length];
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7031
diff changeset
  1692
        Set<String> fieldNames = new HashSet<>(serialPersistentFields.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        for (int i = 0; i < serialPersistentFields.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            ObjectStreamField spf = serialPersistentFields[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
            String fname = spf.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
            if (fieldNames.contains(fname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                throw new InvalidClassException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                    "multiple serializable fields named " + fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
            fieldNames.add(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                Field f = cl.getDeclaredField(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                if ((f.getType() == spf.getType()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                    ((f.getModifiers() & Modifier.STATIC) == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                    boundFields[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                        new ObjectStreamField(f, spf.isUnshared(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
            } catch (NoSuchFieldException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            if (boundFields[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                boundFields[i] = new ObjectStreamField(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                    fname, spf.getType(), spf.isUnshared());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        return boundFields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
     * Returns array of ObjectStreamFields corresponding to all non-static
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     * non-transient fields declared by given class.  Each ObjectStreamField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * contains a Field object for the field it represents.  If no default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     * serializable fields exist, NO_FIELDS is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     */
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1728
    private static ObjectStreamField[] getDefaultSerialFields(Class<?> cl) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
        Field[] clFields = cl.getDeclaredFields();
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7031
diff changeset
  1730
        ArrayList<ObjectStreamField> list = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        int mask = Modifier.STATIC | Modifier.TRANSIENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        for (int i = 0; i < clFields.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
            if ((clFields[i].getModifiers() & mask) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                list.add(new ObjectStreamField(clFields[i], false, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        int size = list.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        return (size == 0) ? NO_FIELDS :
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1740
            list.toArray(new ObjectStreamField[size]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     * Returns explicit serial version UID value declared by given class, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     * null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     */
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1747
    private static Long getDeclaredSUID(Class<?> cl) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            Field f = cl.getDeclaredField("serialVersionUID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
            int mask = Modifier.STATIC | Modifier.FINAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            if ((f.getModifiers() & mask) == mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                f.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                return Long.valueOf(f.getLong(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     * Computes the default serial version UID value for the given class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     */
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1763
    private static long computeDefaultSUID(Class<?> cl) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        if (!Serializable.class.isAssignableFrom(cl) || Proxy.isProxyClass(cl))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
            return 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            DataOutputStream dout = new DataOutputStream(bout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
            dout.writeUTF(cl.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
            int classMods = cl.getModifiers() &
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                (Modifier.PUBLIC | Modifier.FINAL |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
                 Modifier.INTERFACE | Modifier.ABSTRACT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
             * compensate for javac bug in which ABSTRACT bit was set for an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
             * interface only if the interface declared methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            Method[] methods = cl.getDeclaredMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            if ((classMods & Modifier.INTERFACE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                classMods = (methods.length > 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                    (classMods | Modifier.ABSTRACT) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                    (classMods & ~Modifier.ABSTRACT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
            dout.writeInt(classMods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
            if (!cl.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                 * compensate for change in 1.2FCS in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                 * Class.getInterfaces() was modified to return Cloneable and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                 * Serializable for array classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                 */
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1797
                Class<?>[] interfaces = cl.getInterfaces();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                String[] ifaceNames = new String[interfaces.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                    ifaceNames[i] = interfaces[i].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                Arrays.sort(ifaceNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                for (int i = 0; i < ifaceNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
                    dout.writeUTF(ifaceNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
            Field[] fields = cl.getDeclaredFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
            MemberSignature[] fieldSigs = new MemberSignature[fields.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
            for (int i = 0; i < fields.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                fieldSigs[i] = new MemberSignature(fields[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
            }
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
  1813
            Arrays.sort(fieldSigs, new Comparator<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1814
                public int compare(MemberSignature ms1, MemberSignature ms2) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1815
                    return ms1.name.compareTo(ms2.name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
            for (int i = 0; i < fieldSigs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                MemberSignature sig = fieldSigs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                int mods = sig.member.getModifiers() &
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                    (Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                     Modifier.STATIC | Modifier.FINAL | Modifier.VOLATILE |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                     Modifier.TRANSIENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                if (((mods & Modifier.PRIVATE) == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                    ((mods & (Modifier.STATIC | Modifier.TRANSIENT)) == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                    dout.writeUTF(sig.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                    dout.writeInt(mods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                    dout.writeUTF(sig.signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            if (hasStaticInitializer(cl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                dout.writeUTF("<clinit>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                dout.writeInt(Modifier.STATIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                dout.writeUTF("()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
21655
55f32ae4f920 8028229: Fix more raw types lint warning in core libraries
darcy
parents: 18260
diff changeset
  1839
            Constructor<?>[] cons = cl.getDeclaredConstructors();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            MemberSignature[] consSigs = new MemberSignature[cons.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            for (int i = 0; i < cons.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                consSigs[i] = new MemberSignature(cons[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            }
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
  1844
            Arrays.sort(consSigs, new Comparator<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1845
                public int compare(MemberSignature ms1, MemberSignature ms2) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1846
                    return ms1.signature.compareTo(ms2.signature);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
            for (int i = 0; i < consSigs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                MemberSignature sig = consSigs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                int mods = sig.member.getModifiers() &
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                    (Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                     Modifier.STATIC | Modifier.FINAL |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                     Modifier.SYNCHRONIZED | Modifier.NATIVE |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                     Modifier.ABSTRACT | Modifier.STRICT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                if ((mods & Modifier.PRIVATE) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                    dout.writeUTF("<init>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                    dout.writeInt(mods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                    dout.writeUTF(sig.signature.replace('/', '.'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            MemberSignature[] methSigs = new MemberSignature[methods.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
            for (int i = 0; i < methods.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
                methSigs[i] = new MemberSignature(methods[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            }
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
  1867
            Arrays.sort(methSigs, new Comparator<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1868
                public int compare(MemberSignature ms1, MemberSignature ms2) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                    int comp = ms1.name.compareTo(ms2.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                    if (comp == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                        comp = ms1.signature.compareTo(ms2.signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            for (int i = 0; i < methSigs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
                MemberSignature sig = methSigs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                int mods = sig.member.getModifiers() &
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                    (Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                     Modifier.STATIC | Modifier.FINAL |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                     Modifier.SYNCHRONIZED | Modifier.NATIVE |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                     Modifier.ABSTRACT | Modifier.STRICT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                if ((mods & Modifier.PRIVATE) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                    dout.writeUTF(sig.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                    dout.writeInt(mods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                    dout.writeUTF(sig.signature.replace('/', '.'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
            dout.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
            MessageDigest md = MessageDigest.getInstance("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
            byte[] hashBytes = md.digest(bout.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            long hash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
            for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
                hash = (hash << 8) | (hashBytes[i] & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
            return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        } catch (IOException ex) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
  1900
            throw new InternalError(ex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        } catch (NoSuchAlgorithmException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
            throw new SecurityException(ex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     * Returns true if the given class defines a static initializer method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     * false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30911
diff changeset
  1910
    private static native boolean hasStaticInitializer(Class<?> cl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     * Class for computing and caching field/constructor/method signatures
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     * during serialVersionUID calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
    private static class MemberSignature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        public final Member member;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        public final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
        public final String signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
        public MemberSignature(Field field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
            member = field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            name = field.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            signature = getClassSignature(field.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10419
diff changeset
  1928
        public MemberSignature(Constructor<?> cons) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
            member = cons;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
            name = cons.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
            signature = getMethodSignature(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
                cons.getParameterTypes(), Void.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        public MemberSignature(Method meth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
            member = meth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
            name = meth.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
            signature = getMethodSignature(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                meth.getParameterTypes(), meth.getReturnType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     * Class for setting and retrieving serializable field values in batch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
    // REMIND: dynamically generate these?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
    private static class FieldReflector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        /** handle for performing unsafe operations */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        private static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        /** fields to operate on */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        private final ObjectStreamField[] fields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        /** number of primitive fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        private final int numPrimFields;
7031
d77ff2048ad5 6966692: defaultReadObject can set a field multiple times
skoppar
parents: 5506
diff changeset
  1956
        /** unsafe field keys for reading fields - may contain dupes */
d77ff2048ad5 6966692: defaultReadObject can set a field multiple times
skoppar
parents: 5506
diff changeset
  1957
        private final long[] readKeys;
d77ff2048ad5 6966692: defaultReadObject can set a field multiple times
skoppar
parents: 5506
diff changeset
  1958
        /** unsafe fields keys for writing fields - no dupes */
d77ff2048ad5 6966692: defaultReadObject can set a field multiple times
skoppar
parents: 5506
diff changeset
  1959
        private final long[] writeKeys;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        /** field data offsets */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        private final int[] offsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        /** field type codes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        private final char[] typeCodes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        /** field types */
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  1965
        private final Class<?>[] types;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
         * Constructs FieldReflector capable of setting/getting values from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
         * subset of fields whose ObjectStreamFields contain non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
         * reflective Field objects.  ObjectStreamFields with null Fields are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
         * treated as filler, for which get operations return default values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
         * and set operations discard given values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
        FieldReflector(ObjectStreamField[] fields) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
            this.fields = fields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
            int nfields = fields.length;
7031
d77ff2048ad5 6966692: defaultReadObject can set a field multiple times
skoppar
parents: 5506
diff changeset
  1977
            readKeys = new long[nfields];
d77ff2048ad5 6966692: defaultReadObject can set a field multiple times
skoppar
parents: 5506
diff changeset
  1978
            writeKeys = new long[nfields];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            offsets = new int[nfields];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            typeCodes = new char[nfields];
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7031
diff changeset
  1981
            ArrayList<Class<?>> typeList = new ArrayList<>();
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7031
diff changeset
  1982
            Set<Long> usedKeys = new HashSet<>();
7031
d77ff2048ad5 6966692: defaultReadObject can set a field multiple times
skoppar
parents: 5506
diff changeset
  1983
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
            for (int i = 0; i < nfields; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                ObjectStreamField f = fields[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                Field rf = f.getField();
7031
d77ff2048ad5 6966692: defaultReadObject can set a field multiple times
skoppar
parents: 5506
diff changeset
  1988
                long key = (rf != null) ?
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                    unsafe.objectFieldOffset(rf) : Unsafe.INVALID_FIELD_OFFSET;
7031
d77ff2048ad5 6966692: defaultReadObject can set a field multiple times
skoppar
parents: 5506
diff changeset
  1990
                readKeys[i] = key;
d77ff2048ad5 6966692: defaultReadObject can set a field multiple times
skoppar
parents: 5506
diff changeset
  1991
                writeKeys[i] = usedKeys.add(key) ?
d77ff2048ad5 6966692: defaultReadObject can set a field multiple times
skoppar
parents: 5506
diff changeset
  1992
                    key : Unsafe.INVALID_FIELD_OFFSET;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                offsets[i] = f.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                typeCodes[i] = f.getTypeCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                if (!f.isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                    typeList.add((rf != null) ? rf.getType() : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2000
            types = typeList.toArray(new Class<?>[typeList.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            numPrimFields = nfields - types.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
         * Returns list of ObjectStreamFields representing fields operated on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
         * by this reflector.  The shared/unshared values and Field objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
         * contained by ObjectStreamFields in the list reflect their bindings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
         * to locally defined serializable fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        ObjectStreamField[] getFields() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
            return fields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
         * Fetches the serializable primitive field values of object obj and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
         * marshals them into byte array buf starting at offset 0.  The caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
         * is responsible for ensuring that obj is of the proper type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
        void getPrimFieldValues(Object obj, byte[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
            if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
            /* assuming checkDefaultSerialize() has been called on the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
             * descriptor this FieldReflector was obtained from, no field keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
             * in array should be equal to Unsafe.INVALID_FIELD_OFFSET.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
            for (int i = 0; i < numPrimFields; i++) {
7031
d77ff2048ad5 6966692: defaultReadObject can set a field multiple times
skoppar
parents: 5506
diff changeset
  2028
                long key = readKeys[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                int off = offsets[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                switch (typeCodes[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                    case 'Z':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                        Bits.putBoolean(buf, off, unsafe.getBoolean(obj, key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
                    case 'B':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
                        buf[off] = unsafe.getByte(obj, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                    case 'C':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                        Bits.putChar(buf, off, unsafe.getChar(obj, key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                    case 'S':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
                        Bits.putShort(buf, off, unsafe.getShort(obj, key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
                    case 'I':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                        Bits.putInt(buf, off, unsafe.getInt(obj, key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                    case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                        Bits.putFloat(buf, off, unsafe.getFloat(obj, key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
                    case 'J':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                        Bits.putLong(buf, off, unsafe.getLong(obj, key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                    case 'D':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
                        Bits.putDouble(buf, off, unsafe.getDouble(obj, key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
         * Sets the serializable primitive fields of object obj using values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
         * unmarshalled from byte array buf starting at offset 0.  The caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
         * is responsible for ensuring that obj is of the proper type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        void setPrimFieldValues(Object obj, byte[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
            if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            for (int i = 0; i < numPrimFields; i++) {
7031
d77ff2048ad5 6966692: defaultReadObject can set a field multiple times
skoppar
parents: 5506
diff changeset
  2079
                long key = writeKeys[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                if (key == Unsafe.INVALID_FIELD_OFFSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                    continue;           // discard value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                int off = offsets[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                switch (typeCodes[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                    case 'Z':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                        unsafe.putBoolean(obj, key, Bits.getBoolean(buf, off));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                    case 'B':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                        unsafe.putByte(obj, key, buf[off]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                    case 'C':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                        unsafe.putChar(obj, key, Bits.getChar(buf, off));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
                    case 'S':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
                        unsafe.putShort(obj, key, Bits.getShort(buf, off));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                    case 'I':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                        unsafe.putInt(obj, key, Bits.getInt(buf, off));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                    case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                        unsafe.putFloat(obj, key, Bits.getFloat(buf, off));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                    case 'J':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                        unsafe.putLong(obj, key, Bits.getLong(buf, off));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
                    case 'D':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                        unsafe.putDouble(obj, key, Bits.getDouble(buf, off));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
         * Fetches the serializable object field values of object obj and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
         * stores them in array vals starting at offset 0.  The caller is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
         * responsible for ensuring that obj is of the proper type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
        void getObjFieldValues(Object obj, Object[] vals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
            if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
            /* assuming checkDefaultSerialize() has been called on the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
             * descriptor this FieldReflector was obtained from, no field keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
             * in array should be equal to Unsafe.INVALID_FIELD_OFFSET.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
            for (int i = numPrimFields; i < fields.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                switch (typeCodes[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                    case 'L':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                    case '[':
52220
9c260a6b6471 8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents: 47722
diff changeset
  2140
                        vals[offsets[i]] = unsafe.getReference(obj, readKeys[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
        /**
30911
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2150
         * Checks that the given values, from array vals starting at offset 0,
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2151
         * are assignable to the given serializable object fields.
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2152
         * @throws ClassCastException if any value is not assignable
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2153
         */
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2154
        void checkObjectFieldValueTypes(Object obj, Object[] vals) {
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2155
            setObjFieldValues(obj, vals, true);
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2156
        }
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2157
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2158
        /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
         * Sets the serializable object fields of object obj using values from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
         * array vals starting at offset 0.  The caller is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
         * ensuring that obj is of the proper type; however, attempts to set a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
         * field with a value of the wrong type will trigger an appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
         * ClassCastException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
        void setObjFieldValues(Object obj, Object[] vals) {
30911
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2166
            setObjFieldValues(obj, vals, false);
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2167
        }
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2168
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2169
        private void setObjFieldValues(Object obj, Object[] vals, boolean dryRun) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
            if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
            for (int i = numPrimFields; i < fields.length; i++) {
7031
d77ff2048ad5 6966692: defaultReadObject can set a field multiple times
skoppar
parents: 5506
diff changeset
  2174
                long key = writeKeys[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                if (key == Unsafe.INVALID_FIELD_OFFSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                    continue;           // discard value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                switch (typeCodes[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                    case 'L':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                    case '[':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                        Object val = vals[offsets[i]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                        if (val != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                            !types[i - numPrimFields].isInstance(val))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                            Field f = fields[i].getField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                            throw new ClassCastException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                                "cannot assign instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                                val.getClass().getName() + " to field " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                                f.getDeclaringClass().getName() + "." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                                f.getName() + " of type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                                f.getType().getName() + " in instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                                obj.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                        }
30911
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2194
                        if (!dryRun)
52220
9c260a6b6471 8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents: 47722
diff changeset
  2195
                            unsafe.putReference(obj, key, val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
     * Matches given set of serializable fields with serializable fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
     * described by the given local class descriptor, and returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     * FieldReflector instance capable of setting/getting values from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     * subset of fields that match (non-matching fields are treated as filler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
     * for which get operations return default values and set operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     * discard given values).  Throws InvalidClassException if unresolvable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     * type conflicts exist between the two sets of fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
    private static FieldReflector getReflector(ObjectStreamField[] fields,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                                               ObjectStreamClass localDesc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
        throws InvalidClassException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
        // class irrelevant if no fields
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  2219
        Class<?> cl = (localDesc != null && fields.length > 0) ?
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
            localDesc.cl : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
        processQueue(Caches.reflectorsQueue, Caches.reflectors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
        FieldReflectorKey key = new FieldReflectorKey(cl, fields,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                                                      Caches.reflectorsQueue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
        Reference<?> ref = Caches.reflectors.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        Object entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        if (ref != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
            entry = ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
        EntryFuture future = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
        if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
            EntryFuture newEntry = new EntryFuture();
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7031
diff changeset
  2232
            Reference<?> newRef = new SoftReference<>(newEntry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                if (ref != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                    Caches.reflectors.remove(key, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                ref = Caches.reflectors.putIfAbsent(key, newRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                if (ref != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
                    entry = ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
            } while (ref != null && entry == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
            if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
                future = newEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
        if (entry instanceof FieldReflector) {  // check common case first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
            return (FieldReflector) entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
        } else if (entry instanceof EntryFuture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
            entry = ((EntryFuture) entry).get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
        } else if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
                entry = new FieldReflector(matchFields(fields, localDesc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
            } catch (Throwable th) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                entry = th;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
            future.set(entry);
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
  2258
            Caches.reflectors.put(key, new SoftReference<>(entry));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
        if (entry instanceof FieldReflector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
            return (FieldReflector) entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
        } else if (entry instanceof InvalidClassException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
            throw (InvalidClassException) entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
        } else if (entry instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
            throw (RuntimeException) entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
        } else if (entry instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            throw (Error) entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            throw new InternalError("unexpected entry: " + entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
     * FieldReflector cache lookup key.  Keys are considered equal if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
     * refer to the same class and equivalent field formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
    private static class FieldReflectorKey extends WeakReference<Class<?>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
53127
b561ea19a7b9 6996807: FieldReflectorKey hash code computation can be improved
igerasim
parents: 52427
diff changeset
  2280
        private final String[] sigs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
        private final int hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
        private final boolean nullClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 715
diff changeset
  2284
        FieldReflectorKey(Class<?> cl, ObjectStreamField[] fields,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                          ReferenceQueue<Class<?>> queue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
            super(cl, queue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            nullClass = (cl == null);
53127
b561ea19a7b9 6996807: FieldReflectorKey hash code computation can be improved
igerasim
parents: 52427
diff changeset
  2289
            sigs = new String[2 * fields.length];
b561ea19a7b9 6996807: FieldReflectorKey hash code computation can be improved
igerasim
parents: 52427
diff changeset
  2290
            for (int i = 0, j = 0; i < fields.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                ObjectStreamField f = fields[i];
53127
b561ea19a7b9 6996807: FieldReflectorKey hash code computation can be improved
igerasim
parents: 52427
diff changeset
  2292
                sigs[j++] = f.getName();
b561ea19a7b9 6996807: FieldReflectorKey hash code computation can be improved
igerasim
parents: 52427
diff changeset
  2293
                sigs[j++] = f.getSignature();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
            }
53127
b561ea19a7b9 6996807: FieldReflectorKey hash code computation can be improved
igerasim
parents: 52427
diff changeset
  2295
            hash = System.identityHashCode(cl) + Arrays.hashCode(sigs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
            return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
            if (obj == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
            if (obj instanceof FieldReflectorKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
                FieldReflectorKey other = (FieldReflectorKey) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
                Class<?> referent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
                return (nullClass ? other.nullClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                                  : ((referent = get()) != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                                    (referent == other.get())) &&
53127
b561ea19a7b9 6996807: FieldReflectorKey hash code computation can be improved
igerasim
parents: 52427
diff changeset
  2313
                        Arrays.equals(sigs, other.sigs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
     * Matches given set of serializable fields with serializable fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
     * obtained from the given local class descriptor (which contain bindings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
     * to reflective Field objects).  Returns list of ObjectStreamFields in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
     * which each ObjectStreamField whose signature matches that of a local
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
     * field contains a Field object for that field; unmatched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
     * ObjectStreamFields contain null Field objects.  Shared/unshared settings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
     * of the returned ObjectStreamFields also reflect those of matched local
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
     * ObjectStreamFields.  Throws InvalidClassException if unresolvable type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
     * conflicts exist between the two sets of fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
    private static ObjectStreamField[] matchFields(ObjectStreamField[] fields,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                                                   ObjectStreamClass localDesc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
        throws InvalidClassException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
        ObjectStreamField[] localFields = (localDesc != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
            localDesc.fields : NO_FIELDS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
         * Even if fields == localFields, we cannot simply return localFields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
         * here.  In previous implementations of serialization,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
         * ObjectStreamField.getType() returned Object.class if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
         * ObjectStreamField represented a non-primitive field and belonged to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
         * a non-local class descriptor.  To preserve this (questionable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
         * behavior, the ObjectStreamField instances returned by matchFields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
         * cannot report non-primitive types other than Object.class; hence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
         * localFields cannot be returned directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
        ObjectStreamField[] matches = new ObjectStreamField[fields.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
        for (int i = 0; i < fields.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
            ObjectStreamField f = fields[i], m = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
            for (int j = 0; j < localFields.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                ObjectStreamField lf = localFields[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                if (f.getName().equals(lf.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                    if ((f.isPrimitive() || lf.isPrimitive()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
                        f.getTypeCode() != lf.getTypeCode())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                        throw new InvalidClassException(localDesc.name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
                            "incompatible types for field " + f.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                    if (lf.getField() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
                        m = new ObjectStreamField(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                            lf.getField(), lf.isUnshared(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
                        m = new ObjectStreamField(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
                            lf.getName(), lf.getSignature(), lf.isUnshared());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
            if (m == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
                m = new ObjectStreamField(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
                    f.getName(), f.getSignature(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
            m.setOffset(f.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
            matches[i] = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
        return matches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
     * Removes from the specified map any keys that have been enqueued
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
     * on the specified reference queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
    static void processQueue(ReferenceQueue<Class<?>> queue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
                             ConcurrentMap<? extends
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
                             WeakReference<Class<?>>, ?> map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
        Reference<? extends Class<?>> ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
        while((ref = queue.poll()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
            map.remove(ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
     *  Weak key for Class objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
    static class WeakClassKey extends WeakReference<Class<?>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
         * saved value of the referent's identity hash code, to maintain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
         * a consistent hash code after the referent has been cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
        private final int hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
         * Create a new WeakClassKey to the given object, registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
         * with a queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
        WeakClassKey(Class<?> cl, ReferenceQueue<Class<?>> refQueue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
            super(cl, refQueue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
            hash = System.identityHashCode(cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
         * Returns the identity hash code of the original referent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
            return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
         * Returns true if the given object is this identical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
         * WeakClassKey instance, or, if this object's referent has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
         * been cleared, if the given object is another WeakClassKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
         * instance with the identical non-null referent as this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
            if (obj == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
            if (obj instanceof WeakClassKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
                Object referent = get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
                return (referent != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
                       (referent == ((WeakClassKey) obj).get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
}