KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > ObjectStreamField


1 /*
2  * @(#)ObjectStreamField.java 1.45 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.io;
9
10 import java.lang.reflect.Field JavaDoc;
11
12 /**
13  * A description of a Serializable field from a Serializable class. An array
14  * of ObjectStreamFields is used to declare the Serializable fields of a class.
15  *
16  * @author Mike Warres
17  * @author Roger Riggs
18  * @version 1.45, 04/05/05
19  * @see ObjectStreamClass
20  * @since 1.2
21  */

22 public class ObjectStreamField
23     implements Comparable JavaDoc<Object JavaDoc>
24 {
25
26     /** field name */
27     private final String JavaDoc name;
28     /** canonical JVM signature of field type */
29     private final String JavaDoc signature;
30     /** field type (Object.class if unknown non-primitive type) */
31     private final Class JavaDoc type;
32     /** whether or not to (de)serialize field values as unshared */
33     private final boolean unshared;
34     /** corresponding reflective field object, if any */
35     private final Field JavaDoc field;
36     /** offset of field value in enclosing field group */
37     private int offset = 0;
38
39     /**
40      * Create a Serializable field with the specified type. This field should
41      * be documented with a <code>serialField</code> tag.
42      *
43      * @param name the name of the serializable field
44      * @param type the <code>Class</code> object of the serializable field
45      */

46     public ObjectStreamField(String JavaDoc name, Class JavaDoc<?> type) {
47     this(name, type, false);
48     }
49     
50     /**
51      * Creates an ObjectStreamField representing a serializable field with the
52      * given name and type. If unshared is false, values of the represented
53      * field are serialized and deserialized in the default manner--if the
54      * field is non-primitive, object values are serialized and deserialized as
55      * if they had been written and read by calls to writeObject and
56      * readObject. If unshared is true, values of the represented field are
57      * serialized and deserialized as if they had been written and read by
58      * calls to writeUnshared and readUnshared.
59      *
60      * @param name field name
61      * @param type field type
62      * @param unshared if false, write/read field values in the same manner
63      * as writeObject/readObject; if true, write/read in the same
64      * manner as writeUnshared/readUnshared
65      */

66     public ObjectStreamField(String JavaDoc name, Class JavaDoc<?> type, boolean unshared) {
67     if (name == null) {
68         throw new NullPointerException JavaDoc();
69     }
70     this.name = name;
71     this.type = type;
72     this.unshared = unshared;
73     signature = ObjectStreamClass.getClassSignature(type).intern();
74     field = null;
75     }
76
77     /**
78      * Creates an ObjectStreamField representing a field with the given name,
79      * signature and unshared setting.
80      */

81     ObjectStreamField(String JavaDoc name, String JavaDoc signature, boolean unshared) {
82     if (name == null) {
83         throw new NullPointerException JavaDoc();
84     }
85     this.name = name;
86     this.signature = signature.intern();
87     this.unshared = unshared;
88     field = null;
89     
90     switch (signature.charAt(0)) {
91         case 'Z': type = Boolean.TYPE; break;
92         case 'B': type = Byte.TYPE; break;
93         case 'C': type = Character.TYPE; break;
94         case 'S': type = Short.TYPE; break;
95         case 'I': type = Integer.TYPE; break;
96         case 'J': type = Long.TYPE; break;
97         case 'F': type = Float.TYPE; break;
98         case 'D': type = Double.TYPE; break;
99         case 'L':
100         case '[': type = Object JavaDoc.class; break;
101         default: throw new IllegalArgumentException JavaDoc("illegal signature");
102     }
103     }
104     
105     /**
106      * Creates an ObjectStreamField representing the given field with the
107      * specified unshared setting. For compatibility with the behavior of
108      * earlier serialization implementations, a "showType" parameter is
109      * necessary to govern whether or not a getType() call on this
110      * ObjectStreamField (if non-primitive) will return Object.class (as
111      * opposed to a more specific reference type).
112      */

113     ObjectStreamField(Field JavaDoc field, boolean unshared, boolean showType) {
114     this.field = field;
115     this.unshared = unshared;
116     name = field.getName();
117     Class JavaDoc ftype = field.getType();
118     type = (showType || ftype.isPrimitive()) ? ftype : Object JavaDoc.class;
119     signature = ObjectStreamClass.getClassSignature(ftype).intern();
120     }
121
122     /**
123      * Get the name of this field.
124      *
125      * @return a <code>String</code> representing the name of the serializable
126      * field
127      */

128     public String JavaDoc getName() {
129     return name;
130     }
131
132     /**
133      * Get the type of the field. If the type is non-primitive and this
134      * <code>ObjectStreamField</code> was obtained from a deserialized {@link
135      * ObjectStreamClass} instance, then <code>Object.class</code> is returned.
136      * Otherwise, the <code>Class</code> object for the type of the field is
137      * returned.
138      *
139      * @return a <code>Class</code> object representing the type of the
140      * serializable field
141      */

142     public Class JavaDoc<?> getType() {
143     return type;
144     }
145
146     /**
147      * Returns character encoding of field type. The encoding is as follows:
148      * <blockquote><pre>
149      * B byte
150      * C char
151      * D double
152      * F float
153      * I int
154      * J long
155      * L class or interface
156      * S short
157      * Z boolean
158      * [ array
159      * </pre></blockquote>
160      *
161      * @return the typecode of the serializable field
162      */

163     // REMIND: deprecate?
164
public char getTypeCode() {
165     return signature.charAt(0);
166     }
167
168     /**
169      * Return the JVM type signature.
170      *
171      * @return null if this field has a primitive type.
172      */

173     // REMIND: deprecate?
174
public String JavaDoc getTypeString() {
175     return isPrimitive() ? null : signature;
176     }
177
178     /**
179      * Offset of field within instance data.
180      *
181      * @return the offset of this field
182      * @see #setOffset
183      */

184     // REMIND: deprecate?
185
public int getOffset() {
186     return offset;
187     }
188
189     /**
190      * Offset within instance data.
191      *
192      * @param offset the offset of the field
193      * @see #getOffset
194      */

195     // REMIND: deprecate?
196
protected void setOffset(int offset) {
197     this.offset = offset;
198     }
199
200     /**
201      * Return true if this field has a primitive type.
202      *
203      * @return true if and only if this field corresponds to a primitive type
204      */

205     // REMIND: deprecate?
206
public boolean isPrimitive() {
207     char tcode = signature.charAt(0);
208     return ((tcode != 'L') && (tcode != '['));
209     }
210     
211     /**
212      * Returns boolean value indicating whether or not the serializable field
213      * represented by this ObjectStreamField instance is unshared.
214      */

215     public boolean isUnshared() {
216     return unshared;
217     }
218
219     /**
220      * Compare this field with another <code>ObjectStreamField</code>. Return
221      * -1 if this is smaller, 0 if equal, 1 if greater. Types that are
222      * primitives are "smaller" than object types. If equal, the field names
223      * are compared.
224      */

225     // REMIND: deprecate?
226
public int compareTo(Object JavaDoc obj) {
227     ObjectStreamField JavaDoc other = (ObjectStreamField JavaDoc) obj;
228     boolean isPrim = isPrimitive();
229     if (isPrim != other.isPrimitive()) {
230         return isPrim ? -1 : 1;
231     }
232     return name.compareTo(other.name);
233     }
234
235     /**
236      * Return a string that describes this field.
237      */

238     public String JavaDoc toString() {
239     return signature + ' ' + name;
240     }
241     
242     /**
243      * Returns field represented by this ObjectStreamField, or null if
244      * ObjectStreamField is not associated with an actual field.
245      */

246     Field JavaDoc getField() {
247     return field;
248     }
249     
250     /**
251      * Returns JVM type signature of field (similar to getTypeString, except
252      * that signature strings are returned for primitive fields as well).
253      */

254     String JavaDoc getSignature() {
255     return signature;
256     }
257 }
258
Popular Tags