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