37 * @since 1.2
38 */
39 public class ObjectStreamField
40 implements Comparable<Object>
41 {
42
43 /** field name */
44 private final String name;
45 /** canonical JVM signature of field type, if given */
46 private final String signature;
47 /** field type (Object.class if unknown non-primitive type) */
48 private final Class<?> type;
49 /** lazily constructed signature for the type, if no explicit signature */
50 private String typeSignature;
51 /** whether or not to (de)serialize field values as unshared */
52 private final boolean unshared;
53 /** corresponding reflective field object, if any */
54 private final Field field;
55 /** offset of field value in enclosing field group */
56 private int offset;
57
58 /**
59 * Create a Serializable field with the specified type. This field should
60 * be documented with a {@code serialField} tag.
61 *
62 * @param name the name of the serializable field
63 * @param type the {@code Class} object of the serializable field
64 */
65 public ObjectStreamField(String name, Class<?> type) {
66 this(name, type, false);
67 }
68
69 /**
70 * Creates an ObjectStreamField representing a serializable field with the
71 * given name and type. If unshared is false, values of the represented
72 * field are serialized and deserialized in the default manner--if the
73 * field is non-primitive, object values are serialized and deserialized as
74 * if they had been written and read by calls to writeObject and
75 * readObject. If unshared is true, values of the represented field are
76 * serialized and deserialized as if they had been written and read by
77 * calls to writeUnshared and readUnshared.
78 *
79 * @param name field name
80 * @param type field type
81 * @param unshared if false, write/read field values in the same manner
82 * as writeObject/readObject; if true, write/read in the same
83 * manner as writeUnshared/readUnshared
84 * @since 1.4
85 */
86 public ObjectStreamField(String name, Class<?> type, boolean unshared) {
87 if (name == null) {
88 throw new NullPointerException();
89 }
90 this.name = name;
91 this.type = type;
92 this.unshared = unshared;
93 this.field = null;
94 this.signature = null;
95 }
96
97 /**
98 * Creates an ObjectStreamField representing a field with the given name,
99 * signature and unshared setting.
100 */
101 ObjectStreamField(String name, String signature, boolean unshared) {
102 if (name == null) {
103 throw new NullPointerException();
104 }
105 this.name = name;
106 this.signature = signature.intern();
107 this.unshared = unshared;
108 this.field = null;
109
110 type = switch (signature.charAt(0)) {
111 case 'Z' -> Boolean.TYPE;
112 case 'B' -> Byte.TYPE;
113 case 'C' -> Character.TYPE;
114 case 'S' -> Short.TYPE;
115 case 'I' -> Integer.TYPE;
116 case 'J' -> Long.TYPE;
117 case 'F' -> Float.TYPE;
118 case 'D' -> Double.TYPE;
119 case 'L', '[' -> Object.class;
120 default -> throw new IllegalArgumentException("illegal signature");
121 };
122 }
123
124 /**
125 * Creates an ObjectStreamField representing the given field with the
126 * specified unshared setting. For compatibility with the behavior of
127 * earlier serialization implementations, a "showType" parameter is
128 * necessary to govern whether or not a getType() call on this
129 * ObjectStreamField (if non-primitive) will return Object.class (as
130 * opposed to a more specific reference type).
131 */
132 ObjectStreamField(Field field, boolean unshared, boolean showType) {
133 this.field = field;
134 this.unshared = unshared;
135 name = field.getName();
136 Class<?> ftype = field.getType();
137 type = (showType || ftype.isPrimitive()) ? ftype : Object.class;
138 signature = ftype.descriptorString().intern();
139 }
140
141 /**
142 * Get the name of this field.
143 *
144 * @return a {@code String} representing the name of the serializable
145 * field
146 */
147 public String getName() {
148 return name;
149 }
150
151 /**
152 * Get the type of the field. If the type is non-primitive and this
153 * {@code ObjectStreamField} was obtained from a deserialized {@link
154 * ObjectStreamClass} instance, then {@code Object.class} is returned.
155 * Otherwise, the {@code Class} object for the type of the field is
156 * returned.
157 *
158 * @return a {@code Class} object representing the type of the
199 *
200 * @return the offset of this field
201 * @see #setOffset
202 */
203 // REMIND: deprecate?
204 public int getOffset() {
205 return offset;
206 }
207
208 /**
209 * Offset within instance data.
210 *
211 * @param offset the offset of the field
212 * @see #getOffset
213 */
214 // REMIND: deprecate?
215 protected void setOffset(int offset) {
216 this.offset = offset;
217 }
218
219 /**
220 * Return true if this field has a primitive type.
221 *
222 * @return true if and only if this field corresponds to a primitive type
223 */
224 // REMIND: deprecate?
225 public boolean isPrimitive() {
226 char tcode = getTypeCode();
227 return ((tcode != 'L') && (tcode != '['));
228 }
229
230 /**
231 * Returns boolean value indicating whether or not the serializable field
232 * represented by this ObjectStreamField instance is unshared.
233 *
234 * @return {@code true} if this field is unshared
235 *
236 * @since 1.4
237 */
238 public boolean isUnshared() {
|
37 * @since 1.2
38 */
39 public class ObjectStreamField
40 implements Comparable<Object>
41 {
42
43 /** field name */
44 private final String name;
45 /** canonical JVM signature of field type, if given */
46 private final String signature;
47 /** field type (Object.class if unknown non-primitive type) */
48 private final Class<?> type;
49 /** lazily constructed signature for the type, if no explicit signature */
50 private String typeSignature;
51 /** whether or not to (de)serialize field values as unshared */
52 private final boolean unshared;
53 /** corresponding reflective field object, if any */
54 private final Field field;
55 /** offset of field value in enclosing field group */
56 private int offset;
57 /** index of the field in the class, retain the declaration order of serializable fields */
58 private final int argIndex;
59
60 /**
61 * Create a Serializable field with the specified type. This field should
62 * be documented with a {@code serialField} tag.
63 *
64 * @param name the name of the serializable field
65 * @param type the {@code Class} object of the serializable field
66 */
67 public ObjectStreamField(String name, Class<?> type) {
68 this(name, type, false);
69 }
70
71 /**
72 * Creates an ObjectStreamField representing a serializable field with the
73 * given name and type. If unshared is false, values of the represented
74 * field are serialized and deserialized in the default manner--if the
75 * field is non-primitive, object values are serialized and deserialized as
76 * if they had been written and read by calls to writeObject and
77 * readObject. If unshared is true, values of the represented field are
78 * serialized and deserialized as if they had been written and read by
79 * calls to writeUnshared and readUnshared.
80 *
81 * @param name field name
82 * @param type field type
83 * @param unshared if false, write/read field values in the same manner
84 * as writeObject/readObject; if true, write/read in the same
85 * manner as writeUnshared/readUnshared
86 * @since 1.4
87 */
88 public ObjectStreamField(String name, Class<?> type, boolean unshared) {
89 this(name, type, unshared, -1);
90 }
91
92 /* package-private */
93 ObjectStreamField(String name, Class<?> type, boolean unshared, int argIndex) {
94 if (name == null) {
95 throw new NullPointerException();
96 }
97 this.name = name;
98 this.type = type;
99 this.unshared = unshared;
100 this.field = null;
101 this.signature = null;
102 this.argIndex = argIndex;
103 }
104
105 /**
106 * Creates an ObjectStreamField representing a field with the given name,
107 * signature and unshared setting.
108 */
109 ObjectStreamField(String name, String signature, boolean unshared, int argIndex) {
110 if (name == null) {
111 throw new NullPointerException();
112 }
113 this.name = name;
114 this.signature = signature.intern();
115 this.unshared = unshared;
116 this.field = null;
117 this.argIndex = argIndex;
118
119 type = switch (signature.charAt(0)) {
120 case 'Z' -> Boolean.TYPE;
121 case 'B' -> Byte.TYPE;
122 case 'C' -> Character.TYPE;
123 case 'S' -> Short.TYPE;
124 case 'I' -> Integer.TYPE;
125 case 'J' -> Long.TYPE;
126 case 'F' -> Float.TYPE;
127 case 'D' -> Double.TYPE;
128 case 'L', '[' -> Object.class;
129 default -> throw new IllegalArgumentException("illegal signature");
130 };
131 }
132
133 /**
134 * Creates an ObjectStreamField representing the given field with the
135 * specified unshared setting. For compatibility with the behavior of
136 * earlier serialization implementations, a "showType" parameter is
137 * necessary to govern whether or not a getType() call on this
138 * ObjectStreamField (if non-primitive) will return Object.class (as
139 * opposed to a more specific reference type).
140 */
141 ObjectStreamField(Field field, boolean unshared, boolean showType, int argIndex) {
142 this.field = field;
143 this.unshared = unshared;
144 name = field.getName();
145 Class<?> ftype = field.getType();
146 type = (showType || ftype.isPrimitive()) ? ftype : Object.class;
147 signature = ftype.descriptorString().intern();
148 this.argIndex = argIndex;
149 }
150
151 /**
152 * Get the name of this field.
153 *
154 * @return a {@code String} representing the name of the serializable
155 * field
156 */
157 public String getName() {
158 return name;
159 }
160
161 /**
162 * Get the type of the field. If the type is non-primitive and this
163 * {@code ObjectStreamField} was obtained from a deserialized {@link
164 * ObjectStreamClass} instance, then {@code Object.class} is returned.
165 * Otherwise, the {@code Class} object for the type of the field is
166 * returned.
167 *
168 * @return a {@code Class} object representing the type of the
209 *
210 * @return the offset of this field
211 * @see #setOffset
212 */
213 // REMIND: deprecate?
214 public int getOffset() {
215 return offset;
216 }
217
218 /**
219 * Offset within instance data.
220 *
221 * @param offset the offset of the field
222 * @see #getOffset
223 */
224 // REMIND: deprecate?
225 protected void setOffset(int offset) {
226 this.offset = offset;
227 }
228
229 /**
230 * {@return Index of the field in the sequence of Serializable fields}
231 */
232 int getArgIndex() {
233 return argIndex;
234 }
235
236 /**
237 * Return true if this field has a primitive type.
238 *
239 * @return true if and only if this field corresponds to a primitive type
240 */
241 // REMIND: deprecate?
242 public boolean isPrimitive() {
243 char tcode = getTypeCode();
244 return ((tcode != 'L') && (tcode != '['));
245 }
246
247 /**
248 * Returns boolean value indicating whether or not the serializable field
249 * represented by this ObjectStreamField instance is unshared.
250 *
251 * @return {@code true} if this field is unshared
252 *
253 * @since 1.4
254 */
255 public boolean isUnshared() {
|