emul/src/main/java/java/io/ObjectStreamField.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 29 Sep 2012 06:47:05 +0200
branchjdk7-b147
changeset 56 d7a291b7808d
permissions -rw-r--r--
3rd batch of classes
jaroslav@56
     1
/*
jaroslav@56
     2
 * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
jaroslav@56
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@56
     4
 *
jaroslav@56
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@56
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@56
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@56
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@56
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@56
    10
 *
jaroslav@56
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@56
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@56
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@56
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@56
    15
 * accompanied this code).
jaroslav@56
    16
 *
jaroslav@56
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@56
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@56
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@56
    20
 *
jaroslav@56
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@56
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@56
    23
 * questions.
jaroslav@56
    24
 */
jaroslav@56
    25
jaroslav@56
    26
package java.io;
jaroslav@56
    27
jaroslav@56
    28
import java.lang.reflect.Field;
jaroslav@56
    29
jaroslav@56
    30
/**
jaroslav@56
    31
 * A description of a Serializable field from a Serializable class.  An array
jaroslav@56
    32
 * of ObjectStreamFields is used to declare the Serializable fields of a class.
jaroslav@56
    33
 *
jaroslav@56
    34
 * @author      Mike Warres
jaroslav@56
    35
 * @author      Roger Riggs
jaroslav@56
    36
 * @see ObjectStreamClass
jaroslav@56
    37
 * @since 1.2
jaroslav@56
    38
 */
jaroslav@56
    39
public class ObjectStreamField
jaroslav@56
    40
    implements Comparable<Object>
