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