author | smarks |
Mon, 22 Apr 2013 10:55:14 +0100 | |
changeset 18237 | 54af2a0e06da |
parent 5506 | 202f599c92aa |
child 18238 | d1ed51b53296 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.io; |
|
27 |
||
28 |
import java.lang.reflect.Field; |
|
18237 | 29 |
import sun.reflect.Reflection; |
30 |
import sun.reflect.misc.ReflectUtil; |
|
2 | 31 |
|
32 |
/** |
|
33 |
* A description of a Serializable field from a Serializable class. An array |
|
34 |
* of ObjectStreamFields is used to declare the Serializable fields of a class. |
|
35 |
* |
|
36 |
* @author Mike Warres |
|
37 |
* @author Roger Riggs |
|
38 |
* @see ObjectStreamClass |
|
39 |
* @since 1.2 |
|
40 |
*/ |
|
41 |
public class ObjectStreamField |
|
42 |
implements Comparable<Object> |
|
43 |
{ |
|
44 |
||
45 |
/** field name */ |
|
46 |
private final String name; |
|
47 |
/** canonical JVM signature of field type */ |
|
48 |
private final String signature; |
|
49 |
/** field type (Object.class if unknown non-primitive type) */ |
|
4171
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
50 |
private final Class<?> type; |
2 | 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 = 0; |
|
57 |
||
58 |
/** |
|
59 |
* Create a Serializable field with the specified type. This field should |
|
60 |
* be documented with a <code>serialField</code> tag. |
|
61 |
* |
|
62 |
* @param name the name of the serializable field |
|
63 |
* @param type the <code>Class</code> 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; |
|
4171
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
93 |
signature = getClassSignature(type).intern(); |
2 | 94 |
field = 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 |
field = null; |
|
109 |
||
110 |
switch (signature.charAt(0)) { |
|
111 |
case 'Z': type = Boolean.TYPE; break; |
|
112 |
case 'B': type = Byte.TYPE; break; |
|
113 |
case 'C': type = Character.TYPE; break; |
|
114 |
case 'S': type = Short.TYPE; break; |
|
115 |
case 'I': type = Integer.TYPE; break; |
|
116 |
case 'J': type = Long.TYPE; break; |
|
117 |
case 'F': type = Float.TYPE; break; |
|
118 |
case 'D': type = Double.TYPE; break; |
|
119 |
case 'L': |
|
120 |
case '[': type = Object.class; break; |
|
121 |
default: throw new IllegalArgumentException("illegal signature"); |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* Creates an ObjectStreamField representing the given field with the |
|
127 |
* specified unshared setting. For compatibility with the behavior of |
|
128 |
* earlier serialization implementations, a "showType" parameter is |
|
129 |
* necessary to govern whether or not a getType() call on this |
|
130 |
* ObjectStreamField (if non-primitive) will return Object.class (as |
|
131 |
* opposed to a more specific reference type). |
|
132 |
*/ |
|
133 |
ObjectStreamField(Field field, boolean unshared, boolean showType) { |
|
134 |
this.field = field; |
|
135 |
this.unshared = unshared; |
|
136 |
name = field.getName(); |
|
4171
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
137 |
Class<?> ftype = field.getType(); |
2 | 138 |
type = (showType || ftype.isPrimitive()) ? ftype : Object.class; |
4171
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
139 |
signature = getClassSignature(ftype).intern(); |
2 | 140 |
} |
141 |
||
142 |
/** |
|
143 |
* Get the name of this field. |
|
144 |
* |
|
145 |
* @return a <code>String</code> representing the name of the serializable |
|
146 |
* field |
|
147 |
*/ |
|
148 |
public String getName() { |
|
149 |
return name; |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* Get the type of the field. If the type is non-primitive and this |
|
154 |
* <code>ObjectStreamField</code> was obtained from a deserialized {@link |
|
155 |
* ObjectStreamClass} instance, then <code>Object.class</code> is returned. |
|
156 |
* Otherwise, the <code>Class</code> object for the type of the field is |
|
157 |
* returned. |
|
158 |
* |
|
159 |
* @return a <code>Class</code> object representing the type of the |
|
160 |
* serializable field |
|
161 |
*/ |
|
162 |
public Class<?> getType() { |
|
18237 | 163 |
ClassLoader ccl = getCallerClassLoader(); |
164 |
if (ReflectUtil.needsPackageAccessCheck(ccl, type.getClassLoader())) { |
|
165 |
ReflectUtil.checkPackageAccess(type); |
|
166 |
} |
|
2 | 167 |
return type; |
168 |
} |
|
169 |
||
18237 | 170 |
// Returns the invoker's class loader. |
171 |
// This is package private because it is accessed from ObjectStreamClass. |
|
172 |
// NOTE: This must always be invoked when there is exactly one intervening |
|
173 |
// frame from the core libraries on the stack between this method's |
|
174 |
// invocation and the desired invoker. The frame count of 3 is determined |
|
175 |
// as follows: |
|
176 |
// |
|
177 |
// 0: Reflection.getCallerClass |
|
178 |
// 1: getCallerClassLoader() |
|
179 |
// 2: ObjectStreamField.getType() or ObjectStreamClass.forClass() |
|
180 |
// 3: the caller we want to check |
|
181 |
// |
|
182 |
// NOTE: copied from java.lang.ClassLoader and modified. |
|
183 |
static ClassLoader getCallerClassLoader() { |
|
184 |
Class caller = Reflection.getCallerClass(3); |
|
185 |
return caller.getClassLoader(); |
|
186 |
} |
|
187 |
||
2 | 188 |
/** |
189 |
* Returns character encoding of field type. The encoding is as follows: |
|
190 |
* <blockquote><pre> |
|
191 |
* B byte |
|
192 |
* C char |
|
193 |
* D double |
|
194 |
* F float |
|
195 |
* I int |
|
196 |
* J long |
|
197 |
* L class or interface |
|
198 |
* S short |
|
199 |
* Z boolean |
|
200 |
* [ array |
|
201 |
* </pre></blockquote> |
|
202 |
* |
|
203 |
* @return the typecode of the serializable field |
|
204 |
*/ |
|
205 |
// REMIND: deprecate? |
|
206 |
public char getTypeCode() { |
|
207 |
return signature.charAt(0); |
|
208 |
} |
|
209 |
||
210 |
/** |
|
211 |
* Return the JVM type signature. |
|
212 |
* |
|
213 |
* @return null if this field has a primitive type. |
|
214 |
*/ |
|
215 |
// REMIND: deprecate? |
|
216 |
public String getTypeString() { |
|
217 |
return isPrimitive() ? null : signature; |
|
218 |
} |
|
219 |
||
220 |
/** |
|
221 |
* Offset of field within instance data. |
|
222 |
* |
|
223 |
* @return the offset of this field |
|
224 |
* @see #setOffset |
|
225 |
*/ |
|
226 |
// REMIND: deprecate? |
|
227 |
public int getOffset() { |
|
228 |
return offset; |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* Offset within instance data. |
|
233 |
* |
|
234 |
* @param offset the offset of the field |
|
235 |
* @see #getOffset |
|
236 |
*/ |
|
237 |
// REMIND: deprecate? |
|
238 |
protected void setOffset(int offset) { |
|
239 |
this.offset = offset; |
|
240 |
} |
|
241 |
||
242 |
/** |
|
243 |
* Return true if this field has a primitive type. |
|
244 |
* |
|
245 |
* @return true if and only if this field corresponds to a primitive type |
|
246 |
*/ |
|
247 |
// REMIND: deprecate? |
|
248 |
public boolean isPrimitive() { |
|
249 |
char tcode = signature.charAt(0); |
|
250 |
return ((tcode != 'L') && (tcode != '[')); |
|
251 |
} |
|
252 |
||
253 |
/** |
|
254 |
* Returns boolean value indicating whether or not the serializable field |
|
255 |
* represented by this ObjectStreamField instance is unshared. |
|
256 |
* |
|
257 |
* @since 1.4 |
|
258 |
*/ |
|
259 |
public boolean isUnshared() { |
|
260 |
return unshared; |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* Compare this field with another <code>ObjectStreamField</code>. Return |
|
265 |
* -1 if this is smaller, 0 if equal, 1 if greater. Types that are |
|
266 |
* primitives are "smaller" than object types. If equal, the field names |
|
267 |
* are compared. |
|
268 |
*/ |
|
269 |
// REMIND: deprecate? |
|
270 |
public int compareTo(Object obj) { |
|
271 |
ObjectStreamField other = (ObjectStreamField) obj; |
|
272 |
boolean isPrim = isPrimitive(); |
|
273 |
if (isPrim != other.isPrimitive()) { |
|
274 |
return isPrim ? -1 : 1; |
|
275 |
} |
|
276 |
return name.compareTo(other.name); |
|
277 |
} |
|
278 |
||
279 |
/** |
|
280 |
* Return a string that describes this field. |
|
281 |
*/ |
|
282 |
public String toString() { |
|
283 |
return signature + ' ' + name; |
|
284 |
} |
|
285 |
||
286 |
/** |
|
287 |
* Returns field represented by this ObjectStreamField, or null if |
|
288 |
* ObjectStreamField is not associated with an actual field. |
|
289 |
*/ |
|
290 |
Field getField() { |
|
291 |
return field; |
|
292 |
} |
|
293 |
||
294 |
/** |
|
295 |
* Returns JVM type signature of field (similar to getTypeString, except |
|
296 |
* that signature strings are returned for primitive fields as well). |
|
297 |
*/ |
|
298 |
String getSignature() { |
|
299 |
return signature; |
|
300 |
} |
|
4171
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
301 |
|
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
302 |
/** |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
303 |
* Returns JVM type signature for given class. |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
304 |
*/ |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
305 |
private static String getClassSignature(Class<?> cl) { |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
306 |
StringBuilder sbuf = new StringBuilder(); |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
307 |
while (cl.isArray()) { |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
308 |
sbuf.append('['); |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
309 |
cl = cl.getComponentType(); |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
310 |
} |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
311 |
if (cl.isPrimitive()) { |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
312 |
if (cl == Integer.TYPE) { |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
313 |
sbuf.append('I'); |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
314 |
} else if (cl == Byte.TYPE) { |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
315 |
sbuf.append('B'); |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
316 |
} else if (cl == Long.TYPE) { |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
317 |
sbuf.append('J'); |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
318 |
} else if (cl == Float.TYPE) { |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
319 |
sbuf.append('F'); |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
320 |
} else if (cl == Double.TYPE) { |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
321 |
sbuf.append('D'); |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
322 |
} else if (cl == Short.TYPE) { |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
323 |
sbuf.append('S'); |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
324 |
} else if (cl == Character.TYPE) { |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
325 |
sbuf.append('C'); |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
326 |
} else if (cl == Boolean.TYPE) { |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
327 |
sbuf.append('Z'); |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
328 |
} else if (cl == Void.TYPE) { |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
329 |
sbuf.append('V'); |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
330 |
} else { |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
331 |
throw new InternalError(); |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
332 |
} |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
333 |
} else { |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
334 |
sbuf.append('L' + cl.getName().replace('.', '/') + ';'); |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
335 |
} |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
336 |
return sbuf.toString(); |
7edb512fdaa6
6895456: Eliminate dependency on java.io.ObjectStreamClass during boot
mchung
parents:
2
diff
changeset
|
337 |
} |
2 | 338 |
} |