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