jdk/src/share/classes/java/io/ObjectStreamField.java
author mchung
Tue, 27 Oct 2009 16:31:01 -0700
changeset 4171 7edb512fdaa6
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot Summary: Duplicate ObjectStreamClass.getClassSignature method in ObjectStreamField class Reviewed-by: alanb
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.reflect.Field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A description of a Serializable field from a Serializable class.  An array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * of ObjectStreamFields is used to declare the Serializable fields of a class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author      Mike Warres
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author      Roger Riggs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @see ObjectStreamClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class ObjectStreamField
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    implements Comparable<Object>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /** field name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /** canonical JVM signature of field type */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private final String signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /** field type (Object.class if unknown non-primitive type) */
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
    48
    private final Class<?> type;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /** whether or not to (de)serialize field values as unshared */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private final boolean unshared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /** corresponding reflective field object, if any */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private final Field field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /** offset of field value in enclosing field group */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private int offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Create a Serializable field with the specified type.  This field should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * be documented with a <code>serialField</code> tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param   name the name of the serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @param   type the <code>Class</code> object of the serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public ObjectStreamField(String name, Class<?> type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        this(name, type, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Creates an ObjectStreamField representing a serializable field with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * given name and type.  If unshared is false, values of the represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * field are serialized and deserialized in the default manner--if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * field is non-primitive, object values are serialized and deserialized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * if they had been written and read by calls to writeObject and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * readObject.  If unshared is true, values of the represented field are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * serialized and deserialized as if they had been written and read by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * calls to writeUnshared and readUnshared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param   name field name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param   type field type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param   unshared if false, write/read field values in the same manner
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *          as writeObject/readObject; if true, write/read in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *          manner as writeUnshared/readUnshared
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public ObjectStreamField(String name, Class<?> type, boolean unshared) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        this.unshared = unshared;
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
    91
        signature = getClassSignature(type).intern();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        field = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Creates an ObjectStreamField representing a field with the given name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * signature and unshared setting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    ObjectStreamField(String name, String signature, boolean unshared) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.signature = signature.intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.unshared = unshared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        field = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        switch (signature.charAt(0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            case 'Z': type = Boolean.TYPE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            case 'B': type = Byte.TYPE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            case 'C': type = Character.TYPE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            case 'S': type = Short.TYPE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            case 'I': type = Integer.TYPE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            case 'J': type = Long.TYPE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            case 'F': type = Float.TYPE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            case 'D': type = Double.TYPE; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            case 'L':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            case '[': type = Object.class; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            default: throw new IllegalArgumentException("illegal signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Creates an ObjectStreamField representing the given field with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * specified unshared setting.  For compatibility with the behavior of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * earlier serialization implementations, a "showType" parameter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * necessary to govern whether or not a getType() call on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * ObjectStreamField (if non-primitive) will return Object.class (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * opposed to a more specific reference type).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    ObjectStreamField(Field field, boolean unshared, boolean showType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.field = field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this.unshared = unshared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        name = field.getName();
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   135
        Class<?> ftype = field.getType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        type = (showType || ftype.isPrimitive()) ? ftype : Object.class;
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   137
        signature = getClassSignature(ftype).intern();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Get the name of this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @return  a <code>String</code> representing the name of the serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *          field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Get the type of the field.  If the type is non-primitive and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <code>ObjectStreamField</code> was obtained from a deserialized {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * ObjectStreamClass} instance, then <code>Object.class</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Otherwise, the <code>Class</code> object for the type of the field is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @return  a <code>Class</code> object representing the type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *          serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public Class<?> getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Returns character encoding of field type.  The encoding is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * B            byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * C            char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * D            double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * F            float
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * I            int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * J            long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * L            class or interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * S            short
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Z            boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * [            array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @return  the typecode of the serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    // REMIND: deprecate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public char getTypeCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return signature.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Return the JVM type signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @return  null if this field has a primitive type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    // REMIND: deprecate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public String getTypeString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return isPrimitive() ? null : signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Offset of field within instance data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @return  the offset of this field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @see #setOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    // REMIND: deprecate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public int getOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Offset within instance data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param   offset the offset of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @see #getOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    // REMIND: deprecate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    protected void setOffset(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Return true if this field has a primitive type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @return  true if and only if this field corresponds to a primitive type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    // REMIND: deprecate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public boolean isPrimitive() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        char tcode = signature.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return ((tcode != 'L') && (tcode != '['));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Returns boolean value indicating whether or not the serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * represented by this ObjectStreamField instance is unshared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public boolean isUnshared() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return unshared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Compare this field with another <code>ObjectStreamField</code>.  Return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * -1 if this is smaller, 0 if equal, 1 if greater.  Types that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * primitives are "smaller" than object types.  If equal, the field names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * are compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    // REMIND: deprecate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public int compareTo(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        ObjectStreamField other = (ObjectStreamField) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        boolean isPrim = isPrimitive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (isPrim != other.isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return isPrim ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return name.compareTo(other.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Return a string that describes this field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return signature + ' ' + name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Returns field represented by this ObjectStreamField, or null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * ObjectStreamField is not associated with an actual field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    Field getField() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Returns JVM type signature of field (similar to getTypeString, except
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * that signature strings are returned for primitive fields as well).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    String getSignature() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
4171
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   277
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   278
    /**
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   279
     * Returns JVM type signature for given class.
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   280
     */
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   281
    private static String getClassSignature(Class<?> cl) {
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   282
        StringBuilder sbuf = new StringBuilder();
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   283
        while (cl.isArray()) {
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   284
            sbuf.append('[');
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   285
            cl = cl.getComponentType();
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   286
        }
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   287
        if (cl.isPrimitive()) {
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   288
            if (cl == Integer.TYPE) {
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   289
                sbuf.append('I');
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   290
            } else if (cl == Byte.TYPE) {
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   291
                sbuf.append('B');
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   292
            } else if (cl == Long.TYPE) {
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   293
                sbuf.append('J');
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   294
            } else if (cl == Float.TYPE) {
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   295
                sbuf.append('F');
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   296
            } else if (cl == Double.TYPE) {
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   297
                sbuf.append('D');
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   298
            } else if (cl == Short.TYPE) {
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   299
                sbuf.append('S');
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   300
            } else if (cl == Character.TYPE) {
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   301
                sbuf.append('C');
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   302
            } else if (cl == Boolean.TYPE) {
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   303
                sbuf.append('Z');
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   304
            } else if (cl == Void.TYPE) {
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   305
                sbuf.append('V');
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   306
            } else {
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   307
                throw new InternalError();
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   308
            }
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   309
        } else {
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   310
            sbuf.append('L' + cl.getName().replace('.', '/') + ';');
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   311
        }
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   312
        return sbuf.toString();
7edb512fdaa6 6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents: 2
diff changeset
   313
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
}