Source Home >> Java Source 1.6.0 >> java.io.ObjectStreamField V 0.09
  • 001/*
  • 002 * @(#)ObjectStreamField.java 1.47 06/04/07
  • 003 *
  • 004 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
  • 005 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  • 006 */
  • 007
  • 008package java.io;
  • 009
  • 010import java.lang.reflect.Field;
  • 011
  • 012/**
  • 013 * A description of a Serializable field from a Serializable class. An array
  • 014 * of ObjectStreamFields is used to declare the Serializable fields of a class.
  • 015 *
  • 016 * @author Mike Warres
  • 017 * @author Roger Riggs
  • 018 * @version 1.47, 06/04/07
  • 019 * @see ObjectStreamClass
  • 020 * @since 1.2
  • 021 */
  • 022public class ObjectStreamField
  • 023 implements Comparable<Object>
  • 024{
  • 025
  • 026 /** field name */
  • 027 private final String name;
  • 028 /** canonical JVM signature of field type */
  • 029 private final String signature;
  • 030 /** field type (Object.class if unknown non-primitive type) */
  • 031 private final Class type;
  • 032 /** whether or not to (de)serialize field values as unshared */
  • 033 private final boolean unshared;
  • 034 /** corresponding reflective field object, if any */
  • 035 private final Field field;
  • 036 /** offset of field value in enclosing field group */
  • 037 private int offset = 0;
  • 038
  • 039 /**
  • 040 * Create a Serializable field with the specified type. This field should
  • 041 * be documented with a <code>serialField</code> tag.
  • 042 *
  • 043 * @param name the name of the serializable field
  • 044 * @param type the <code>Class</code> object of the serializable field
  • 045 */
  • 046 public ObjectStreamField(String name, Class<?> type) {
  • 047 this(name, type, false);
  • 048 }
  • 049
  • 050 /**
  • 051 * Creates an ObjectStreamField representing a serializable field with the
  • 052 * given name and type. If unshared is false, values of the represented
  • 053 * field are serialized and deserialized in the default manner--if the
  • 054 * field is non-primitive, object values are serialized and deserialized as
  • 055 * if they had been written and read by calls to writeObject and
  • 056 * readObject. If unshared is true, values of the represented field are
  • 057 * serialized and deserialized as if they had been written and read by
  • 058 * calls to writeUnshared and readUnshared.
  • 059 *
  • 060 * @param name field name
  • 061 * @param type field type
  • 062 * @param unshared if false, write/read field values in the same manner
  • 063 * as writeObject/readObject; if true, write/read in the same
  • 064 * manner as writeUnshared/readUnshared
  • 065 * @since 1.4
  • 066 */
  • 067 public ObjectStreamField(String name, Class<?> type, boolean unshared) {
  • 068 if (name == null) {
  • 069 throw new NullPointerException();
  • 070 }
  • 071 this.name = name;
  • 072 this.type = type;
  • 073 this.unshared = unshared;
  • 074 signature = ObjectStreamClass.getClassSignature(type).intern();
  • 075 field = null;
  • 076 }
  • 077
  • 078 /**
  • 079 * Creates an ObjectStreamField representing a field with the given name,
  • 080 * signature and unshared setting.
  • 081 */
  • 082 ObjectStreamField(String name, String signature, boolean unshared) {
  • 083 if (name == null) {
  • 084 throw new NullPointerException();
  • 085 }
  • 086 this.name = name;
  • 087 this.signature = signature.intern();
  • 088 this.unshared = unshared;
  • 089 field = null;
  • 090
  • 091 switch (signature.charAt(0)) {
  • 092 case 'Z': type = Boolean.TYPE; break;
  • 093 case 'B': type = Byte.TYPE; break;
  • 094 case 'C': type = Character.TYPE; break;
  • 095 case 'S': type = Short.TYPE; break;
  • 096 case 'I': type = Integer.TYPE; break;
  • 097 case 'J': type = Long.TYPE; break;
  • 098 case 'F': type = Float.TYPE; break;
  • 099 case 'D': type = Double.TYPE; break;
  • 100 case 'L':
  • 101 case '[': type = Object.class; break;
  • 102 default: throw new IllegalArgumentException("illegal signature");
  • 103 }
  • 104 }
  • 105
  • 106 /**
  • 107 * Creates an ObjectStreamField representing the given field with the
  • 108 * specified unshared setting. For compatibility with the behavior of
  • 109 * earlier serialization implementations, a "showType" parameter is
  • 110 * necessary to govern whether or not a getType() call on this
  • 111 * ObjectStreamField (if non-primitive) will return Object.class (as
  • 112 * opposed to a more specific reference type).
  • 113 */
  • 114 ObjectStreamField(Field field, boolean unshared, boolean showType) {
  • 115 this.field = field;
  • 116 this.unshared = unshared;
  • 117 name = field.getName();
  • 118 Class ftype = field.getType();
  • 119 type = (showType || ftype.isPrimitive()) ? ftype : Object.class;
  • 120 signature = ObjectStreamClass.getClassSignature(ftype).intern();
  • 121 }
  • 122
  • 123 /**
  • 124 * Get the name of this field.
  • 125 *
  • 126 * @return a <code>String</code> representing the name of the serializable
  • 127 * field
  • 128 */
  • 129 public String getName() {
  • 130 return name;
  • 131 }
  • 132
  • 133 /**
  • 134 * Get the type of the field. If the type is non-primitive and this
  • 135 * <code>ObjectStreamField</code> was obtained from a deserialized {@link
  • 136 * ObjectStreamClass} instance, then <code>Object.class</code> is returned.
  • 137 * Otherwise, the <code>Class</code> object for the type of the field is
  • 138 * returned.
  • 139 *
  • 140 * @return a <code>Class</code> object representing the type of the
  • 141 * serializable field
  • 142 */
  • 143 public Class<?> getType() {
  • 144 return type;
  • 145 }
  • 146
  • 147 /**
  • 148 * Returns character encoding of field type. The encoding is as follows:
  • 149 * <blockquote><pre>
  • 150 * B byte
  • 151 * C char
  • 152 * D double
  • 153 * F float
  • 154 * I int
  • 155 * J long
  • 156 * L class or interface
  • 157 * S short
  • 158 * Z boolean
  • 159 * [ array
  • 160 * </pre></blockquote>
  • 161 *
  • 162 * @return the typecode of the serializable field
  • 163 */
  • 164 // REMIND: deprecate?
  • 165 public char getTypeCode() {
  • 166 return signature.charAt(0);
  • 167 }
  • 168
  • 169 /**
  • 170 * Return the JVM type signature.
  • 171 *
  • 172 * @return null if this field has a primitive type.
  • 173 */
  • 174 // REMIND: deprecate?
  • 175 public String getTypeString() {
  • 176 return isPrimitive() ? null : signature;
  • 177 }
  • 178
  • 179 /**
  • 180 * Offset of field within instance data.
  • 181 *
  • 182 * @return the offset of this field
  • 183 * @see #setOffset
  • 184 */
  • 185 // REMIND: deprecate?
  • 186 public int getOffset() {
  • 187 return offset;
  • 188 }
  • 189
  • 190 /**
  • 191 * Offset within instance data.
  • 192 *
  • 193 * @param offset the offset of the field
  • 194 * @see #getOffset
  • 195 */
  • 196 // REMIND: deprecate?
  • 197 protected void setOffset(int offset) {
  • 198 this.offset = offset;
  • 199 }
  • 200
  • 201 /**
  • 202 * Return true if this field has a primitive type.
  • 203 *
  • 204 * @return true if and only if this field corresponds to a primitive type
  • 205 */
  • 206 // REMIND: deprecate?
  • 207 public boolean isPrimitive() {
  • 208 char tcode = signature.charAt(0);
  • 209 return ((tcode != 'L') && (tcode != '['));
  • 210 }
  • 211
  • 212 /**
  • 213 * Returns boolean value indicating whether or not the serializable field
  • 214 * represented by this ObjectStreamField instance is unshared.
  • 215 *
  • 216 * @since 1.4
  • 217 */
  • 218 public boolean isUnshared() {
  • 219 return unshared;
  • 220 }
  • 221
  • 222 /**
  • 223 * Compare this field with another <code>ObjectStreamField</code>. Return
  • 224 * -1 if this is smaller, 0 if equal, 1 if greater. Types that are
  • 225 * primitives are "smaller" than object types. If equal, the field names
  • 226 * are compared.
  • 227 */
  • 228 // REMIND: deprecate?
  • 229 public int compareTo(Object obj) {
  • 230 ObjectStreamField other = (ObjectStreamField) obj;
  • 231 boolean isPrim = isPrimitive();
  • 232 if (isPrim != other.isPrimitive()) {
  • 233 return isPrim ? -1 : 1;
  • 234 }
  • 235 return name.compareTo(other.name);
  • 236 }
  • 237
  • 238 /**
  • 239 * Return a string that describes this field.
  • 240 */
  • 241 public String toString() {
  • 242 return signature + ' ' + name;
  • 243 }
  • 244
  • 245 /**
  • 246 * Returns field represented by this ObjectStreamField, or null if
  • 247 * ObjectStreamField is not associated with an actual field.
  • 248 */
  • 249 Field getField() {
  • 250 return field;
  • 251 }
  • 252
  • 253 /**
  • 254 * Returns JVM type signature of field (similar to getTypeString, except
  • 255 * that signature strings are returned for primitive fields as well).
  • 256 */
  • 257 String getSignature() {
  • 258 return signature;
  • 259 }
  • 260}

文件:ObjectStreamField.java
包名:java.io
类名:ObjectStreamField
继承:
接口:[Comparable]