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