jaroslav@56
    41
{
jaroslav@56
    42
jaroslav@56
    43
    /** field name */
jaroslav@56
    44
    private final String name;
jaroslav@56
    45
    /** canonical JVM signature of field type */
jaroslav@56
    46
    private final String signature;
jaroslav@56
    47
    /** field type (Object.class if unknown non-primitive type) */
jaroslav@56
    48
    private final Class<?> type;
jaroslav@56
    49
    /** whether or not to (de)serialize field values as unshared */
jaroslav@56
    50
    private final boolean unshared;
jaroslav@56
    51
    /** corresponding reflective field object, if any */
jaroslav@56
    52
    private final Field field;
jaroslav@56
    53
    /** offset of field value in enclosing field group */
jaroslav@56
    54
    private int offset = 0;
jaroslav@56
    55
jaroslav@56
    56
    /**
jaroslav@56
    57
     * Create a Serializable field with the specified type.  This field should
jaroslav@56
    58
     * be documented with a <code>serialField</code> tag.
jaroslav@56
    59
     *
jaroslav@56
    60
     * @param   name the name of the serializable field
jaroslav@56
    61
     * @param   type the <code>Class</code> object of the serializable field
jaroslav@56
    62
     */
jaroslav@56
    63
    public ObjectStreamField(String name, Class<?> type) {
jaroslav@56
    64
        this(name, type, false);
jaroslav@56
    65
    }
jaroslav@56
    66
jaroslav@56
    67
    /**
jaroslav@56
    68
     * Creates an ObjectStreamField representing a serializable field with the
jaroslav@56
    69
     * given name and type.  If unshared is false, values of the represented
jaroslav@56
    70
     * field are serialized and deserialized in the default manner--if the
jaroslav@56
    71
     * field is non-primitive, object values are serialized and deserialized as
jaroslav@56
    72
     * if they had been written and read by calls to writeObject and
jaroslav@56
    73
     * readObject.  If unshared is true, values of the represented field are
jaroslav@56
    74
     * serialized and deserialized as if they had been written and read by
jaroslav@56
    75
     * calls to writeUnshared and readUnshared.
jaroslav@56
    76
     *
jaroslav@56
    77
     * @param   name field name
jaroslav@56
    78
     * @param   type field type
jaroslav@56
    79
     * @param   unshared if false, write/read field values in the same manner
jaroslav@56
    80
     *          as writeObject/readObject; if true, write/read in the same
jaroslav@56
    81
     *          manner as writeUnshared/readUnshared
jaroslav@56
    82
     * @since   1.4
jaroslav@56
    83
     */
jaroslav@56
    84
    public ObjectStreamField(String name, Class<?> type, boolean unshared) {
jaroslav@56
    85
        if (name == null) {
jaroslav@56
    86
            throw new NullPointerException();
jaroslav@56
    87
        }
jaroslav@56
    88
        this.name = name;
jaroslav@56
    89
        this.type = type;
jaroslav@56
    90
        this.unshared = unshared;
jaroslav@56
    91
        signature = getClassSignature(type).intern();
jaroslav@56
    92
        field = null;
jaroslav@56
    93
    }
jaroslav@56
    94
jaroslav@56
    95
    /**
jaroslav@56
    96
     * Creates an ObjectStreamField representing a field with the given name,
jaroslav@56
    97
     * signature and unshared setting.
jaroslav@56
    98
     */
jaroslav@56
    99
    ObjectStreamField(String name, String signature, boolean unshared) {
jaroslav@56
   100
        if (name == null) {
jaroslav@56
   101
            throw new NullPointerException();
jaroslav@56
   102
        }
jaroslav@56
   103
        this.name = name;
jaroslav@56
   104
        this.signature = signature.intern();
jaroslav@56
   105
        this.unshared = unshared;
jaroslav@56
   106
        field = null;
jaroslav@56
   107
jaroslav@56
   108
        switch (signature.charAt(0)) {
jaroslav@56
   109
            case 'Z': type = Boolean.TYPE; break;
jaroslav@56
   110
            case 'B': type = Byte.TYPE; break;
jaroslav@56
   111
            case 'C': type = Character.TYPE; break;
jaroslav@56
   112
            case 'S': type = Short.TYPE; break;
jaroslav@56
   113
            case 'I': type = Integer.TYPE; break;
jaroslav@56
   114
            case 'J': type = Long.TYPE; break;
jaroslav@56
   115
            case 'F': type = Float.TYPE; break;
jaroslav@56
   116
            case 'D': type = Double.TYPE; break;
jaroslav@56
   117
            case 'L':
jaroslav@56
   118
            case '[': type = Object.class; break;
jaroslav@56
   119
            default: throw new IllegalArgumentException("illegal signature");
jaroslav@56
   120
        }
jaroslav@56
   121
    }
jaroslav@56
   122
jaroslav@56
   123
    /**
jaroslav@56
   124
     * Creates an ObjectStreamField representing the given field with the
jaroslav@56
   125
     * specified unshared setting.  For compatibility with the behavior of
jaroslav@56
   126
     * earlier serialization implementations, a "showType" parameter is
jaroslav@56
   127
     * necessary to govern whether or not a getType() call on this
jaroslav@56
   128
     * ObjectStreamField (if non-primitive) will return Object.class (as
jaroslav@56
   129
     * opposed to a more specific reference type).
jaroslav@56
   130
     */
jaroslav@56
   131
    ObjectStreamField(Field field, boolean unshared, boolean showType) {
jaroslav@56
   132
        this.field = field;
jaroslav@56
   133
        this.unshared = unshared;
jaroslav@56
   134
        name = field.getName();
jaroslav@56
   135
        Class<?> ftype = field.getType();
jaroslav@56
   136
        type = (showType || ftype.isPrimitive()) ? ftype : Object.class;
jaroslav@56
   137
        signature = getClassSignature(ftype).intern();
jaroslav@56
   138
    }
jaroslav@56
   139
jaroslav@56
   140
    /**
jaroslav@56
   141
     * Get the name of this field.
jaroslav@56
   142
     *
jaroslav@56
   143
     * @return  a <code>String</code> representing the name of the serializable
jaroslav@56
   144
     *          field
jaroslav@56
   145
     */
jaroslav@56
   146
    public String getName() {
jaroslav@56
   147
        return name;
jaroslav@56
   148
    }
jaroslav@56
   149
jaroslav@56
   150
    /**
jaroslav@56
   151
     * Get the type of the field.  If the type is non-primitive and this
jaroslav@56
   152
     * <code>ObjectStreamField</code> was obtained from a deserialized {@link
jaroslav@56
   153
     * ObjectStreamClass} instance, then <code>Object.class</code> is returned.
jaroslav@56
   154
     * Otherwise, the <code>Class</code> object for the type of the field is
jaroslav@56
   155
     * returned.
jaroslav@56
   156
     *
jaroslav@56
   157
     * @return  a <code>Class</code> object representing the type of the
jaroslav@56
   158
     *          serializable field
jaroslav@56
   159
     */
jaroslav@56
   160
    public Class<?> getType() {
jaroslav@56
   161
        return type;
jaroslav@56
   162
    }
jaroslav@56
   163
jaroslav@56
   164
    /**
jaroslav@56
   165
     * Returns character encoding of field type.  The encoding is as follows:
jaroslav@56
   166
     * <blockquote><pre>
jaroslav@56
   167
     * B            byte
jaroslav@56
   168
     * C            char
jaroslav@56
   169
     * D            double
jaroslav@56
   170
     * F            float
jaroslav@56
   171
     * I            int
jaroslav@56
   172
     * J            long
jaroslav@56
   173
     * L            class or interface
jaroslav@56
   174
     * S            short
jaroslav@56
   175
     * Z            boolean
jaroslav@56
   176
     * [            array
jaroslav@56
   177
     * </pre></blockquote>
jaroslav@56
   178
     *
jaroslav@56
   179
     * @return  the typecode of the serializable field
jaroslav@56
   180
     */
jaroslav@56
   181
    // REMIND: deprecate?
jaroslav@56
   182
    public char getTypeCode() {
jaroslav@56
   183
        return signature.charAt(0);
jaroslav@56
   184
    }
jaroslav@56
   185
jaroslav@56
   186
    /**
jaroslav@56
   187
     * Return the JVM type signature.
jaroslav@56
   188
     *
jaroslav@56
   189
     * @return  null if this field has a primitive type.
jaroslav@56
   190
     */
jaroslav@56
   191
    // REMIND: deprecate?
jaroslav@56
   192
    public String getTypeString() {
jaroslav@56
   193
        return isPrimitive() ? null : signature;
jaroslav@56
   194
    }
jaroslav@56
   195
jaroslav@56
   196
    /**
jaroslav@56
   197
     * Offset of field within instance data.
jaroslav@56
   198
     *
jaroslav@56
   199
     * @return  the offset of this field
jaroslav@56
   200
     * @see #setOffset
jaroslav@56
   201
     */
jaroslav@56
   202
    // REMIND: deprecate?
jaroslav@56
   203
    public int getOffset() {
jaroslav@56
   204
        return offset;
jaroslav@56
   205
    }
jaroslav@56
   206
jaroslav@56
   207
    /**
jaroslav@56
   208
     * Offset within instance data.
jaroslav@56
   209
     *
jaroslav@56
   210
     * @param   offset the offset of the field
jaroslav@56
   211
     * @see #getOffset
jaroslav@56
   212
     */
jaroslav@56
   213
    // REMIND: deprecate?
jaroslav@56
   214
    protected void setOffset(int offset) {
jaroslav@56
   215
        this.offset = offset;
jaroslav@56
   216
    }
jaroslav@56
   217
jaroslav@56
   218
    /**
jaroslav@56
   219
     * Return true if this field has a primitive type.
jaroslav@56
   220
     *
jaroslav@56
   221
     * @return  true if and only if this field corresponds to a primitive type
jaroslav@56
   222
     */
jaroslav@56
   223
    // REMIND: deprecate?
jaroslav@56
   224
    public boolean isPrimitive() {
jaroslav@56
   225
        char tcode = signature.charAt(0);
jaroslav@56
   226
        return ((tcode != 'L') && (tcode != '['));
jaroslav@56
   227
    }
jaroslav@56
   228
jaroslav@56
   229
    /**
jaroslav@56
   230
     * Returns boolean value indicating whether or not the serializable field
jaroslav@56
   231
     * represented by this ObjectStreamField instance is unshared.
jaroslav@56
   232
     *
jaroslav@56
   233
     * @since 1.4
jaroslav@56
   234
     */
jaroslav@56
   235
    public boolean isUnshared() {
jaroslav@56
   236
        return unshared;
jaroslav@56
   237
    }
jaroslav@56
   238
jaroslav@56
   239
    /**
jaroslav@56
   240
     * Compare this field with another <code>ObjectStreamField</code>.  Return
jaroslav@56
   241
     * -1 if this is smaller, 0 if equal, 1 if greater.  Types that are
jaroslav@56
   242
     * primitives are "smaller" than object types.  If equal, the field names
jaroslav@56
   243
     * are compared.
jaroslav@56
   244
     */
jaroslav@56
   245
    // REMIND: deprecate?
jaroslav@56
   246
    public int compareTo(Object obj) {
jaroslav@56
   247
        ObjectStreamField other = (ObjectStreamField) obj;
jaroslav@56
   248
        boolean isPrim = isPrimitive();
jaroslav@56
   249
        if (isPrim != other.isPrimitive()) {
jaroslav@56
   250
            return isPrim ? -1 : 1;
jaroslav@56
   251
        }
jaroslav@56
   252
        return name.compareTo(other.name);
jaroslav@56
   253
    }
jaroslav@56
   254
jaroslav@56
   255
    /**
jaroslav@56
   256
     * Return a string that describes this field.
jaroslav@56
   257
     */
jaroslav@56
   258
    public String toString() {
jaroslav@56
   259
        return signature + ' ' + name;
jaroslav@56
   260
    }
jaroslav@56
   261
jaroslav@56
   262
    /**
jaroslav@56
   263
     * Returns field represented by this ObjectStreamField, or null if
jaroslav@56
   264
     * ObjectStreamField is not associated with an actual field.
jaroslav@56
   265
     */
jaroslav@56
   266
    Field getField() {
jaroslav@56
   267
        return field;
jaroslav@56
   268
    }
jaroslav@56
   269
jaroslav@56
   270
    /**
jaroslav@56
   271
     * Returns JVM type signature of field (similar to getTypeString, except
jaroslav@56
   272
     * that signature strings are returned for primitive fields as well).
jaroslav@56
   273
     */
jaroslav@56
   274
    String getSignature() {
jaroslav@56
   275
        return signature;
jaroslav@56
   276
    }
jaroslav@56
   277
jaroslav@56
   278
    /**
jaroslav@56
   279
     * Returns JVM type signature for given class.
jaroslav@56
   280
     */
jaroslav@56
   281
    private static String getClassSignature(Class<?> cl) {
jaroslav@56
   282
        StringBuilder sbuf = new StringBuilder();
jaroslav@56
   283
        while (cl.isArray()) {
jaroslav@56
   284
            sbuf.append('[');
jaroslav@56
   285
            cl = cl.getComponentType();
jaroslav@56
   286
        }
jaroslav@56
   287
        if (cl.isPrimitive()) {
jaroslav@56
   288
            if (cl == Integer.TYPE) {
jaroslav@56
   289
                sbuf.append('I');
jaroslav@56
   290
            } else if (cl == Byte.TYPE) {
jaroslav@56
   291
                sbuf.append('B');
jaroslav@56
   292
            } else if (cl == Long.TYPE) {
jaroslav@56
   293
                sbuf.append('J');
jaroslav@56
   294
            } else if (cl == Float.TYPE) {
jaroslav@56
   295
                sbuf.append('F');
jaroslav@56
   296
            } else if (cl == Double.TYPE) {
jaroslav@56
   297
                sbuf.append('D');
jaroslav@56
   298
            } else if (cl == Short.TYPE) {
jaroslav@56
   299
                sbuf.append('S');
jaroslav@56
   300
            } else if (cl == Character.TYPE) {
jaroslav@56
   301
                sbuf.append('C');
jaroslav@56
   302
            } else if (cl == Boolean.TYPE) {
jaroslav@56
   303
                sbuf.append('Z');
jaroslav@56
   304
            } else if (cl == Void.TYPE) {
jaroslav@56
   305
                sbuf.append('V');
jaroslav@56
   306
            } else {
jaroslav@56
   307
                throw new InternalError();
jaroslav@56
   308
            }
jaroslav@56
   309
        } else {
jaroslav@56
   310
            sbuf.append('L' + cl.getName().replace('.', '/') + ';');
jaroslav@56
   311
        }
jaroslav@56
   312
        return sbuf.toString();
jaroslav@56
   313
    }
jaroslav@56
   314
}