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;
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 type = (showType || ftype.isPrimitive()) ? ftype : Object.class;
140 signature = getClassSignature(ftype).intern();
141 }
142
143 /**
144 * Get the name of this field.
145 *
146 * @return a <code>String</code> representing the name of the serializable
147 * field
148 */
149 public String getName() {
150 return name;
151 }
152
153 /**
154 * Get the type of the field. If the type is non-primitive and this
155 * <code>ObjectStreamField</code> was obtained from a deserialized {@link
156 * ObjectStreamClass} instance, then <code>Object.class</code> is returned.
157 * Otherwise, the <code>Class</code> object for the type of the field is
158 * returned.
159 *
160 * @return a <code>Class</code> object representing the type of the
268 * Return a string that describes this field.
269 */
270 public String toString() {
271 return signature + ' ' + name;
272 }
273
274 /**
275 * Returns field represented by this ObjectStreamField, or null if
276 * ObjectStreamField is not associated with an actual field.
277 */
278 Field getField() {
279 return field;
280 }
281
282 /**
283 * Returns JVM type signature of field (similar to getTypeString, except
284 * that signature strings are returned for primitive fields as well).
285 */
286 String getSignature() {
287 return signature;
288 }
289
290 /**
291 * Returns JVM type signature for given class.
292 */
293 private static String getClassSignature(Class<?> cl) {
294 StringBuilder sbuf = new StringBuilder();
295 while (cl.isArray()) {
296 sbuf.append('[');
297 cl = cl.getComponentType();
298 }
299 if (cl.isPrimitive()) {
300 if (cl == Integer.TYPE) {
301 sbuf.append('I');
302 } else if (cl == Byte.TYPE) {
303 sbuf.append('B');
304 } else if (cl == Long.TYPE) {
305 sbuf.append('J');
306 } else if (cl == Float.TYPE) {
307 sbuf.append('F');
308 } else if (cl == Double.TYPE) {
309 sbuf.append('D');
310 } else if (cl == Short.TYPE) {
311 sbuf.append('S');
312 } else if (cl == Character.TYPE) {
313 sbuf.append('C');
314 } else if (cl == Boolean.TYPE) {
315 sbuf.append('Z');
316 } else if (cl == Void.TYPE) {
317 sbuf.append('V');
318 } else {
319 throw new InternalError();
320 }
321 } else {
322 sbuf.append('L' + cl.getName().replace('.', '/') + ';');
323 }
324 return sbuf.toString();
325 }
326 }
|
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 = ObjectStreamClass.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;
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 type = (showType || ftype.isPrimitive()) ? ftype : Object.class;
140 signature = ObjectStreamClass.getClassSignature(ftype).intern();
141 }
142
143 /**
144 * Get the name of this field.
145 *
146 * @return a <code>String</code> representing the name of the serializable
147 * field
148 */
149 public String getName() {
150 return name;
151 }
152
153 /**
154 * Get the type of the field. If the type is non-primitive and this
155 * <code>ObjectStreamField</code> was obtained from a deserialized {@link
156 * ObjectStreamClass} instance, then <code>Object.class</code> is returned.
157 * Otherwise, the <code>Class</code> object for the type of the field is
158 * returned.
159 *
160 * @return a <code>Class</code> object representing the type of the
268 * Return a string that describes this field.
269 */
270 public String toString() {
271 return signature + ' ' + name;
272 }
273
274 /**
275 * Returns field represented by this ObjectStreamField, or null if
276 * ObjectStreamField is not associated with an actual field.
277 */
278 Field getField() {
279 return field;
280 }
281
282 /**
283 * Returns JVM type signature of field (similar to getTypeString, except
284 * that signature strings are returned for primitive fields as well).
285 */
286 String getSignature() {
287 return signature;
288 }
289 }
|