< prev index next >

src/java.base/share/classes/java/io/ObjectStreamField.java

Print this page




  74      * field is non-primitive, object values are serialized and deserialized as
  75      * if they had been written and read by calls to writeObject and
  76      * readObject.  If unshared is true, values of the represented field are
  77      * serialized and deserialized as if they had been written and read by
  78      * calls to writeUnshared and readUnshared.
  79      *
  80      * @param   name field name
  81      * @param   type field type
  82      * @param   unshared if false, write/read field values in the same manner
  83      *          as writeObject/readObject; if true, write/read in the same
  84      *          manner as writeUnshared/readUnshared
  85      * @since   1.4
  86      */
  87     public ObjectStreamField(String name, Class<?> type, boolean unshared) {
  88         if (name == null) {
  89             throw new NullPointerException();
  90         }
  91         this.name = name;
  92         this.type = type;
  93         this.unshared = unshared;
  94         signature = getClassSignature(type).intern();
  95         field = null;
  96     }
  97 
  98     /**
  99      * Creates an ObjectStreamField representing a field with the given name,
 100      * signature and unshared setting.
 101      */
 102     ObjectStreamField(String name, String signature, boolean unshared) {
 103         if (name == null) {
 104             throw new NullPointerException();
 105         }
 106         this.name = name;
 107         this.signature = signature.intern();
 108         this.unshared = unshared;
 109         field = null;
 110 
 111         switch (signature.charAt(0)) {
 112             case 'Z': type = Boolean.TYPE; break;
 113             case 'B': type = Byte.TYPE; break;
 114             case 'C': type = Character.TYPE; break;
 115             case 'S': type = Short.TYPE; break;
 116             case 'I': type = Integer.TYPE; break;
 117             case 'J': type = Long.TYPE; break;
 118             case 'F': type = Float.TYPE; break;
 119             case 'D': type = Double.TYPE; break;
 120             case 'L':
 121             case '[': type = Object.class; break;
 122             default: throw new IllegalArgumentException("illegal signature");
 123         }
 124     }
 125 
 126     /**
 127      * Returns JVM type signature for given primitive.
 128      */
 129     private static String getPrimitiveSignature(Class<?> cl) {
 130         if (cl == Integer.TYPE)
 131             return "I";
 132         else if (cl == Byte.TYPE)
 133             return "B";
 134         else if (cl == Long.TYPE)
 135             return "J";
 136         else if (cl == Float.TYPE)
 137             return "F";
 138         else if (cl == Double.TYPE)
 139             return "D";
 140         else if (cl == Short.TYPE)
 141             return "S";
 142         else if (cl == Character.TYPE)
 143             return "C";
 144         else if (cl == Boolean.TYPE)
 145             return "Z";
 146         else if (cl == Void.TYPE)
 147             return "V";
 148         else
 149             throw new InternalError();
 150     }
 151 
 152     /**
 153      * Returns JVM type signature for given class.
 154      */
 155     static String getClassSignature(Class<?> cl) {
 156         if (cl.isPrimitive()) {
 157             return getPrimitiveSignature(cl);
 158         } else {
 159             return appendClassSignature(new StringBuilder(), cl).toString();
 160         }
 161     }
 162 
 163     static StringBuilder appendClassSignature(StringBuilder sbuf, Class<?> cl) {
 164         while (cl.isArray()) {
 165             sbuf.append('[');
 166             cl = cl.getComponentType();
 167         }
 168 
 169         if (cl.isPrimitive()) {
 170             sbuf.append(getPrimitiveSignature(cl));
 171         } else {
 172             sbuf.append('L').append(cl.getName().replace('.', '/')).append(';');
 173         }
 174 
 175         return sbuf;
 176     }
 177 
 178     /**
 179      * Creates an ObjectStreamField representing the given field with the
 180      * specified unshared setting.  For compatibility with the behavior of
 181      * earlier serialization implementations, a "showType" parameter is
 182      * necessary to govern whether or not a getType() call on this
 183      * ObjectStreamField (if non-primitive) will return Object.class (as
 184      * opposed to a more specific reference type).
 185      */
 186     ObjectStreamField(Field field, boolean unshared, boolean showType) {
 187         this.field = field;
 188         this.unshared = unshared;
 189         name = field.getName();
 190         Class<?> ftype = field.getType();
 191         type = (showType || ftype.isPrimitive()) ? ftype : Object.class;
 192         signature = getClassSignature(ftype).intern();





 193     }
 194 
 195     /**
 196      * Get the name of this field.
 197      *
 198      * @return  a <code>String</code> representing the name of the serializable
 199      *          field
 200      */
 201     public String getName() {
 202         return name;
 203     }
 204 
 205     /**
 206      * Get the type of the field.  If the type is non-primitive and this
 207      * <code>ObjectStreamField</code> was obtained from a deserialized {@link
 208      * ObjectStreamClass} instance, then <code>Object.class</code> is returned.
 209      * Otherwise, the <code>Class</code> object for the type of the field is
 210      * returned.
 211      *
 212      * @return  a <code>Class</code> object representing the type of the


 225 
 226     /**
 227      * Returns character encoding of field type.  The encoding is as follows:
 228      * <blockquote><pre>
 229      * B            byte
 230      * C            char
 231      * D            double
 232      * F            float
 233      * I            int
 234      * J            long
 235      * L            class or interface
 236      * S            short
 237      * Z            boolean
 238      * [            array
 239      * </pre></blockquote>
 240      *
 241      * @return  the typecode of the serializable field
 242      */
 243     // REMIND: deprecate?
 244     public char getTypeCode() {
 245         return signature.charAt(0);
 246     }
 247 
 248     /**
 249      * Return the JVM type signature.
 250      *
 251      * @return  null if this field has a primitive type.
 252      */
 253     // REMIND: deprecate?
 254     public String getTypeString() {
 255         return isPrimitive() ? null : signature;
 256     }
 257 
 258     /**
 259      * Offset of field within instance data.
 260      *
 261      * @return  the offset of this field
 262      * @see #setOffset
 263      */
 264     // REMIND: deprecate?
 265     public int getOffset() {
 266         return offset;
 267     }
 268 
 269     /**
 270      * Offset within instance data.
 271      *
 272      * @param   offset the offset of the field
 273      * @see #getOffset
 274      */
 275     // REMIND: deprecate?
 276     protected void setOffset(int offset) {
 277         this.offset = offset;
 278     }
 279 
 280     /**
 281      * Return true if this field has a primitive type.
 282      *
 283      * @return  true if and only if this field corresponds to a primitive type
 284      */
 285     // REMIND: deprecate?
 286     public boolean isPrimitive() {
 287         char tcode = signature.charAt(0);
 288         return ((tcode != 'L') && (tcode != '['));
 289     }
 290 
 291     /**
 292      * Returns boolean value indicating whether or not the serializable field
 293      * represented by this ObjectStreamField instance is unshared.
 294      *
 295      * @return {@code true} if this field is unshared
 296      *
 297      * @since 1.4
 298      */
 299     public boolean isUnshared() {
 300         return unshared;
 301     }
 302 
 303     /**
 304      * Compare this field with another <code>ObjectStreamField</code>.  Return
 305      * -1 if this is smaller, 0 if equal, 1 if greater.  Types that are
 306      * primitives are "smaller" than object types.  If equal, the field names
 307      * are compared.
 308      */
 309     // REMIND: deprecate?
 310     public int compareTo(Object obj) {
 311         ObjectStreamField other = (ObjectStreamField) obj;
 312         boolean isPrim = isPrimitive();
 313         if (isPrim != other.isPrimitive()) {
 314             return isPrim ? -1 : 1;
 315         }
 316         return name.compareTo(other.name);
 317     }
 318 
 319     /**
 320      * Return a string that describes this field.
 321      */
 322     public String toString() {
 323         return signature + ' ' + name;
 324     }
 325 
 326     /**
 327      * Returns field represented by this ObjectStreamField, or null if
 328      * ObjectStreamField is not associated with an actual field.
 329      */
 330     Field getField() {
 331         return field;
 332     }
 333 
 334     /**
 335      * Returns JVM type signature of field (similar to getTypeString, except
 336      * that signature strings are returned for primitive fields as well).
 337      */
 338     String getSignature() {
 339         return signature;
 340     }
 341 }


  74      * field is non-primitive, object values are serialized and deserialized as
  75      * if they had been written and read by calls to writeObject and
  76      * readObject.  If unshared is true, values of the represented field are
  77      * serialized and deserialized as if they had been written and read by
  78      * calls to writeUnshared and readUnshared.
  79      *
  80      * @param   name field name
  81      * @param   type field type
  82      * @param   unshared if false, write/read field values in the same manner
  83      *          as writeObject/readObject; if true, write/read in the same
  84      *          manner as writeUnshared/readUnshared
  85      * @since   1.4
  86      */
  87     public ObjectStreamField(String name, Class<?> type, boolean unshared) {
  88         if (name == null) {
  89             throw new NullPointerException();
  90         }
  91         this.name = name;
  92         this.type = type;
  93         this.unshared = unshared;
  94         signature = null; // will be lazily obtained from type
  95         field = null;
  96     }
  97 
  98     /**
  99      * Creates an ObjectStreamField representing a field with the given name,
 100      * signature and unshared setting.
 101      */
 102     ObjectStreamField(String name, String signature, boolean unshared) {
 103         if (name == null) {
 104             throw new NullPointerException();
 105         }
 106         this.name = name;
 107         this.signature = signature.intern();
 108         this.unshared = unshared;
 109         field = null;
 110 
 111         switch (signature.charAt(0)) {
 112             case 'Z': type = Boolean.TYPE; break;
 113             case 'B': type = Byte.TYPE; break;
 114             case 'C': type = Character.TYPE; break;
 115             case 'S': type = Short.TYPE; break;
 116             case 'I': type = Integer.TYPE; break;
 117             case 'J': type = Long.TYPE; break;
 118             case 'F': type = Float.TYPE; break;
 119             case 'D': type = Double.TYPE; break;
 120             case 'L':
 121             case '[': type = Object.class; break;
 122             default: throw new IllegalArgumentException("illegal signature");
 123         }
 124     }
 125 
 126     /**




















































 127      * Creates an ObjectStreamField representing the given field with the
 128      * specified unshared setting.  For compatibility with the behavior of
 129      * earlier serialization implementations, a "showType" parameter is
 130      * necessary to govern whether or not a getType() call on this
 131      * ObjectStreamField (if non-primitive) will return Object.class (as
 132      * opposed to a more specific reference type).
 133      */
 134     ObjectStreamField(Field field, boolean unshared, boolean showType) {
 135         this.field = field;
 136         this.unshared = unshared;
 137         name = field.getName();
 138         Class<?> ftype = field.getType();
 139         if (showType || ftype.isPrimitive()) {
 140             type = ftype;
 141             signature = null; // will be lazily obtained from type
 142         } else {
 143             type = Object.class;
 144             signature = ftype.getJvmTypeSignature();
 145         }
 146     }
 147 
 148     /**
 149      * Get the name of this field.
 150      *
 151      * @return  a <code>String</code> representing the name of the serializable
 152      *          field
 153      */
 154     public String getName() {
 155         return name;
 156     }
 157 
 158     /**
 159      * Get the type of the field.  If the type is non-primitive and this
 160      * <code>ObjectStreamField</code> was obtained from a deserialized {@link
 161      * ObjectStreamClass} instance, then <code>Object.class</code> is returned.
 162      * Otherwise, the <code>Class</code> object for the type of the field is
 163      * returned.
 164      *
 165      * @return  a <code>Class</code> object representing the type of the


 178 
 179     /**
 180      * Returns character encoding of field type.  The encoding is as follows:
 181      * <blockquote><pre>
 182      * B            byte
 183      * C            char
 184      * D            double
 185      * F            float
 186      * I            int
 187      * J            long
 188      * L            class or interface
 189      * S            short
 190      * Z            boolean
 191      * [            array
 192      * </pre></blockquote>
 193      *
 194      * @return  the typecode of the serializable field
 195      */
 196     // REMIND: deprecate?
 197     public char getTypeCode() {
 198         return getSignature().charAt(0);
 199     }
 200 
 201     /**
 202      * Return the JVM type signature.
 203      *
 204      * @return  null if this field has a primitive type.
 205      */
 206     // REMIND: deprecate?
 207     public String getTypeString() {
 208         return isPrimitive() ? null : getSignature();
 209     }
 210 
 211     /**
 212      * Offset of field within instance data.
 213      *
 214      * @return  the offset of this field
 215      * @see #setOffset
 216      */
 217     // REMIND: deprecate?
 218     public int getOffset() {
 219         return offset;
 220     }
 221 
 222     /**
 223      * Offset within instance data.
 224      *
 225      * @param   offset the offset of the field
 226      * @see #getOffset
 227      */
 228     // REMIND: deprecate?
 229     protected void setOffset(int offset) {
 230         this.offset = offset;
 231     }
 232 
 233     /**
 234      * Return true if this field has a primitive type.
 235      *
 236      * @return  true if and only if this field corresponds to a primitive type
 237      */
 238     // REMIND: deprecate?
 239     public boolean isPrimitive() {
 240         char tcode = getSignature().charAt(0);
 241         return ((tcode != 'L') && (tcode != '['));
 242     }
 243 
 244     /**
 245      * Returns boolean value indicating whether or not the serializable field
 246      * represented by this ObjectStreamField instance is unshared.
 247      *
 248      * @return {@code true} if this field is unshared
 249      *
 250      * @since 1.4
 251      */
 252     public boolean isUnshared() {
 253         return unshared;
 254     }
 255 
 256     /**
 257      * Compare this field with another <code>ObjectStreamField</code>.  Return
 258      * -1 if this is smaller, 0 if equal, 1 if greater.  Types that are
 259      * primitives are "smaller" than object types.  If equal, the field names
 260      * are compared.
 261      */
 262     // REMIND: deprecate?
 263     public int compareTo(Object obj) {
 264         ObjectStreamField other = (ObjectStreamField) obj;
 265         boolean isPrim = isPrimitive();
 266         if (isPrim != other.isPrimitive()) {
 267             return isPrim ? -1 : 1;
 268         }
 269         return name.compareTo(other.name);
 270     }
 271 
 272     /**
 273      * Return a string that describes this field.
 274      */
 275     public String toString() {
 276         return getSignature() + ' ' + name;
 277     }
 278 
 279     /**
 280      * Returns field represented by this ObjectStreamField, or null if
 281      * ObjectStreamField is not associated with an actual field.
 282      */
 283     Field getField() {
 284         return field;
 285     }
 286 
 287     /**
 288      * Returns JVM type signature of field (similar to getTypeString, except
 289      * that signature strings are returned for primitive fields as well).
 290      */
 291     String getSignature() {
 292         return signature == null ? type.getJvmTypeSignature() : signature;
 293     }
 294 }
< prev index next >