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