J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 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 |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 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 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package java.io; |
| 27 | |
| 28 | import java.lang.ref.Reference; |
| 29 | import java.lang.ref.ReferenceQueue; |
| 30 | import java.lang.ref.SoftReference; |
| 31 | import java.lang.ref.WeakReference; |
| 32 | import java.lang.reflect.Constructor; |
| 33 | import java.lang.reflect.Field; |
| 34 | import java.lang.reflect.InvocationTargetException; |
| 35 | import java.lang.reflect.Member; |
| 36 | import java.lang.reflect.Method; |
| 37 | import java.lang.reflect.Modifier; |
| 38 | import java.lang.reflect.Proxy; |
| 39 | import java.security.AccessController; |
| 40 | import java.security.MessageDigest; |
| 41 | import java.security.NoSuchAlgorithmException; |
| 42 | import java.security.PrivilegedAction; |
| 43 | import java.util.ArrayList; |
| 44 | import java.util.Arrays; |
| 45 | import java.util.Collections; |
| 46 | import java.util.Comparator; |
| 47 | import java.util.HashSet; |
| 48 | import java.util.Set; |
| 49 | import java.util.concurrent.ConcurrentHashMap; |
| 50 | import java.util.concurrent.ConcurrentMap; |
| 51 | import sun.misc.Unsafe; |
| 52 | import sun.reflect.ReflectionFactory; |
| 53 | |
| 54 | /** |
| 55 | * Serialization's descriptor for classes. It contains the name and |
| 56 | * serialVersionUID of the class. The ObjectStreamClass for a specific class |
| 57 | * loaded in this Java VM can be found/created using the lookup method. |
| 58 | * |
| 59 | * <p>The algorithm to compute the SerialVersionUID is described in |
| 60 | * <a href="../../../platform/serialization/spec/class.html#4100">Object |
| 61 | * Serialization Specification, Section 4.6, Stream Unique Identifiers</a>. |
| 62 | * |
| 63 | * @author Mike Warres |
| 64 | * @author Roger Riggs |
| 65 | * @see ObjectStreamField |
| 66 | * @see <a href="../../../platform/serialization/spec/class.html">Object Serialization Specification, Section 4, Class Descriptors</a> |
| 67 | * @since JDK1.1 |
| 68 | */ |
| 69 | public class ObjectStreamClass implements Serializable { |
| 70 | |
| 71 | /** serialPersistentFields value indicating no serializable fields */ |
| 72 | public static final ObjectStreamField[] NO_FIELDS = |
| 73 | new ObjectStreamField[0]; |
| 74 | |
| 75 | private static final long serialVersionUID = -6120832682080437368L; |
| 76 | private static final ObjectStreamField[] serialPersistentFields = |
| 77 | NO_FIELDS; |
| 78 | |
| 79 | /** reflection factory for obtaining serialization constructors */ |
| 80 | private static final ReflectionFactory reflFactory = (ReflectionFactory) |
| 81 | AccessController.doPrivileged( |
| 82 | new ReflectionFactory.GetReflectionFactoryAction()); |
| 83 | |
| 84 | private static class Caches { |
| 85 | /** cache mapping local classes -> descriptors */ |
| 86 | static final ConcurrentMap<WeakClassKey,Reference<?>> localDescs = |
| 87 | new ConcurrentHashMap<WeakClassKey,Reference<?>>(); |
| 88 | |
| 89 | /** cache mapping field group/local desc pairs -> field reflectors */ |
| 90 | static final ConcurrentMap<FieldReflectorKey,Reference<?>> reflectors = |
| 91 | new ConcurrentHashMap<FieldReflectorKey,Reference<?>>(); |
| 92 | |
| 93 | /** queue for WeakReferences to local classes */ |
| 94 | private static final ReferenceQueue<Class<?>> localDescsQueue = |
| 95 | new ReferenceQueue<Class<?>>(); |
| 96 | /** queue for WeakReferences to field reflectors keys */ |
| 97 | private static final ReferenceQueue<Class<?>> reflectorsQueue = |
| 98 | new ReferenceQueue<Class<?>>(); |
| 99 | } |
| 100 | |
| 101 | /** class associated with this descriptor (if any) */ |
| 102 | private Class cl; |
| 103 | /** name of class represented by this descriptor */ |
| 104 | private String name; |
| 105 | /** serialVersionUID of represented class (null if not computed yet) */ |
| 106 | private volatile Long suid; |
| 107 | |
| 108 | /** true if represents dynamic proxy class */ |
| 109 | private boolean isProxy; |
| 110 | /** true if represents enum type */ |
| 111 | private boolean isEnum; |
| 112 | /** true if represented class implements Serializable */ |
| 113 | private boolean serializable; |
| 114 | /** true if represented class implements Externalizable */ |
| 115 | private boolean externalizable; |
| 116 | /** true if desc has data written by class-defined writeObject method */ |
| 117 | private boolean hasWriteObjectData; |
| 118 | /** |
| 119 | * true if desc has externalizable data written in block data format; this |
| 120 | * must be true by default to accommodate ObjectInputStream subclasses which |
| 121 | * override readClassDescriptor() to return class descriptors obtained from |
| 122 | * ObjectStreamClass.lookup() (see 4461737) |
| 123 | */ |
| 124 | private boolean hasBlockExternalData = true; |
| 125 | |
| 126 | /** exception (if any) thrown while attempting to resolve class */ |
| 127 | private ClassNotFoundException resolveEx; |
| 128 | /** exception (if any) to throw if non-enum deserialization attempted */ |
| 129 | private InvalidClassException deserializeEx; |
| 130 | /** exception (if any) to throw if non-enum serialization attempted */ |
| 131 | private InvalidClassException serializeEx; |
| 132 | /** exception (if any) to throw if default serialization attempted */ |
| 133 | private InvalidClassException defaultSerializeEx; |
| 134 | |
| 135 | /** serializable fields */ |
| 136 | private ObjectStreamField[] fields; |
| 137 | /** aggregate marshalled size of primitive fields */ |
| 138 | private int primDataSize; |
| 139 | /** number of non-primitive fields */ |
| 140 | private int numObjFields; |
| 141 | /** reflector for setting/getting serializable field values */ |
| 142 | private FieldReflector fieldRefl; |
| 143 | /** data layout of serialized objects described by this class desc */ |
| 144 | private volatile ClassDataSlot[] dataLayout; |
| 145 | |
| 146 | /** serialization-appropriate constructor, or null if none */ |
| 147 | private Constructor cons; |
| 148 | /** class-defined writeObject method, or null if none */ |
| 149 | private Method writeObjectMethod; |
| 150 | /** class-defined readObject method, or null if none */ |
| 151 | private Method readObjectMethod; |
| 152 | /** class-defined readObjectNoData method, or null if none */ |
| 153 | private Method readObjectNoDataMethod; |
| 154 | /** class-defined writeReplace method, or null if none */ |
| 155 | private Method writeReplaceMethod; |
| 156 | /** class-defined readResolve method, or null if none */ |
| 157 | private Method readResolveMethod; |
| 158 | |
| 159 | /** local class descriptor for represented class (may point to self) */ |
| 160 | private ObjectStreamClass localDesc; |
| 161 | /** superclass descriptor appearing in stream */ |
| 162 | private ObjectStreamClass superDesc; |
| 163 | |
| 164 | /** |
| 165 | * Initializes native code. |
| 166 | */ |
| 167 | private static native void initNative(); |
| 168 | static { |
| 169 | initNative(); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Find the descriptor for a class that can be serialized. Creates an |
| 174 | * ObjectStreamClass instance if one does not exist yet for class. Null is |
| 175 | * returned if the specified class does not implement java.io.Serializable |
| 176 | * or java.io.Externalizable. |
| 177 | * |
| 178 | * @param cl class for which to get the descriptor |
| 179 | * @return the class descriptor for the specified class |
| 180 | */ |
| 181 | public static ObjectStreamClass lookup(Class<?> cl) { |
| 182 | return lookup(cl, false); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Returns the descriptor for any class, regardless of whether it |
| 187 | * implements {@link Serializable}. |
| 188 | * |
| 189 | * @param cl class for which to get the descriptor |
| 190 | * @return the class descriptor for the specified class |
| 191 | * @since 1.6 |
| 192 | */ |
| 193 | public static ObjectStreamClass lookupAny(Class<?> cl) { |
| 194 | return lookup(cl, true); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Returns the name of the class described by this descriptor. |
| 199 | * This method returns the name of the class in the format that |
| 200 | * is used by the {@link Class#getName} method. |
| 201 | * |
| 202 | * @return a string representing the name of the class |
| 203 | */ |
| 204 | public String getName() { |
| 205 | return name; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Return the serialVersionUID for this class. The serialVersionUID |
| 210 | * defines a set of classes all with the same name that have evolved from a |
| 211 | * common root class and agree to be serialized and deserialized using a |
| 212 | * common format. NonSerializable classes have a serialVersionUID of 0L. |
| 213 | * |
| 214 | * @return the SUID of the class described by this descriptor |
| 215 | */ |
| 216 | public long getSerialVersionUID() { |
| 217 | // REMIND: synchronize instead of relying on volatile? |
| 218 | if (suid == null) { |
| 219 | suid = (Long) AccessController.doPrivileged( |
| 220 | new PrivilegedAction() { |
| 221 | public Object run() { |
| 222 | return Long.valueOf(computeDefaultSUID(cl)); |
| 223 | } |
| 224 | } |
| 225 | ); |
| 226 | } |
| 227 | return suid.longValue(); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Return the class in the local VM that this version is mapped to. Null |
| 232 | * is returned if there is no corresponding local class. |
| 233 | * |
| 234 | * @return the <code>Class</code> instance that this descriptor represents |
| 235 | */ |
| 236 | public Class<?> forClass() { |
| 237 | return cl; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Return an array of the fields of this serializable class. |
| 242 | * |
| 243 | * @return an array containing an element for each persistent field of |
| 244 | * this class. Returns an array of length zero if there are no |
| 245 | * fields. |
| 246 | * @since 1.2 |
| 247 | */ |
| 248 | public ObjectStreamField[] getFields() { |
| 249 | return getFields(true); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Get the field of this class by name. |
| 254 | * |
| 255 | * @param name the name of the data field to look for |
| 256 | * @return The ObjectStreamField object of the named field or null if |
| 257 | * there is no such named field. |
| 258 | */ |
| 259 | public ObjectStreamField getField(String name) { |
| 260 | return getField(name, null); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Return a string describing this ObjectStreamClass. |
| 265 | */ |
| 266 | public String toString() { |
| 267 | return name + ": static final long serialVersionUID = " + |
| 268 | getSerialVersionUID() + "L;"; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Looks up and returns class descriptor for given class, or null if class |
| 273 | * is non-serializable and "all" is set to false. |
| 274 | * |
| 275 | * @param cl class to look up |
| 276 | * @param all if true, return descriptors for all classes; if false, only |
| 277 | * return descriptors for serializable classes |
| 278 | */ |
| 279 | static ObjectStreamClass lookup(Class cl, boolean all) { |
| 280 | if (!(all || Serializable.class.isAssignableFrom(cl))) { |
| 281 | return null; |
| 282 | } |
| 283 | processQueue(Caches.localDescsQueue, Caches.localDescs); |
| 284 | WeakClassKey key = new WeakClassKey(cl, Caches.localDescsQueue); |
| 285 | Reference<?> ref = Caches.localDescs.get(key); |
| 286 | Object entry = null; |
| 287 | if (ref != null) { |
| 288 | entry = ref.get(); |
| 289 | } |
| 290 | EntryFuture future = null; |
| 291 | if (entry == null) { |
| 292 | EntryFuture newEntry = new EntryFuture(); |
| 293 | Reference<?> newRef = new SoftReference<EntryFuture>(newEntry); |
| 294 | do { |
| 295 | if (ref != null) { |
| 296 | Caches.localDescs.remove(key, ref); |
| 297 | } |
| 298 | ref = Caches.localDescs.putIfAbsent(key, newRef); |
| 299 | if (ref != null) { |
| 300 | entry = ref.get(); |
| 301 | } |
| 302 | } while (ref != null && entry == null); |
| 303 | if (entry == null) { |
| 304 | future = newEntry; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | if (entry instanceof ObjectStreamClass) { // check common case first |
| 309 | return (ObjectStreamClass) entry; |
| 310 | } |
| 311 | if (entry instanceof EntryFuture) { |
| 312 | future = (EntryFuture) entry; |
| 313 | if (future.getOwner() == Thread.currentThread()) { |
| 314 | /* |
| 315 | * Handle nested call situation described by 4803747: waiting |
| 316 | * for future value to be set by a lookup() call further up the |
| 317 | * stack will result in deadlock, so calculate and set the |
| 318 | * future value here instead. |
| 319 | */ |
| 320 | entry = null; |
| 321 | } else { |
| 322 | entry = future.get(); |
| 323 | } |
| 324 | } |
| 325 | if (entry == null) { |
| 326 | try { |
| 327 | entry = new ObjectStreamClass(cl); |
| 328 | } catch (Throwable th) { |
| 329 | entry = th; |
| 330 | } |
| 331 | if (future.set(entry)) { |
| 332 | Caches.localDescs.put(key, new SoftReference<Object>(entry)); |
| 333 | } else { |
| 334 | // nested lookup call already set future |
| 335 | entry = future.get(); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | if (entry instanceof ObjectStreamClass) { |
| 340 | return (ObjectStreamClass) entry; |
| 341 | } else if (entry instanceof RuntimeException) { |
| 342 | throw (RuntimeException) entry; |
| 343 | } else if (entry instanceof Error) { |
| 344 | throw (Error) entry; |
| 345 | } else { |
| 346 | throw new InternalError("unexpected entry: " + entry); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Placeholder used in class descriptor and field reflector lookup tables |
| 352 | * for an entry in the process of being initialized. (Internal) callers |
| 353 | * which receive an EntryFuture belonging to another thread as the result |
| 354 | * of a lookup should call the get() method of the EntryFuture; this will |
| 355 | * return the actual entry once it is ready for use and has been set(). To |
| 356 | * conserve objects, EntryFutures synchronize on themselves. |
| 357 | */ |
| 358 | private static class EntryFuture { |
| 359 | |
| 360 | private static final Object unset = new Object(); |
| 361 | private final Thread owner = Thread.currentThread(); |
| 362 | private Object entry = unset; |
| 363 | |
| 364 | /** |
| 365 | * Attempts to set the value contained by this EntryFuture. If the |
| 366 | * EntryFuture's value has not been set already, then the value is |
| 367 | * saved, any callers blocked in the get() method are notified, and |
| 368 | * true is returned. If the value has already been set, then no saving |
| 369 | * or notification occurs, and false is returned. |
| 370 | */ |
| 371 | synchronized boolean set(Object entry) { |
| 372 | if (this.entry != unset) { |
| 373 | return false; |
| 374 | } |
| 375 | this.entry = entry; |
| 376 | notifyAll(); |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Returns the value contained by this EntryFuture, blocking if |
| 382 | * necessary until a value is set. |
| 383 | */ |
| 384 | synchronized Object get() { |
| 385 | boolean interrupted = false; |
| 386 | while (entry == unset) { |
| 387 | try { |
| 388 | wait(); |
| 389 | } catch (InterruptedException ex) { |
| 390 | interrupted = true; |
| 391 | } |
| 392 | } |
| 393 | if (interrupted) { |
| 394 | AccessController.doPrivileged( |
| 395 | new PrivilegedAction() { |
| 396 | public Object run() { |
| 397 | Thread.currentThread().interrupt(); |
| 398 | return null; |
| 399 | } |
| 400 | } |
| 401 | ); |
| 402 | } |
| 403 | return entry; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Returns the thread that created this EntryFuture. |
| 408 | */ |
| 409 | Thread getOwner() { |
| 410 | return owner; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Creates local class descriptor representing given class. |
| 416 | */ |
| 417 | private ObjectStreamClass(final Class cl) { |
| 418 | this.cl = cl; |
| 419 | name = cl.getName(); |
| 420 | isProxy = Proxy.isProxyClass(cl); |
| 421 | isEnum = Enum.class.isAssignableFrom(cl); |
| 422 | serializable = Serializable.class.isAssignableFrom(cl); |
| 423 | externalizable = Externalizable.class.isAssignableFrom(cl); |
| 424 | |
| 425 | Class superCl = cl.getSuperclass(); |
| 426 | superDesc = (superCl != null) ? lookup(superCl, false) : null; |
| 427 | localDesc = this; |
| 428 | |
| 429 | if (serializable) { |
| 430 | AccessController.doPrivileged(new PrivilegedAction() { |
| 431 | public Object run() { |
| 432 | if (isEnum) { |
| 433 | suid = Long.valueOf(0); |
| 434 | fields = NO_FIELDS; |
| 435 | return null; |
| 436 | } |
| 437 | if (cl.isArray()) { |
| 438 | fields = NO_FIELDS; |
| 439 | return null; |
| 440 | } |
| 441 | |
| 442 | suid = getDeclaredSUID(cl); |
| 443 | try { |
| 444 | fields = getSerialFields(cl); |
| 445 | computeFieldOffsets(); |
| 446 | } catch (InvalidClassException e) { |
| 447 | serializeEx = deserializeEx = e; |
| 448 | fields = NO_FIELDS; |
| 449 | } |
| 450 | |
| 451 | if (externalizable) { |
| 452 | cons = getExternalizableConstructor(cl); |
| 453 | } else { |
| 454 | cons = getSerializableConstructor(cl); |
| 455 | writeObjectMethod = getPrivateMethod(cl, "writeObject", |
| 456 | new Class[] { ObjectOutputStream.class }, |
| 457 | Void.TYPE); |
| 458 | readObjectMethod = getPrivateMethod(cl, "readObject", |
| 459 | new Class[] { ObjectInputStream.class }, |
| 460 | Void.TYPE); |
| 461 | readObjectNoDataMethod = getPrivateMethod( |
| 462 | cl, "readObjectNoData", null, Void.TYPE); |
| 463 | hasWriteObjectData = (writeObjectMethod != null); |
| 464 | } |
| 465 | writeReplaceMethod = getInheritableMethod( |
| 466 | cl, "writeReplace", null, Object.class); |
| 467 | readResolveMethod = getInheritableMethod( |
| 468 | cl, "readResolve", null, Object.class); |
| 469 | return null; |
| 470 | } |
| 471 | }); |
| 472 | } else { |
| 473 | suid = Long.valueOf(0); |
| 474 | fields = NO_FIELDS; |
| 475 | } |
| 476 | |
| 477 | try { |
| 478 | fieldRefl = getReflector(fields, this); |
| 479 | } catch (InvalidClassException ex) { |
| 480 | // field mismatches impossible when matching local fields vs. self |
| 481 | throw new InternalError(); |
| 482 | } |
| 483 | |
| 484 | if (deserializeEx == null) { |
| 485 | if (isEnum) { |
| 486 | deserializeEx = new InvalidClassException(name, "enum type"); |
| 487 | } else if (cons == null) { |
| 488 | deserializeEx = new InvalidClassException( |
| 489 | name, "no valid constructor"); |
| 490 | } |
| 491 | } |
| 492 | for (int i = 0; i < fields.length; i++) { |
| 493 | if (fields[i].getField() == null) { |
| 494 | defaultSerializeEx = new InvalidClassException( |
| 495 | name, "unmatched serializable field(s) declared"); |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Creates blank class descriptor which should be initialized via a |
| 502 | * subsequent call to initProxy(), initNonProxy() or readNonProxy(). |
| 503 | */ |
| 504 | ObjectStreamClass() { |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Initializes class descriptor representing a proxy class. |
| 509 | */ |
| 510 | void initProxy(Class cl, |
| 511 | ClassNotFoundException resolveEx, |
| 512 | ObjectStreamClass superDesc) |
| 513 | throws InvalidClassException |
| 514 | { |
| 515 | this.cl = cl; |
| 516 | this.resolveEx = resolveEx; |
| 517 | this.superDesc = superDesc; |
| 518 | isProxy = true; |
| 519 | serializable = true; |
| 520 | suid = Long.valueOf(0); |
| 521 | fields = NO_FIELDS; |
| 522 | |
| 523 | if (cl != null) { |
| 524 | localDesc = lookup(cl, true); |
| 525 | if (!localDesc.isProxy) { |
| 526 | throw new InvalidClassException( |
| 527 | "cannot bind proxy descriptor to a non-proxy class"); |
| 528 | } |
| 529 | name = localDesc.name; |
| 530 | externalizable = localDesc.externalizable; |
| 531 | cons = localDesc.cons; |
| 532 | writeReplaceMethod = localDesc.writeReplaceMethod; |
| 533 | readResolveMethod = localDesc.readResolveMethod; |
| 534 | deserializeEx = localDesc.deserializeEx; |
| 535 | } |
| 536 | fieldRefl = getReflector(fields, localDesc); |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * Initializes class descriptor representing a non-proxy class. |
| 541 | */ |
| 542 | void initNonProxy(ObjectStreamClass model, |
| 543 | Class cl, |
| 544 | ClassNotFoundException resolveEx, |
| 545 | ObjectStreamClass superDesc) |
| 546 | throws InvalidClassException |
| 547 | { |
| 548 | this.cl = cl; |
| 549 | this.resolveEx = resolveEx; |
| 550 | this.superDesc = superDesc; |
| 551 | name = model.name; |
| 552 | suid = Long.valueOf(model.getSerialVersionUID()); |
| 553 | isProxy = false; |
| 554 | isEnum = model.isEnum; |
| 555 | serializable = model.serializable; |
| 556 | externalizable = model.externalizable; |
| 557 | hasBlockExternalData = model.hasBlockExternalData; |
| 558 | hasWriteObjectData = model.hasWriteObjectData; |
| 559 | fields = model.fields; |
| 560 | primDataSize = model.primDataSize; |
| 561 | numObjFields = model.numObjFields; |
| 562 | |
| 563 | if (cl != null) { |
| 564 | localDesc = lookup(cl, true); |
| 565 | if (localDesc.isProxy) { |
| 566 | throw new InvalidClassException( |
| 567 | "cannot bind non-proxy descriptor to a proxy class"); |
| 568 | } |
| 569 | if (isEnum != localDesc.isEnum) { |
| 570 | throw new InvalidClassException(isEnum ? |
| 571 | "cannot bind enum descriptor to a non-enum class" : |
| 572 | "cannot bind non-enum descriptor to an enum class"); |
| 573 | } |
| 574 | |
| 575 | if (serializable == localDesc.serializable && |
| 576 | !cl.isArray() && |
| 577 | suid.longValue() != localDesc.getSerialVersionUID()) |
| 578 | { |
| 579 | throw new InvalidClassException(localDesc.name, |
| 580 | "local class incompatible: " + |
| 581 | "stream classdesc serialVersionUID = " + suid + |
| 582 | ", local class serialVersionUID = " + |
| 583 | localDesc.getSerialVersionUID()); |
| 584 | } |
| 585 | |
| 586 | if (!classNamesEqual(name, localDesc.name)) { |
| 587 | throw new InvalidClassException(localDesc.name, |
| 588 | "local class name incompatible with stream class " + |
| 589 | "name \"" + name + "\""); |
| 590 | } |
| 591 | |
| 592 | if (!isEnum) { |
| 593 | if ((serializable == localDesc.serializable) && |
| 594 | (externalizable != localDesc.externalizable)) |
| 595 | { |
| 596 | throw new InvalidClassException(localDesc.name, |
| 597 | "Serializable incompatible with Externalizable"); |
| 598 | } |
| 599 | |
| 600 | if ((serializable != localDesc.serializable) || |
| 601 | (externalizable != localDesc.externalizable) || |
| 602 | !(serializable || externalizable)) |
| 603 | { |
| 604 | deserializeEx = new InvalidClassException(localDesc.name, |
| 605 | "class invalid for deserialization"); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | cons = localDesc.cons; |
| 610 | writeObjectMethod = localDesc.writeObjectMethod; |
| 611 | readObjectMethod = localDesc.readObjectMethod; |
| 612 | readObjectNoDataMethod = localDesc.readObjectNoDataMethod; |
| 613 | writeReplaceMethod = localDesc.writeReplaceMethod; |
| 614 | readResolveMethod = localDesc.readResolveMethod; |
| 615 | if (deserializeEx == null) { |
| 616 | deserializeEx = localDesc.deserializeEx; |
| 617 | } |
| 618 | } |
| 619 | fieldRefl = getReflector(fields, localDesc); |
| 620 | // reassign to matched fields so as to reflect local unshared settings |
| 621 | fields = fieldRefl.getFields(); |
| 622 | } |
| 623 | |
| 624 | /** |
| 625 | * Reads non-proxy class descriptor information from given input stream. |
| 626 | * The resulting class descriptor is not fully functional; it can only be |
| 627 | * used as input to the ObjectInputStream.resolveClass() and |
| 628 | * ObjectStreamClass.initNonProxy() methods. |
| 629 | */ |
| 630 | void readNonProxy(ObjectInputStream in) |
| 631 | throws IOException, ClassNotFoundException |
| 632 | { |
| 633 | name = in.readUTF(); |
| 634 | suid = Long.valueOf(in.readLong()); |
| 635 | isProxy = false; |
| 636 | |
| 637 | byte flags = in.readByte(); |
| 638 | hasWriteObjectData = |
| 639 | ((flags & ObjectStreamConstants.SC_WRITE_METHOD) != 0); |
| 640 | hasBlockExternalData = |
| 641 | ((flags & ObjectStreamConstants.SC_BLOCK_DATA) != 0); |
| 642 | externalizable = |
| 643 | ((flags & ObjectStreamConstants.SC_EXTERNALIZABLE) != 0); |
| 644 | boolean sflag = |
| 645 | ((flags & ObjectStreamConstants.SC_SERIALIZABLE) != 0); |
| 646 | if (externalizable && sflag) { |
| 647 | throw new InvalidClassException( |
| 648 | name, "serializable and externalizable flags conflict"); |
| 649 | } |
| 650 | serializable = externalizable || sflag; |
| 651 | isEnum = ((flags & ObjectStreamConstants.SC_ENUM) != 0); |
| 652 | if (isEnum && suid.longValue() != 0L) { |
| 653 | throw new InvalidClassException(name, |
| 654 | "enum descriptor has non-zero serialVersionUID: " + suid); |
| 655 | } |
| 656 | |
| 657 | int numFields = in.readShort(); |
| 658 | if (isEnum && numFields != 0) { |
| 659 | throw new InvalidClassException(name, |
| 660 | "enum descriptor has non-zero field count: " + numFields); |
| 661 | } |
| 662 | fields = (numFields > 0) ? |
| 663 | new ObjectStreamField[numFields] : NO_FIELDS; |
| 664 | for (int i = 0; i < numFields; i++) { |
| 665 | char tcode = (char) in.readByte(); |
| 666 | String fname = in.readUTF(); |
| 667 | String signature = ((tcode == 'L') || (tcode == '[')) ? |
| 668 | in.readTypeString() : new String(new char[] { tcode }); |
| 669 | try { |
| 670 | fields[i] = new ObjectStreamField(fname, signature, false); |
| 671 | } catch (RuntimeException e) { |
| 672 | throw (IOException) new InvalidClassException(name, |
| 673 | "invalid descriptor for field " + fname).initCause(e); |
| 674 | } |
| 675 | } |
| 676 | computeFieldOffsets(); |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * Writes non-proxy class descriptor information to given output stream. |
| 681 | */ |
| 682 | void writeNonProxy(ObjectOutputStream out) throws IOException { |
| 683 | out.writeUTF(name); |
| 684 | out.writeLong(getSerialVersionUID()); |
| 685 | |
| 686 | byte flags = 0; |
| 687 | if (externalizable) { |
| 688 | flags |= ObjectStreamConstants.SC_EXTERNALIZABLE; |
| 689 | int protocol = out.getProtocolVersion(); |
| 690 | if (protocol != ObjectStreamConstants.PROTOCOL_VERSION_1) { |
| 691 | flags |= ObjectStreamConstants.SC_BLOCK_DATA; |
| 692 | } |
| 693 | } else if (serializable) { |
| 694 | flags |= ObjectStreamConstants.SC_SERIALIZABLE; |
| 695 | } |
| 696 | if (hasWriteObjectData) { |
| 697 | flags |= ObjectStreamConstants.SC_WRITE_METHOD; |
| 698 | } |
| 699 | if (isEnum) { |
| 700 | flags |= ObjectStreamConstants.SC_ENUM; |
| 701 | } |
| 702 | out.writeByte(flags); |
| 703 | |
| 704 | out.writeShort(fields.length); |
| 705 | for (int i = 0; i < fields.length; i++) { |
| 706 | ObjectStreamField f = fields[i]; |
| 707 | out.writeByte(f.getTypeCode()); |
| 708 | out.writeUTF(f.getName()); |
| 709 | if (!f.isPrimitive()) { |
| 710 | out.writeTypeString(f.getTypeString()); |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * Returns ClassNotFoundException (if any) thrown while attempting to |
| 717 | * resolve local class corresponding to this class descriptor. |
| 718 | */ |
| 719 | ClassNotFoundException getResolveException() { |
| 720 | return resolveEx; |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Throws an InvalidClassException if object instances referencing this |
| 725 | * class descriptor should not be allowed to deserialize. This method does |
| 726 | * not apply to deserialization of enum constants. |
| 727 | */ |
| 728 | void checkDeserialize() throws InvalidClassException { |
| 729 | if (deserializeEx != null) { |
| 730 | InvalidClassException ice = |
| 731 | new InvalidClassException(deserializeEx.classname, |
| 732 | deserializeEx.getMessage()); |
| 733 | ice.initCause(deserializeEx); |
| 734 | throw ice; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * Throws an InvalidClassException if objects whose class is represented by |
| 740 | * this descriptor should not be allowed to serialize. This method does |
| 741 | * not apply to serialization of enum constants. |
| 742 | */ |
| 743 | void checkSerialize() throws InvalidClassException { |
| 744 | if (serializeEx != null) { |
| 745 | InvalidClassException ice = |
| 746 | new InvalidClassException(serializeEx.classname, |
| 747 | serializeEx.getMessage()); |
| 748 | ice.initCause(serializeEx); |
| 749 | throw ice; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * Throws an InvalidClassException if objects whose class is represented by |
| 755 | * this descriptor should not be permitted to use default serialization |
| 756 | * (e.g., if the class declares serializable fields that do not correspond |
| 757 | * to actual fields, and hence must use the GetField API). This method |
| 758 | * does not apply to deserialization of enum constants. |
| 759 | */ |
| 760 | void checkDefaultSerialize() throws InvalidClassException { |
| 761 | if (defaultSerializeEx != null) { |
| 762 | InvalidClassException ice = |
| 763 | new InvalidClassException(defaultSerializeEx.classname, |
| 764 | defaultSerializeEx.getMessage()); |
| 765 | ice.initCause(defaultSerializeEx); |
| 766 | throw ice; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * Returns superclass descriptor. Note that on the receiving side, the |
| 772 | * superclass descriptor may be bound to a class that is not a superclass |
| 773 | * of the subclass descriptor's bound class. |
| 774 | */ |
| 775 | ObjectStreamClass getSuperDesc() { |
| 776 | return superDesc; |
| 777 | } |
| 778 | |
| 779 | /** |
| 780 | * Returns the "local" class descriptor for the class associated with this |
| 781 | * class descriptor (i.e., the result of |
| 782 | * ObjectStreamClass.lookup(this.forClass())) or null if there is no class |
| 783 | * associated with this descriptor. |
| 784 | */ |
| 785 | ObjectStreamClass getLocalDesc() { |
| 786 | return localDesc; |
| 787 | } |
| 788 | |
| 789 | /** |
| 790 | * Returns arrays of ObjectStreamFields representing the serializable |
| 791 | * fields of the represented class. If copy is true, a clone of this class |
| 792 | * descriptor's field array is returned, otherwise the array itself is |
| 793 | * returned. |
| 794 | */ |
| 795 | ObjectStreamField[] getFields(boolean copy) { |
| 796 | return copy ? fields.clone() : fields; |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * Looks up a serializable field of the represented class by name and type. |
| 801 | * A specified type of null matches all types, Object.class matches all |
| 802 | * non-primitive types, and any other non-null type matches assignable |
| 803 | * types only. Returns matching field, or null if no match found. |
| 804 | */ |
| 805 | ObjectStreamField getField(String name, Class type) { |
| 806 | for (int i = 0; i < fields.length; i++) { |
| 807 | ObjectStreamField f = fields[i]; |
| 808 | if (f.getName().equals(name)) { |
| 809 | if (type == null || |
| 810 | (type == Object.class && !f.isPrimitive())) |
| 811 | { |
| 812 | return f; |
| 813 | } |
| 814 | Class ftype = f.getType(); |
| 815 | if (ftype != null && type.isAssignableFrom(ftype)) { |
| 816 | return f; |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | return null; |
| 821 | } |
| 822 | |
| 823 | /** |
| 824 | * Returns true if class descriptor represents a dynamic proxy class, false |
| 825 | * otherwise. |
| 826 | */ |
| 827 | boolean isProxy() { |
| 828 | return isProxy; |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * Returns true if class descriptor represents an enum type, false |
| 833 | * otherwise. |
| 834 | */ |
| 835 | boolean isEnum() { |
| 836 | return isEnum; |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * Returns true if represented class implements Externalizable, false |
| 841 | * otherwise. |
| 842 | */ |
| 843 | boolean isExternalizable() { |
| 844 | return externalizable; |
| 845 | } |
| 846 | |
| 847 | /** |
| 848 | * Returns true if represented class implements Serializable, false |
| 849 | * otherwise. |
| 850 | */ |
| 851 | boolean isSerializable() { |
| 852 | return serializable; |
| 853 | } |
| 854 | |
| 855 | /** |
| 856 | * Returns true if class descriptor represents externalizable class that |
| 857 | * has written its data in 1.2 (block data) format, false otherwise. |
| 858 | */ |
| 859 | boolean hasBlockExternalData() { |
| 860 | return hasBlockExternalData; |
| 861 | } |
| 862 | |
| 863 | /** |
| 864 | * Returns true if class descriptor represents serializable (but not |
| 865 | * externalizable) class which has written its data via a custom |
| 866 | * writeObject() method, false otherwise. |
| 867 | */ |
| 868 | boolean hasWriteObjectData() { |
| 869 | return hasWriteObjectData; |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * Returns true if represented class is serializable/externalizable and can |
| 874 | * be instantiated by the serialization runtime--i.e., if it is |
| 875 | * externalizable and defines a public no-arg constructor, or if it is |
| 876 | * non-externalizable and its first non-serializable superclass defines an |
| 877 | * accessible no-arg constructor. Otherwise, returns false. |
| 878 | */ |
| 879 | boolean isInstantiable() { |
| 880 | return (cons != null); |
| 881 | } |
| 882 | |
| 883 | /** |
| 884 | * Returns true if represented class is serializable (but not |
| 885 | * externalizable) and defines a conformant writeObject method. Otherwise, |
| 886 | * returns false. |
| 887 | */ |
| 888 | boolean hasWriteObjectMethod() { |
| 889 | return (writeObjectMethod != null); |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * Returns true if represented class is serializable (but not |
| 894 | * externalizable) and defines a conformant readObject method. Otherwise, |
| 895 | * returns false. |
| 896 | */ |
| 897 | boolean hasReadObjectMethod() { |
| 898 | return (readObjectMethod != null); |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * Returns true if represented class is serializable (but not |
| 903 | * externalizable) and defines a conformant readObjectNoData method. |
| 904 | * Otherwise, returns false. |
| 905 | */ |
| 906 | boolean hasReadObjectNoDataMethod() { |
| 907 | return (readObjectNoDataMethod != null); |
| 908 | } |
| 909 | |
| 910 | /** |
| 911 | * Returns true if represented class is serializable or externalizable and |
| 912 | * defines a conformant writeReplace method. Otherwise, returns false. |
| 913 | */ |
| 914 | boolean hasWriteReplaceMethod() { |
| 915 | return (writeReplaceMethod != null); |
| 916 | } |
| 917 | |
| 918 | /** |
| 919 | * Returns true if represented class is serializable or externalizable and |
| 920 | * defines a conformant readResolve method. Otherwise, returns false. |
| 921 | */ |
| 922 | boolean hasReadResolveMethod() { |
| 923 | return (readResolveMethod != null); |
| 924 | } |
| 925 | |
| 926 | /** |
| 927 | * Creates a new instance of the represented class. If the class is |
| 928 | * externalizable, invokes its public no-arg constructor; otherwise, if the |
| 929 | * class is serializable, invokes the no-arg constructor of the first |
| 930 | * non-serializable superclass. Throws UnsupportedOperationException if |
| 931 | * this class descriptor is not associated with a class, if the associated |
| 932 | * class is non-serializable or if the appropriate no-arg constructor is |
| 933 | * inaccessible/unavailable. |
| 934 | */ |
| 935 | Object newInstance() |
| 936 | throws InstantiationException, InvocationTargetException, |
| 937 | UnsupportedOperationException |
| 938 | { |
| 939 | if (cons != null) { |
| 940 | try { |
| 941 | return cons.newInstance(); |
| 942 | } catch (IllegalAccessException ex) { |
| 943 | // should not occur, as access checks have been suppressed |
| 944 | throw new InternalError(); |
| 945 | } |
| 946 | } else { |
| 947 | throw new UnsupportedOperationException(); |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | /** |
| 952 | * Invokes the writeObject method of the represented serializable class. |
| 953 | * Throws UnsupportedOperationException if this class descriptor is not |
| 954 | * associated with a class, or if the class is externalizable, |
| 955 | * non-serializable or does not define writeObject. |
| 956 | */ |
| 957 | void invokeWriteObject(Object obj, ObjectOutputStream out) |
| 958 | throws IOException, UnsupportedOperationException |
| 959 | { |
| 960 | if (writeObjectMethod != null) { |
| 961 | try { |
| 962 | writeObjectMethod.invoke(obj, new Object[]{ out }); |
| 963 | } catch (InvocationTargetException ex) { |
| 964 | Throwable th = ex.getTargetException(); |
| 965 | if (th instanceof IOException) { |
| 966 | throw (IOException) th; |
| 967 | } else { |
| 968 | throwMiscException(th); |
| 969 | } |
| 970 | } catch (IllegalAccessException ex) { |
| 971 | // should not occur, as access checks have been suppressed |
| 972 | throw new InternalError(); |
| 973 | } |
| 974 | } else { |
| 975 | throw new UnsupportedOperationException(); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | /** |
| 980 | * Invokes the readObject method of the represented serializable class. |
| 981 | * Throws UnsupportedOperationException if this class descriptor is not |
| 982 | * associated with a class, or if the class is externalizable, |
| 983 | * non-serializable or does not define readObject. |
| 984 | */ |
| 985 | void invokeReadObject(Object obj, ObjectInputStream in) |
| 986 | throws ClassNotFoundException, IOException, |
| 987 | UnsupportedOperationException |
| 988 | { |
| 989 | if (readObjectMethod != null) { |
| 990 | try { |
| 991 | readObjectMethod.invoke(obj, new Object[]{ in }); |
| 992 | } catch (InvocationTargetException ex) { |
| 993 | Throwable th = ex.getTargetException(); |
| 994 | if (th instanceof ClassNotFoundException) { |
| 995 | throw (ClassNotFoundException) th; |
| 996 | } else if (th instanceof IOException) { |
| 997 | throw (IOException) th; |
| 998 | } else { |
| 999 | throwMiscException(th); |
| 1000 | } |
| 1001 | } catch (IllegalAccessException ex) { |
| 1002 | // should not occur, as access checks have been suppressed |
| 1003 | throw new InternalError(); |
| 1004 | } |
| 1005 | } else { |
| 1006 | throw new UnsupportedOperationException(); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | /** |
| 1011 | * Invokes the readObjectNoData method of the represented serializable |
| 1012 | * class. Throws UnsupportedOperationException if this class descriptor is |
| 1013 | * not associated with a class, or if the class is externalizable, |
| 1014 | * non-serializable or does not define readObjectNoData. |
| 1015 | */ |
| 1016 | void invokeReadObjectNoData(Object obj) |
| 1017 | throws IOException, UnsupportedOperationException |
| 1018 | { |
| 1019 | if (readObjectNoDataMethod != null) { |
| 1020 | try { |
| 1021 | readObjectNoDataMethod.invoke(obj, (Object[]) null); |
| 1022 | } catch (InvocationTargetException ex) { |
| 1023 | Throwable th = ex.getTargetException(); |
| 1024 | if (th instanceof ObjectStreamException) { |
| 1025 | throw (ObjectStreamException) th; |
| 1026 | } else { |
| 1027 | throwMiscException(th); |
| 1028 | } |
| 1029 | } catch (IllegalAccessException ex) { |
| 1030 | // should not occur, as access checks have been suppressed |
| 1031 | throw new InternalError(); |
| 1032 | } |
| 1033 | } else { |
| 1034 | throw new UnsupportedOperationException(); |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * Invokes the writeReplace method of the represented serializable class and |
| 1040 | * returns the result. Throws UnsupportedOperationException if this class |
| 1041 | * descriptor is not associated with a class, or if the class is |
| 1042 | * non-serializable or does not define writeReplace. |
| 1043 | */ |
| 1044 | Object invokeWriteReplace(Object obj) |
| 1045 | throws IOException, UnsupportedOperationException |
| 1046 | { |
| 1047 | if (writeReplaceMethod != null) { |
| 1048 | try { |
| 1049 | return writeReplaceMethod.invoke(obj, (Object[]) null); |
| 1050 | } catch (InvocationTargetException ex) { |
| 1051 | Throwable th = ex.getTargetException(); |
| 1052 | if (th instanceof ObjectStreamException) { |
| 1053 | throw (ObjectStreamException) th; |
| 1054 | } else { |
| 1055 | throwMiscException(th); |
| 1056 | throw new InternalError(); // never reached |
| 1057 | } |
| 1058 | } catch (IllegalAccessException ex) { |
| 1059 | // should not occur, as access checks have been suppressed |
| 1060 | throw new InternalError(); |
| 1061 | } |
| 1062 | } else { |
| 1063 | throw new UnsupportedOperationException(); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | /** |
| 1068 | * Invokes the readResolve method of the represented serializable class and |
| 1069 | * returns the result. Throws UnsupportedOperationException if this class |
| 1070 | * descriptor is not associated with a class, or if the class is |
| 1071 | * non-serializable or does not define readResolve. |
| 1072 | */ |
| 1073 | Object invokeReadResolve(Object obj) |
| 1074 | throws IOException, UnsupportedOperationException |
| 1075 | { |
| 1076 | if (readResolveMethod != null) { |
| 1077 | try { |
| 1078 | return readResolveMethod.invoke(obj, (Object[]) null); |
| 1079 | } catch (InvocationTargetException ex) { |
| 1080 | Throwable th = ex.getTargetException(); |
| 1081 | if (th instanceof ObjectStreamException) { |
| 1082 | throw (ObjectStreamException) th; |
| 1083 | } else { |
| 1084 | throwMiscException(th); |
| 1085 | throw new InternalError(); // never reached |
| 1086 | } |
| 1087 | } catch (IllegalAccessException ex) { |
| 1088 | // should not occur, as access checks have been suppressed |
| 1089 | throw new InternalError(); |
| 1090 | } |
| 1091 | } else { |
| 1092 | throw new UnsupportedOperationException(); |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | /** |
| 1097 | * Class representing the portion of an object's serialized form allotted |
| 1098 | * to data described by a given class descriptor. If "hasData" is false, |
| 1099 | * the object's serialized form does not contain data associated with the |
| 1100 | * class descriptor. |
| 1101 | */ |
| 1102 | static class ClassDataSlot { |
| 1103 | |
| 1104 | /** class descriptor "occupying" this slot */ |
| 1105 | final ObjectStreamClass desc; |
| 1106 | /** true if serialized form includes data for this slot's descriptor */ |
| 1107 | final boolean hasData; |
| 1108 | |
| 1109 | ClassDataSlot(ObjectStreamClass desc, boolean hasData) { |
| 1110 | this.desc = desc; |
| 1111 | this.hasData = hasData; |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | /** |
| 1116 | * Returns array of ClassDataSlot instances representing the data layout |
| 1117 | * (including superclass data) for serialized objects described by this |
| 1118 | * class descriptor. ClassDataSlots are ordered by inheritance with those |
| 1119 | * containing "higher" superclasses appearing first. The final |
| 1120 | * ClassDataSlot contains a reference to this descriptor. |
| 1121 | */ |
| 1122 | ClassDataSlot[] getClassDataLayout() throws InvalidClassException { |
| 1123 | // REMIND: synchronize instead of relying on volatile? |
| 1124 | if (dataLayout == null) { |
| 1125 | dataLayout = getClassDataLayout0(); |
| 1126 | } |
| 1127 | return dataLayout; |
| 1128 | } |
| 1129 | |
| 1130 | private ClassDataSlot[] getClassDataLayout0() |
| 1131 | throws InvalidClassException |
| 1132 | { |
| 1133 | ArrayList slots = new ArrayList(); |
| 1134 | Class start = cl, end = cl; |
| 1135 | |
| 1136 | // locate closest non-serializable superclass |
| 1137 | while (end != null && Serializable.class.isAssignableFrom(end)) { |
| 1138 | end = end.getSuperclass(); |
| 1139 | } |
| 1140 | |
| 1141 | for (ObjectStreamClass d = this; d != null; d = d.superDesc) { |
| 1142 | |
| 1143 | // search up inheritance hierarchy for class with matching name |
| 1144 | String searchName = (d.cl != null) ? d.cl.getName() : d.name; |
| 1145 | Class match = null; |
| 1146 | for (Class c = start; c != end; c = c.getSuperclass()) { |
| 1147 | if (searchName.equals(c.getName())) { |
| 1148 | match = c; |
| 1149 | break; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | // add "no data" slot for each unmatched class below match |
| 1154 | if (match != null) { |
| 1155 | for (Class c = start; c != match; c = c.getSuperclass()) { |
| 1156 | slots.add(new ClassDataSlot( |
| 1157 | ObjectStreamClass.lookup(c, true), false)); |
| 1158 | } |
| 1159 | start = match.getSuperclass(); |
| 1160 | } |
| 1161 | |
| 1162 | // record descriptor/class pairing |
| 1163 | slots.add(new ClassDataSlot(d.getVariantFor(match), true)); |
| 1164 | } |
| 1165 | |
| 1166 | // add "no data" slot for any leftover unmatched classes |
| 1167 | for (Class c = start; c != end; c = c.getSuperclass()) { |
| 1168 | slots.add(new ClassDataSlot( |
| 1169 | ObjectStreamClass.lookup(c, true), false)); |
| 1170 | } |
| 1171 | |
| 1172 | // order slots from superclass -> subclass |
| 1173 | Collections.reverse(slots); |
| 1174 | return (ClassDataSlot[]) |
| 1175 | slots.toArray(new ClassDataSlot[slots.size()]); |
| 1176 | } |
| 1177 | |
| 1178 | /** |
| 1179 | * Returns aggregate size (in bytes) of marshalled primitive field values |
| 1180 | * for represented class. |
| 1181 | */ |
| 1182 | int getPrimDataSize() { |
| 1183 | return primDataSize; |
| 1184 | } |
| 1185 | |
| 1186 | /** |
| 1187 | * Returns number of non-primitive serializable fields of represented |
| 1188 | * class. |
| 1189 | */ |
| 1190 | int getNumObjFields() { |
| 1191 | return numObjFields; |
| 1192 | } |
| 1193 | |
| 1194 | /** |
| 1195 | * Fetches the serializable primitive field values of object obj and |
| 1196 | * marshals them into byte array buf starting at offset 0. It is the |
| 1197 | * responsibility of the caller to ensure that obj is of the proper type if |
| 1198 | * non-null. |
| 1199 | */ |
| 1200 | void getPrimFieldValues(Object obj, byte[] buf) { |
| 1201 | fieldRefl.getPrimFieldValues(obj, buf); |
| 1202 | } |
| 1203 | |
| 1204 | /** |
| 1205 | * Sets the serializable primitive fields of object obj using values |
| 1206 | * unmarshalled from byte array buf starting at offset 0. It is the |
| 1207 | * responsibility of the caller to ensure that obj is of the proper type if |
| 1208 | * non-null. |
| 1209 | */ |
| 1210 | void setPrimFieldValues(Object obj, byte[] buf) { |
| 1211 | fieldRefl.setPrimFieldValues(obj, buf); |
| 1212 | } |
| 1213 | |
| 1214 | /** |
| 1215 | * Fetches the serializable object field values of object obj and stores |
| 1216 | * them in array vals starting at offset 0. It is the responsibility of |
| 1217 | * the caller to ensure that obj is of the proper type if non-null. |
| 1218 | */ |
| 1219 | void getObjFieldValues(Object obj, Object[] vals) { |
| 1220 | fieldRefl.getObjFieldValues(obj, vals); |
| 1221 | } |
| 1222 | |
| 1223 | /** |
| 1224 | * Sets the serializable object fields of object obj using values from |
| 1225 | * array vals starting at offset 0. It is the responsibility of the caller |
| 1226 | * to ensure that obj is of the proper type if non-null. |
| 1227 | */ |
| 1228 | void setObjFieldValues(Object obj, Object[] vals) { |
| 1229 | fieldRefl.setObjFieldValues(obj, vals); |
| 1230 | } |
| 1231 | |
| 1232 | /** |
| 1233 | * Calculates and sets serializable field offsets, as well as primitive |
| 1234 | * data size and object field count totals. Throws InvalidClassException |
| 1235 | * if fields are illegally ordered. |
| 1236 | */ |
| 1237 | private void computeFieldOffsets() throws InvalidClassException { |
| 1238 | primDataSize = 0; |
| 1239 | numObjFields = 0; |
| 1240 | int firstObjIndex = -1; |
| 1241 | |
| 1242 | for (int i = 0; i < fields.length; i++) { |
| 1243 | ObjectStreamField f = fields[i]; |
| 1244 | switch (f.getTypeCode()) { |
| 1245 | case 'Z': |
| 1246 | case 'B': |
| 1247 | f.setOffset(primDataSize++); |
| 1248 | break; |
| 1249 | |
| 1250 | case 'C': |
| 1251 | case 'S': |
| 1252 | f.setOffset(primDataSize); |
| 1253 | primDataSize += 2; |
| 1254 | break; |
| 1255 | |
| 1256 | case 'I': |
| 1257 | case 'F': |
| 1258 | f.setOffset(primDataSize); |
| 1259 | primDataSize += 4; |
| 1260 | break; |
| 1261 | |
| 1262 | case 'J': |
| 1263 | case 'D': |
| 1264 | f.setOffset(primDataSize); |
| 1265 | primDataSize += 8; |
| 1266 | break; |
| 1267 | |
| 1268 | case '[': |
| 1269 | case 'L': |
| 1270 | f.setOffset(numObjFields++); |
| 1271 | if (firstObjIndex == -1) { |
| 1272 | firstObjIndex = i; |
| 1273 | } |
| 1274 | break; |
| 1275 | |
| 1276 | default: |
| 1277 | throw new InternalError(); |
| 1278 | } |
| 1279 | } |
| 1280 | if (firstObjIndex != -1 && |
| 1281 | firstObjIndex + numObjFields != fields.length) |
| 1282 | { |
| 1283 | throw new InvalidClassException(name, "illegal field order"); |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | /** |
| 1288 | * If given class is the same as the class associated with this class |
| 1289 | * descriptor, returns reference to this class descriptor. Otherwise, |
| 1290 | * returns variant of this class descriptor bound to given class. |
| 1291 | */ |
| 1292 | private ObjectStreamClass getVariantFor(Class cl) |
| 1293 | throws InvalidClassException |
| 1294 | { |
| 1295 | if (this.cl == cl) { |
| 1296 | return this; |
| 1297 | } |
| 1298 | ObjectStreamClass desc = new ObjectStreamClass(); |
| 1299 | if (isProxy) { |
| 1300 | desc.initProxy(cl, null, superDesc); |
| 1301 | } else { |
| 1302 | desc.initNonProxy(this, cl, null, superDesc); |
| 1303 | } |
| 1304 | return desc; |
| 1305 | } |
| 1306 | |
| 1307 | /** |
| 1308 | * Returns public no-arg constructor of given class, or null if none found. |
| 1309 | * Access checks are disabled on the returned constructor (if any), since |
| 1310 | * the defining class may still be non-public. |
| 1311 | */ |
| 1312 | private static Constructor getExternalizableConstructor(Class cl) { |
| 1313 | try { |
| 1314 | Constructor cons = cl.getDeclaredConstructor((Class[]) null); |
| 1315 | cons.setAccessible(true); |
| 1316 | return ((cons.getModifiers() & Modifier.PUBLIC) != 0) ? |
| 1317 | cons : null; |
| 1318 | } catch (NoSuchMethodException ex) { |
| 1319 | return null; |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | /** |
| 1324 | * Returns subclass-accessible no-arg constructor of first non-serializable |
| 1325 | * superclass, or null if none found. Access checks are disabled on the |
| 1326 | * returned constructor (if any). |
| 1327 | */ |
| 1328 | private static Constructor getSerializableConstructor(Class cl) { |
| 1329 | Class initCl = cl; |
| 1330 | while (Serializable.class.isAssignableFrom(initCl)) { |
| 1331 | if ((initCl = initCl.getSuperclass()) == null) { |
| 1332 | return null; |
| 1333 | } |
| 1334 | } |
| 1335 | try { |
| 1336 | Constructor cons = initCl.getDeclaredConstructor((Class[]) null); |
| 1337 | int mods = cons.getModifiers(); |
| 1338 | if ((mods & Modifier.PRIVATE) != 0 || |
| 1339 | ((mods & (Modifier.PUBLIC | Modifier.PROTECTED)) == 0 && |
| 1340 | !packageEquals(cl, initCl))) |
| 1341 | { |
| 1342 | return null; |
| 1343 | } |
| 1344 | cons = reflFactory.newConstructorForSerialization(cl, cons); |
| 1345 | cons.setAccessible(true); |
| 1346 | return cons; |
| 1347 | } catch (NoSuchMethodException ex) { |
| 1348 | return null; |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | /** |
| 1353 | * Returns non-static, non-abstract method with given signature provided it |
| 1354 | * is defined by or accessible (via inheritance) by the given class, or |
| 1355 | * null if no match found. Access checks are disabled on the returned |
| 1356 | * method (if any). |
| 1357 | */ |
| 1358 | private static Method getInheritableMethod(Class cl, String name, |
| 1359 | Class[] argTypes, |
| 1360 | Class returnType) |
| 1361 | { |
| 1362 | Method meth = null; |
| 1363 | Class defCl = cl; |
| 1364 | while (defCl != null) { |
| 1365 | try { |
| 1366 | meth = defCl.getDeclaredMethod(name, argTypes); |
| 1367 | break; |
| 1368 | } catch (NoSuchMethodException ex) { |
| 1369 | defCl = defCl.getSuperclass(); |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | if ((meth == null) || (meth.getReturnType() != returnType)) { |
| 1374 | return null; |
| 1375 | } |
| 1376 | meth.setAccessible(true); |
| 1377 | int mods = meth.getModifiers(); |
| 1378 | if ((mods & (Modifier.STATIC | Modifier.ABSTRACT)) != 0) { |
| 1379 | return null; |
| 1380 | } else if ((mods & (Modifier.PUBLIC | Modifier.PROTECTED)) != 0) { |
| 1381 | return meth; |
| 1382 | } else if ((mods & Modifier.PRIVATE) != 0) { |
| 1383 | return (cl == defCl) ? meth : null; |
| 1384 | } else { |
| 1385 | return packageEquals(cl, defCl) ? meth : null; |
| 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | /** |
| 1390 | * Returns non-static private method with given signature defined by given |
| 1391 | * class, or null if none found. Access checks are disabled on the |
| 1392 | * returned method (if any). |
| 1393 | */ |
| 1394 | private static Method getPrivateMethod(Class cl, String name, |
| 1395 | Class[] argTypes, |
| 1396 | Class returnType) |
| 1397 | { |
| 1398 | try { |
| 1399 | Method meth = cl.getDeclaredMethod(name, argTypes); |
| 1400 | meth.setAccessible(true); |
| 1401 | int mods = meth.getModifiers(); |
| 1402 | return ((meth.getReturnType() == returnType) && |
| 1403 | ((mods & Modifier.STATIC) == 0) && |
| 1404 | ((mods & Modifier.PRIVATE) != 0)) ? meth : null; |
| 1405 | } catch (NoSuchMethodException ex) { |
| 1406 | return null; |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | /** |
| 1411 | * Returns true if classes are defined in the same runtime package, false |
| 1412 | * otherwise. |
| 1413 | */ |
| 1414 | private static boolean packageEquals(Class cl1, Class cl2) { |
| 1415 | return (cl1.getClassLoader() == cl2.getClassLoader() && |
| 1416 | getPackageName(cl1).equals(getPackageName(cl2))); |
| 1417 | } |
| 1418 | |
| 1419 | /** |
| 1420 | * Returns package name of given class. |
| 1421 | */ |
| 1422 | private static String getPackageName(Class cl) { |
| 1423 | String s = cl.getName(); |
| 1424 | int i = s.lastIndexOf('['); |
| 1425 | if (i >= 0) { |
| 1426 | s = s.substring(i + 2); |
| 1427 | } |
| 1428 | i = s.lastIndexOf('.'); |
| 1429 | return (i >= 0) ? s.substring(0, i) : ""; |
| 1430 | } |
| 1431 | |
| 1432 | /** |
| 1433 | * Compares class names for equality, ignoring package names. Returns true |
| 1434 | * if class names equal, false otherwise. |
| 1435 | */ |
| 1436 | private static boolean classNamesEqual(String name1, String name2) { |
| 1437 | name1 = name1.substring(name1.lastIndexOf('.') + 1); |
| 1438 | name2 = name2.substring(name2.lastIndexOf('.') + 1); |
| 1439 | return name1.equals(name2); |
| 1440 | } |
| 1441 | |
| 1442 | /** |
| 1443 | * Returns JVM type signature for given class. |
| 1444 | */ |
| 1445 | static String getClassSignature(Class cl) { |
| 1446 | StringBuilder sbuf = new StringBuilder(); |
| 1447 | while (cl.isArray()) { |
| 1448 | sbuf.append('['); |
| 1449 | cl = cl.getComponentType(); |
| 1450 | } |
| 1451 | if (cl.isPrimitive()) { |
| 1452 | if (cl == Integer.TYPE) { |
| 1453 | sbuf.append('I'); |
| 1454 | } else if (cl == Byte.TYPE) { |
| 1455 | sbuf.append('B'); |
| 1456 | } else if (cl == Long.TYPE) { |
| 1457 | sbuf.append('J'); |
| 1458 | } else if (cl == Float.TYPE) { |
| 1459 | sbuf.append('F'); |
| 1460 | } else if (cl == Double.TYPE) { |
| 1461 | sbuf.append('D'); |
| 1462 | } else if (cl == Short.TYPE) { |
| 1463 | sbuf.append('S'); |
| 1464 | } else if (cl == Character.TYPE) { |
| 1465 | sbuf.append('C'); |
| 1466 | } else if (cl == Boolean.TYPE) { |
| 1467 | sbuf.append('Z'); |
| 1468 | } else if (cl == Void.TYPE) { |
| 1469 | sbuf.append('V'); |
| 1470 | } else { |
| 1471 | throw new InternalError(); |
| 1472 | } |
| 1473 | } else { |
| 1474 | sbuf.append('L' + cl.getName().replace('.', '/') + ';'); |
| 1475 | } |
| 1476 | return sbuf.toString(); |
| 1477 | } |
| 1478 | |
| 1479 | /** |
| 1480 | * Returns JVM type signature for given list of parameters and return type. |
| 1481 | */ |
| 1482 | private static String getMethodSignature(Class[] paramTypes, |
| 1483 | Class retType) |
| 1484 | { |
| 1485 | StringBuilder sbuf = new StringBuilder(); |
| 1486 | sbuf.append('('); |
| 1487 | for (int i = 0; i < paramTypes.length; i++) { |
| 1488 | sbuf.append(getClassSignature(paramTypes[i])); |
| 1489 | } |
| 1490 | sbuf.append(')'); |
| 1491 | sbuf.append(getClassSignature(retType)); |
| 1492 | return sbuf.toString(); |
| 1493 | } |
| 1494 | |
| 1495 | /** |
| 1496 | * Convenience method for throwing an exception that is either a |
| 1497 | * RuntimeException, Error, or of some unexpected type (in which case it is |
| 1498 | * wrapped inside an IOException). |
| 1499 | */ |
| 1500 | private static void throwMiscException(Throwable th) throws IOException { |
| 1501 | if (th instanceof RuntimeException) { |
| 1502 | throw (RuntimeException) th; |
| 1503 | } else if (th instanceof Error) { |
| 1504 | throw (Error) th; |
| 1505 | } else { |
| 1506 | IOException ex = new IOException("unexpected exception type"); |
| 1507 | ex.initCause(th); |
| 1508 | throw ex; |
| 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | /** |
| 1513 | * Returns ObjectStreamField array describing the serializable fields of |
| 1514 | * the given class. Serializable fields backed by an actual field of the |
| 1515 | * class are represented by ObjectStreamFields with corresponding non-null |
| 1516 | * Field objects. Throws InvalidClassException if the (explicitly |
| 1517 | * declared) serializable fields are invalid. |
| 1518 | */ |
| 1519 | private static ObjectStreamField[] getSerialFields(Class cl) |
| 1520 | throws InvalidClassException |
| 1521 | { |
| 1522 | ObjectStreamField[] fields; |
| 1523 | if (Serializable.class.isAssignableFrom(cl) && |
| 1524 | !Externalizable.class.isAssignableFrom(cl) && |
| 1525 | !Proxy.isProxyClass(cl) && |
| 1526 | !cl.isInterface()) |
| 1527 | { |
| 1528 | if ((fields = getDeclaredSerialFields(cl)) == null) { |
| 1529 | fields = getDefaultSerialFields(cl); |
| 1530 | } |
| 1531 | Arrays.sort(fields); |
| 1532 | } else { |
| 1533 | fields = NO_FIELDS; |
| 1534 | } |
| 1535 | return fields; |
| 1536 | } |
| 1537 | |
| 1538 | /** |
| 1539 | * Returns serializable fields of given class as defined explicitly by a |
| 1540 | * "serialPersistentFields" field, or null if no appropriate |
| 1541 | * "serialPersistentFields" field is defined. Serializable fields backed |
| 1542 | * by an actual field of the class are represented by ObjectStreamFields |
| 1543 | * with corresponding non-null Field objects. For compatibility with past |
| 1544 | * releases, a "serialPersistentFields" field with a null value is |
| 1545 | * considered equivalent to not declaring "serialPersistentFields". Throws |
| 1546 | * InvalidClassException if the declared serializable fields are |
| 1547 | * invalid--e.g., if multiple fields share the same name. |
| 1548 | */ |
| 1549 | private static ObjectStreamField[] getDeclaredSerialFields(Class cl) |
| 1550 | throws InvalidClassException |
| 1551 | { |
| 1552 | ObjectStreamField[] serialPersistentFields = null; |
| 1553 | try { |
| 1554 | Field f = cl.getDeclaredField("serialPersistentFields"); |
| 1555 | int mask = Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL; |
| 1556 | if ((f.getModifiers() & mask) == mask) { |
| 1557 | f.setAccessible(true); |
| 1558 | serialPersistentFields = (ObjectStreamField[]) f.get(null); |
| 1559 | } |
| 1560 | } catch (Exception ex) { |
| 1561 | } |
| 1562 | if (serialPersistentFields == null) { |
| 1563 | return null; |
| 1564 | } else if (serialPersistentFields.length == 0) { |
| 1565 | return NO_FIELDS; |
| 1566 | } |
| 1567 | |
| 1568 | ObjectStreamField[] boundFields = |
| 1569 | new ObjectStreamField[serialPersistentFields.length]; |
| 1570 | Set fieldNames = new HashSet(serialPersistentFields.length); |
| 1571 | |
| 1572 | for (int i = 0; i < serialPersistentFields.length; i++) { |
| 1573 | ObjectStreamField spf = serialPersistentFields[i]; |
| 1574 | |
| 1575 | String fname = spf.getName(); |
| 1576 | if (fieldNames.contains(fname)) { |
| 1577 | throw new InvalidClassException( |
| 1578 | "multiple serializable fields named " + fname); |
| 1579 | } |
| 1580 | fieldNames.add(fname); |
| 1581 | |
| 1582 | try { |
| 1583 | Field f = cl.getDeclaredField(fname); |
| 1584 | if ((f.getType() == spf.getType()) && |
| 1585 | ((f.getModifiers() & Modifier.STATIC) == 0)) |
| 1586 | { |
| 1587 | boundFields[i] = |
| 1588 | new ObjectStreamField(f, spf.isUnshared(), true); |
| 1589 | } |
| 1590 | } catch (NoSuchFieldException ex) { |
| 1591 | } |
| 1592 | if (boundFields[i] == null) { |
| 1593 | boundFields[i] = new ObjectStreamField( |
| 1594 | fname, spf.getType(), spf.isUnshared()); |
| 1595 | } |
| 1596 | } |
| 1597 | return boundFields; |
| 1598 | } |
| 1599 | |
| 1600 | /** |
| 1601 | * Returns array of ObjectStreamFields corresponding to all non-static |
| 1602 | * non-transient fields declared by given class. Each ObjectStreamField |
| 1603 | * contains a Field object for the field it represents. If no default |
| 1604 | * serializable fields exist, NO_FIELDS is returned. |
| 1605 | */ |
| 1606 | private static ObjectStreamField[] getDefaultSerialFields(Class cl) { |
| 1607 | Field[] clFields = cl.getDeclaredFields(); |
| 1608 | ArrayList list = new ArrayList(); |
| 1609 | int mask = Modifier.STATIC | Modifier.TRANSIENT; |
| 1610 | |
| 1611 | for (int i = 0; i < clFields.length; i++) { |
| 1612 | if ((clFields[i].getModifiers() & mask) == 0) { |
| 1613 | list.add(new ObjectStreamField(clFields[i], false, true)); |
| 1614 | } |
| 1615 | } |
| 1616 | int size = list.size(); |
| 1617 | return (size == 0) ? NO_FIELDS : |
| 1618 | (ObjectStreamField[]) list.toArray(new ObjectStreamField[size]); |
| 1619 | } |
| 1620 | |
| 1621 | /** |
| 1622 | * Returns explicit serial version UID value declared by given class, or |
| 1623 | * null if none. |
| 1624 | */ |
| 1625 | private static Long getDeclaredSUID(Class cl) { |
| 1626 | try { |
| 1627 | Field f = cl.getDeclaredField("serialVersionUID"); |
| 1628 | int mask = Modifier.STATIC | Modifier.FINAL; |
| 1629 | if ((f.getModifiers() & mask) == mask) { |
| 1630 | f.setAccessible(true); |
| 1631 | return Long.valueOf(f.getLong(null)); |
| 1632 | } |
| 1633 | } catch (Exception ex) { |
| 1634 | } |
| 1635 | return null; |
| 1636 | } |
| 1637 | |
| 1638 | /** |
| 1639 | * Computes the default serial version UID value for the given class. |
| 1640 | */ |
| 1641 | private static long computeDefaultSUID(Class cl) { |
| 1642 | if (!Serializable.class.isAssignableFrom(cl) || Proxy.isProxyClass(cl)) |
| 1643 | { |
| 1644 | return 0L; |
| 1645 | } |
| 1646 | |
| 1647 | try { |
| 1648 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 1649 | DataOutputStream dout = new DataOutputStream(bout); |
| 1650 | |
| 1651 | dout.writeUTF(cl.getName()); |
| 1652 | |
| 1653 | int classMods = cl.getModifiers() & |
| 1654 | (Modifier.PUBLIC | Modifier.FINAL | |
| 1655 | Modifier.INTERFACE | Modifier.ABSTRACT); |
| 1656 | |
| 1657 | /* |
| 1658 | * compensate for javac bug in which ABSTRACT bit was set for an |
| 1659 | * interface only if the interface declared methods |
| 1660 | */ |
| 1661 | Method[] methods = cl.getDeclaredMethods(); |
| 1662 | if ((classMods & Modifier.INTERFACE) != 0) { |
| 1663 | classMods = (methods.length > 0) ? |
| 1664 | (classMods | Modifier.ABSTRACT) : |
| 1665 | (classMods & ~Modifier.ABSTRACT); |
| 1666 | } |
| 1667 | dout.writeInt(classMods); |
| 1668 | |
| 1669 | if (!cl.isArray()) { |
| 1670 | /* |
| 1671 | * compensate for change in 1.2FCS in which |
| 1672 | * Class.getInterfaces() was modified to return Cloneable and |
| 1673 | * Serializable for array classes. |
| 1674 | */ |
| 1675 | Class[] interfaces = cl.getInterfaces(); |
| 1676 | String[] ifaceNames = new String[interfaces.length]; |
| 1677 | for (int i = 0; i < interfaces.length; i++) { |
| 1678 | ifaceNames[i] = interfaces[i].getName(); |
| 1679 | } |
| 1680 | Arrays.sort(ifaceNames); |
| 1681 | for (int i = 0; i < ifaceNames.length; i++) { |
| 1682 | dout.writeUTF(ifaceNames[i]); |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | Field[] fields = cl.getDeclaredFields(); |
| 1687 | MemberSignature[] fieldSigs = new MemberSignature[fields.length]; |
| 1688 | for (int i = 0; i < fields.length; i++) { |
| 1689 | fieldSigs[i] = new MemberSignature(fields[i]); |
| 1690 | } |
| 1691 | Arrays.sort(fieldSigs, new Comparator() { |
| 1692 | public int compare(Object o1, Object o2) { |
| 1693 | String name1 = ((MemberSignature) o1).name; |
| 1694 | String name2 = ((MemberSignature) o2).name; |
| 1695 | return name1.compareTo(name2); |
| 1696 | } |
| 1697 | }); |
| 1698 | for (int i = 0; i < fieldSigs.length; i++) { |
| 1699 | MemberSignature sig = fieldSigs[i]; |
| 1700 | int mods = sig.member.getModifiers() & |
| 1701 | (Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED | |
| 1702 | Modifier.STATIC | Modifier.FINAL | Modifier.VOLATILE | |
| 1703 | Modifier.TRANSIENT); |
| 1704 | if (((mods & Modifier.PRIVATE) == 0) || |
| 1705 | ((mods & (Modifier.STATIC | Modifier.TRANSIENT)) == 0)) |
| 1706 | { |
| 1707 | dout.writeUTF(sig.name); |
| 1708 | dout.writeInt(mods); |
| 1709 | dout.writeUTF(sig.signature); |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | if (hasStaticInitializer(cl)) { |
| 1714 | dout.writeUTF("<clinit>"); |
| 1715 | dout.writeInt(Modifier.STATIC); |
| 1716 | dout.writeUTF("()V"); |
| 1717 | } |
| 1718 | |
| 1719 | Constructor[] cons = cl.getDeclaredConstructors(); |
| 1720 | MemberSignature[] consSigs = new MemberSignature[cons.length]; |
| 1721 | for (int i = 0; i < cons.length; i++) { |
| 1722 | consSigs[i] = new MemberSignature(cons[i]); |
| 1723 | } |
| 1724 | Arrays.sort(consSigs, new Comparator() { |
| 1725 | public int compare(Object o1, Object o2) { |
| 1726 | String sig1 = ((MemberSignature) o1).signature; |
| 1727 | String sig2 = ((MemberSignature) o2).signature; |
| 1728 | return sig1.compareTo(sig2); |
| 1729 | } |
| 1730 | }); |
| 1731 | for (int i = 0; i < consSigs.length; i++) { |
| 1732 | MemberSignature sig = consSigs[i]; |
| 1733 | int mods = sig.member.getModifiers() & |
| 1734 | (Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED | |
| 1735 | Modifier.STATIC | Modifier.FINAL | |
| 1736 | Modifier.SYNCHRONIZED | Modifier.NATIVE | |
| 1737 | Modifier.ABSTRACT | Modifier.STRICT); |
| 1738 | if ((mods & Modifier.PRIVATE) == 0) { |
| 1739 | dout.writeUTF("<init>"); |
| 1740 | dout.writeInt(mods); |
| 1741 | dout.writeUTF(sig.signature.replace('/', '.')); |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | MemberSignature[] methSigs = new MemberSignature[methods.length]; |
| 1746 | for (int i = 0; i < methods.length; i++) { |
| 1747 | methSigs[i] = new MemberSignature(methods[i]); |
| 1748 | } |
| 1749 | Arrays.sort(methSigs, new Comparator() { |
| 1750 | public int compare(Object o1, Object o2) { |
| 1751 | MemberSignature ms1 = (MemberSignature) o1; |
| 1752 | MemberSignature ms2 = (MemberSignature) o2; |
| 1753 | int comp = ms1.name.compareTo(ms2.name); |
| 1754 | if (comp == 0) { |
| 1755 | comp = ms1.signature.compareTo(ms2.signature); |
| 1756 | } |
| 1757 | return comp; |
| 1758 | } |
| 1759 | }); |
| 1760 | for (int i = 0; i < methSigs.length; i++) { |
| 1761 | MemberSignature sig = methSigs[i]; |
| 1762 | int mods = sig.member.getModifiers() & |
| 1763 | (Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED | |
| 1764 | Modifier.STATIC | Modifier.FINAL | |
| 1765 | Modifier.SYNCHRONIZED | Modifier.NATIVE | |
| 1766 | Modifier.ABSTRACT | Modifier.STRICT); |
| 1767 | if ((mods & Modifier.PRIVATE) == 0) { |
| 1768 | dout.writeUTF(sig.name); |
| 1769 | dout.writeInt(mods); |
| 1770 | dout.writeUTF(sig.signature.replace('/', '.')); |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | dout.flush(); |
| 1775 | |
| 1776 | MessageDigest md = MessageDigest.getInstance("SHA"); |
| 1777 | byte[] hashBytes = md.digest(bout.toByteArray()); |
| 1778 | long hash = 0; |
| 1779 | for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) { |
| 1780 | hash = (hash << 8) | (hashBytes[i] & 0xFF); |
| 1781 | } |
| 1782 | return hash; |
| 1783 | } catch (IOException ex) { |
| 1784 | throw new InternalError(); |
| 1785 | } catch (NoSuchAlgorithmException ex) { |
| 1786 | throw new SecurityException(ex.getMessage()); |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | /** |
| 1791 | * Returns true if the given class defines a static initializer method, |
| 1792 | * false otherwise. |
| 1793 | */ |
| 1794 | private native static boolean hasStaticInitializer(Class cl); |
| 1795 | |
| 1796 | /** |
| 1797 | * Class for computing and caching field/constructor/method signatures |
| 1798 | * during serialVersionUID calculation. |
| 1799 | */ |
| 1800 | private static class MemberSignature { |
| 1801 | |
| 1802 | public final Member member; |
| 1803 | public final String name; |
| 1804 | public final String signature; |
| 1805 | |
| 1806 | public MemberSignature(Field field) { |
| 1807 | member = field; |
| 1808 | name = field.getName(); |
| 1809 | signature = getClassSignature(field.getType()); |
| 1810 | } |
| 1811 | |
| 1812 | public MemberSignature(Constructor cons) { |
| 1813 | member = cons; |
| 1814 | name = cons.getName(); |
| 1815 | signature = getMethodSignature( |
| 1816 | cons.getParameterTypes(), Void.TYPE); |
| 1817 | } |
| 1818 | |
| 1819 | public MemberSignature(Method meth) { |
| 1820 | member = meth; |
| 1821 | name = meth.getName(); |
| 1822 | signature = getMethodSignature( |
| 1823 | meth.getParameterTypes(), meth.getReturnType()); |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | /** |
| 1828 | * Class for setting and retrieving serializable field values in batch. |
| 1829 | */ |
| 1830 | // REMIND: dynamically generate these? |
| 1831 | private static class FieldReflector { |
| 1832 | |
| 1833 | /** handle for performing unsafe operations */ |
| 1834 | private static final Unsafe unsafe = Unsafe.getUnsafe(); |
| 1835 | |
| 1836 | /** fields to operate on */ |
| 1837 | private final ObjectStreamField[] fields; |
| 1838 | /** number of primitive fields */ |
| 1839 | private final int numPrimFields; |
| 1840 | /** unsafe field keys */ |
| 1841 | private final long[] keys; |
| 1842 | /** field data offsets */ |
| 1843 | private final int[] offsets; |
| 1844 | /** field type codes */ |
| 1845 | private final char[] typeCodes; |
| 1846 | /** field types */ |
| 1847 | private final Class[] types; |
| 1848 | |
| 1849 | /** |
| 1850 | * Constructs FieldReflector capable of setting/getting values from the |
| 1851 | * subset of fields whose ObjectStreamFields contain non-null |
| 1852 | * reflective Field objects. ObjectStreamFields with null Fields are |
| 1853 | * treated as filler, for which get operations return default values |
| 1854 | * and set operations discard given values. |
| 1855 | */ |
| 1856 | FieldReflector(ObjectStreamField[] fields) { |
| 1857 | this.fields = fields; |
| 1858 | int nfields = fields.length; |
| 1859 | keys = new long[nfields]; |
| 1860 | offsets = new int[nfields]; |
| 1861 | typeCodes = new char[nfields]; |
| 1862 | ArrayList typeList = new ArrayList(); |
| 1863 | |
| 1864 | for (int i = 0; i < nfields; i++) { |
| 1865 | ObjectStreamField f = fields[i]; |
| 1866 | Field rf = f.getField(); |
| 1867 | keys[i] = (rf != null) ? |
| 1868 | unsafe.objectFieldOffset(rf) : Unsafe.INVALID_FIELD_OFFSET; |
| 1869 | offsets[i] = f.getOffset(); |
| 1870 | typeCodes[i] = f.getTypeCode(); |
| 1871 | if (!f.isPrimitive()) { |
| 1872 | typeList.add((rf != null) ? rf.getType() : null); |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | types = (Class[]) typeList.toArray(new Class[typeList.size()]); |
| 1877 | numPrimFields = nfields - types.length; |
| 1878 | } |
| 1879 | |
| 1880 | /** |
| 1881 | * Returns list of ObjectStreamFields representing fields operated on |
| 1882 | * by this reflector. The shared/unshared values and Field objects |
| 1883 | * contained by ObjectStreamFields in the list reflect their bindings |
| 1884 | * to locally defined serializable fields. |
| 1885 | */ |
| 1886 | ObjectStreamField[] getFields() { |
| 1887 | return fields; |
| 1888 | } |
| 1889 | |
| 1890 | /** |
| 1891 | * Fetches the serializable primitive field values of object obj and |
| 1892 | * marshals them into byte array buf starting at offset 0. The caller |
| 1893 | * is responsible for ensuring that obj is of the proper type. |
| 1894 | */ |
| 1895 | void getPrimFieldValues(Object obj, byte[] buf) { |
| 1896 | if (obj == null) { |
| 1897 | throw new NullPointerException(); |
| 1898 | } |
| 1899 | /* assuming checkDefaultSerialize() has been called on the class |
| 1900 | * descriptor this FieldReflector was obtained from, no field keys |
| 1901 | * in array should be equal to Unsafe.INVALID_FIELD_OFFSET. |
| 1902 | */ |
| 1903 | for (int i = 0; i < numPrimFields; i++) { |
| 1904 | long key = keys[i]; |
| 1905 | int off = offsets[i]; |
| 1906 | switch (typeCodes[i]) { |
| 1907 | case 'Z': |
| 1908 | Bits.putBoolean(buf, off, unsafe.getBoolean(obj, key)); |
| 1909 | break; |
| 1910 | |
| 1911 | case 'B': |
| 1912 | buf[off] = unsafe.getByte(obj, key); |
| 1913 | break; |
| 1914 | |
| 1915 | case 'C': |
| 1916 | Bits.putChar(buf, off, unsafe.getChar(obj, key)); |
| 1917 | break; |
| 1918 | |
| 1919 | case 'S': |
| 1920 | Bits.putShort(buf, off, unsafe.getShort(obj, key)); |
| 1921 | break; |
| 1922 | |
| 1923 | case 'I': |
| 1924 | Bits.putInt(buf, off, unsafe.getInt(obj, key)); |
| 1925 | break; |
| 1926 | |
| 1927 | case 'F': |
| 1928 | Bits.putFloat(buf, off, unsafe.getFloat(obj, key)); |
| 1929 | break; |
| 1930 | |
| 1931 | case 'J': |
| 1932 | Bits.putLong(buf, off, unsafe.getLong(obj, key)); |
| 1933 | break; |
| 1934 | |
| 1935 | case 'D': |
| 1936 | Bits.putDouble(buf, off, unsafe.getDouble(obj, key)); |
| 1937 | break; |
| 1938 | |
| 1939 | default: |
| 1940 | throw new InternalError(); |
| 1941 | } |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | /** |
| 1946 | * Sets the serializable primitive fields of object obj using values |
| 1947 | * unmarshalled from byte array buf starting at offset 0. The caller |
| 1948 | * is responsible for ensuring that obj is of the proper type. |
| 1949 | */ |
| 1950 | void setPrimFieldValues(Object obj, byte[] buf) { |
| 1951 | if (obj == null) { |
| 1952 | throw new NullPointerException(); |
| 1953 | } |
| 1954 | for (int i = 0; i < numPrimFields; i++) { |
| 1955 | long key = keys[i]; |
| 1956 | if (key == Unsafe.INVALID_FIELD_OFFSET) { |
| 1957 | continue; // discard value |
| 1958 | } |
| 1959 | int off = offsets[i]; |
| 1960 | switch (typeCodes[i]) { |
| 1961 | case 'Z': |
| 1962 | unsafe.putBoolean(obj, key, Bits.getBoolean(buf, off)); |
| 1963 | break; |
| 1964 | |
| 1965 | case 'B': |
| 1966 | unsafe.putByte(obj, key, buf[off]); |
| 1967 | break; |
| 1968 | |
| 1969 | case 'C': |
| 1970 | unsafe.putChar(obj, key, Bits.getChar(buf, off)); |
| 1971 | break; |
| 1972 | |
| 1973 | case 'S': |
| 1974 | unsafe.putShort(obj, key, Bits.getShort(buf, off)); |
| 1975 | break; |
| 1976 | |
| 1977 | case 'I': |
| 1978 | unsafe.putInt(obj, key, Bits.getInt(buf, off)); |
| 1979 | break; |
| 1980 | |
| 1981 | case 'F': |
| 1982 | unsafe.putFloat(obj, key, Bits.getFloat(buf, off)); |
| 1983 | break; |
| 1984 | |
| 1985 | case 'J': |
| 1986 | unsafe.putLong(obj, key, Bits.getLong(buf, off)); |
| 1987 | break; |
| 1988 | |
| 1989 | case 'D': |
| 1990 | unsafe.putDouble(obj, key, Bits.getDouble(buf, off)); |
| 1991 | break; |
| 1992 | |
| 1993 | default: |
| 1994 | throw new InternalError(); |
| 1995 | } |
| 1996 | } |
| 1997 | } |
| 1998 | |
| 1999 | /** |
| 2000 | * Fetches the serializable object field values of object obj and |
| 2001 | * stores them in array vals starting at offset 0. The caller is |
| 2002 | * responsible for ensuring that obj is of the proper type. |
| 2003 | */ |
| 2004 | void getObjFieldValues(Object obj, Object[] vals) { |
| 2005 | if (obj == null) { |
| 2006 | throw new NullPointerException(); |
| 2007 | } |
| 2008 | /* assuming checkDefaultSerialize() has been called on the class |
| 2009 | * descriptor this FieldReflector was obtained from, no field keys |
| 2010 | * in array should be equal to Unsafe.INVALID_FIELD_OFFSET. |
| 2011 | */ |
| 2012 | for (int i = numPrimFields; i < fields.length; i++) { |
| 2013 | switch (typeCodes[i]) { |
| 2014 | case 'L': |
| 2015 | case '[': |
| 2016 | vals[offsets[i]] = unsafe.getObject(obj, keys[i]); |
| 2017 | break; |
| 2018 | |
| 2019 | default: |
| 2020 | throw new InternalError(); |
| 2021 | } |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | /** |
| 2026 | * Sets the serializable object fields of object obj using values from |
| 2027 | * array vals starting at offset 0. The caller is responsible for |
| 2028 | * ensuring that obj is of the proper type; however, attempts to set a |
| 2029 | * field with a value of the wrong type will trigger an appropriate |
| 2030 | * ClassCastException. |
| 2031 | */ |
| 2032 | void setObjFieldValues(Object obj, Object[] vals) { |
| 2033 | if (obj == null) { |
| 2034 | throw new NullPointerException(); |
| 2035 | } |
| 2036 | for (int i = numPrimFields; i < fields.length; i++) { |
| 2037 | long key = keys[i]; |
| 2038 | if (key == Unsafe.INVALID_FIELD_OFFSET) { |
| 2039 | continue; // discard value |
| 2040 | } |
| 2041 | switch (typeCodes[i]) { |
| 2042 | case 'L': |
| 2043 | case '[': |
| 2044 | Object val = vals[offsets[i]]; |
| 2045 | if (val != null && |
| 2046 | !types[i - numPrimFields].isInstance(val)) |
| 2047 | { |
| 2048 | Field f = fields[i].getField(); |
| 2049 | throw new ClassCastException( |
| 2050 | "cannot assign instance of " + |
| 2051 | val.getClass().getName() + " to field " + |
| 2052 | f.getDeclaringClass().getName() + "." + |
| 2053 | f.getName() + " of type " + |
| 2054 | f.getType().getName() + " in instance of " + |
| 2055 | obj.getClass().getName()); |
| 2056 | } |
| 2057 | unsafe.putObject(obj, key, val); |
| 2058 | break; |
| 2059 | |
| 2060 | default: |
| 2061 | throw new InternalError(); |
| 2062 | } |
| 2063 | } |
| 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | /** |
| 2068 | * Matches given set of serializable fields with serializable fields |
| 2069 | * described by the given local class descriptor, and returns a |
| 2070 | * FieldReflector instance capable of setting/getting values from the |
| 2071 | * subset of fields that match (non-matching fields are treated as filler, |
| 2072 | * for which get operations return default values and set operations |
| 2073 | * discard given values). Throws InvalidClassException if unresolvable |
| 2074 | * type conflicts exist between the two sets of fields. |
| 2075 | */ |
| 2076 | private static FieldReflector getReflector(ObjectStreamField[] fields, |
| 2077 | ObjectStreamClass localDesc) |
| 2078 | throws InvalidClassException |
| 2079 | { |
| 2080 | // class irrelevant if no fields |
| 2081 | Class cl = (localDesc != null && fields.length > 0) ? |
| 2082 | localDesc.cl : null; |
| 2083 | processQueue(Caches.reflectorsQueue, Caches.reflectors); |
| 2084 | FieldReflectorKey key = new FieldReflectorKey(cl, fields, |
| 2085 | Caches.reflectorsQueue); |
| 2086 | Reference<?> ref = Caches.reflectors.get(key); |
| 2087 | Object entry = null; |
| 2088 | if (ref != null) { |
| 2089 | entry = ref.get(); |
| 2090 | } |
| 2091 | EntryFuture future = null; |
| 2092 | if (entry == null) { |
| 2093 | EntryFuture newEntry = new EntryFuture(); |
| 2094 | Reference<?> newRef = new SoftReference<EntryFuture>(newEntry); |
| 2095 | do { |
| 2096 | if (ref != null) { |
| 2097 | Caches.reflectors.remove(key, ref); |
| 2098 | } |
| 2099 | ref = Caches.reflectors.putIfAbsent(key, newRef); |
| 2100 | if (ref != null) { |
| 2101 | entry = ref.get(); |
| 2102 | } |
| 2103 | } while (ref != null && entry == null); |
| 2104 | if (entry == null) { |
| 2105 | future = newEntry; |
| 2106 | } |
| 2107 | } |
| 2108 | |
| 2109 | if (entry instanceof FieldReflector) { // check common case first |
| 2110 | return (FieldReflector) entry; |
| 2111 | } else if (entry instanceof EntryFuture) { |
| 2112 | entry = ((EntryFuture) entry).get(); |
| 2113 | } else if (entry == null) { |
| 2114 | try { |
| 2115 | entry = new FieldReflector(matchFields(fields, localDesc)); |
| 2116 | } catch (Throwable th) { |
| 2117 | entry = th; |
| 2118 | } |
| 2119 | future.set(entry); |
| 2120 | Caches.reflectors.put(key, new SoftReference<Object>(entry)); |
| 2121 | } |
| 2122 | |
| 2123 | if (entry instanceof FieldReflector) { |
| 2124 | return (FieldReflector) entry; |
| 2125 | } else if (entry instanceof InvalidClassException) { |
| 2126 | throw (InvalidClassException) entry; |
| 2127 | } else if (entry instanceof RuntimeException) { |
| 2128 | throw (RuntimeException) entry; |
| 2129 | } else if (entry instanceof Error) { |
| 2130 | throw (Error) entry; |
| 2131 | } else { |
| 2132 | throw new InternalError("unexpected entry: " + entry); |
| 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | /** |
| 2137 | * FieldReflector cache lookup key. Keys are considered equal if they |
| 2138 | * refer to the same class and equivalent field formats. |
| 2139 | */ |
| 2140 | private static class FieldReflectorKey extends WeakReference<Class<?>> { |
| 2141 | |
| 2142 | private final String sigs; |
| 2143 | private final int hash; |
| 2144 | private final boolean nullClass; |
| 2145 | |
| 2146 | FieldReflectorKey(Class cl, ObjectStreamField[] fields, |
| 2147 | ReferenceQueue<Class<?>> queue) |
| 2148 | { |
| 2149 | super(cl, queue); |
| 2150 | nullClass = (cl == null); |
| 2151 | StringBuilder sbuf = new StringBuilder(); |
| 2152 | for (int i = 0; i < fields.length; i++) { |
| 2153 | ObjectStreamField f = fields[i]; |
| 2154 | sbuf.append(f.getName()).append(f.getSignature()); |
| 2155 | } |
| 2156 | sigs = sbuf.toString(); |
| 2157 | hash = System.identityHashCode(cl) + sigs.hashCode(); |
| 2158 | } |
| 2159 | |
| 2160 | public int hashCode() { |
| 2161 | return hash; |
| 2162 | } |
| 2163 | |
| 2164 | public boolean equals(Object obj) { |
| 2165 | if (obj == this) { |
| 2166 | return true; |
| 2167 | } |
| 2168 | |
| 2169 | if (obj instanceof FieldReflectorKey) { |
| 2170 | FieldReflectorKey other = (FieldReflectorKey) obj; |
| 2171 | Class<?> referent; |
| 2172 | return (nullClass ? other.nullClass |
| 2173 | : ((referent = get()) != null) && |
| 2174 | (referent == other.get())) && |
| 2175 | sigs.equals(other.sigs); |
| 2176 | } else { |
| 2177 | return false; |
| 2178 | } |
| 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | /** |
| 2183 | * Matches given set of serializable fields with serializable fields |
| 2184 | * obtained from the given local class descriptor (which contain bindings |
| 2185 | * to reflective Field objects). Returns list of ObjectStreamFields in |
| 2186 | * which each ObjectStreamField whose signature matches that of a local |
| 2187 | * field contains a Field object for that field; unmatched |
| 2188 | * ObjectStreamFields contain null Field objects. Shared/unshared settings |
| 2189 | * of the returned ObjectStreamFields also reflect those of matched local |
| 2190 | * ObjectStreamFields. Throws InvalidClassException if unresolvable type |
| 2191 | * conflicts exist between the two sets of fields. |
| 2192 | */ |
| 2193 | private static ObjectStreamField[] matchFields(ObjectStreamField[] fields, |
| 2194 | ObjectStreamClass localDesc) |
| 2195 | throws InvalidClassException |
| 2196 | { |
| 2197 | ObjectStreamField[] localFields = (localDesc != null) ? |
| 2198 | localDesc.fields : NO_FIELDS; |
| 2199 | |
| 2200 | /* |
| 2201 | * Even if fields == localFields, we cannot simply return localFields |
| 2202 | * here. In previous implementations of serialization, |
| 2203 | * ObjectStreamField.getType() returned Object.class if the |
| 2204 | * ObjectStreamField represented a non-primitive field and belonged to |
| 2205 | * a non-local class descriptor. To preserve this (questionable) |
| 2206 | * behavior, the ObjectStreamField instances returned by matchFields |
| 2207 | * cannot report non-primitive types other than Object.class; hence |
| 2208 | * localFields cannot be returned directly. |
| 2209 | */ |
| 2210 | |
| 2211 | ObjectStreamField[] matches = new ObjectStreamField[fields.length]; |
| 2212 | for (int i = 0; i < fields.length; i++) { |
| 2213 | ObjectStreamField f = fields[i], m = null; |
| 2214 | for (int j = 0; j < localFields.length; j++) { |
| 2215 | ObjectStreamField lf = localFields[j]; |
| 2216 | if (f.getName().equals(lf.getName())) { |
| 2217 | if ((f.isPrimitive() || lf.isPrimitive()) && |
| 2218 | f.getTypeCode() != lf.getTypeCode()) |
| 2219 | { |
| 2220 | throw new InvalidClassException(localDesc.name, |
| 2221 | "incompatible types for field " + f.getName()); |
| 2222 | } |
| 2223 | if (lf.getField() != null) { |
| 2224 | m = new ObjectStreamField( |
| 2225 | lf.getField(), lf.isUnshared(), false); |
| 2226 | } else { |
| 2227 | m = new ObjectStreamField( |
| 2228 | lf.getName(), lf.getSignature(), lf.isUnshared()); |
| 2229 | } |
| 2230 | } |
| 2231 | } |
| 2232 | if (m == null) { |
| 2233 | m = new ObjectStreamField( |
| 2234 | f.getName(), f.getSignature(), false); |
| 2235 | } |
| 2236 | m.setOffset(f.getOffset()); |
| 2237 | matches[i] = m; |
| 2238 | } |
| 2239 | return matches; |
| 2240 | } |
| 2241 | |
| 2242 | /** |
| 2243 | * Removes from the specified map any keys that have been enqueued |
| 2244 | * on the specified reference queue. |
| 2245 | */ |
| 2246 | static void processQueue(ReferenceQueue<Class<?>> queue, |
| 2247 | ConcurrentMap<? extends |
| 2248 | WeakReference<Class<?>>, ?> map) |
| 2249 | { |
| 2250 | Reference<? extends Class<?>> ref; |
| 2251 | while((ref = queue.poll()) != null) { |
| 2252 | map.remove(ref); |
| 2253 | } |
| 2254 | } |
| 2255 | |
| 2256 | /** |
| 2257 | * Weak key for Class objects. |
| 2258 | * |
| 2259 | **/ |
| 2260 | static class WeakClassKey extends WeakReference<Class<?>> { |
| 2261 | /** |
| 2262 | * saved value of the referent's identity hash code, to maintain |
| 2263 | * a consistent hash code after the referent has been cleared |
| 2264 | */ |
| 2265 | private final int hash; |
| 2266 | |
| 2267 | /** |
| 2268 | * Create a new WeakClassKey to the given object, registered |
| 2269 | * with a queue. |
| 2270 | */ |
| 2271 | WeakClassKey(Class<?> cl, ReferenceQueue<Class<?>> refQueue) { |
| 2272 | super(cl, refQueue); |
| 2273 | hash = System.identityHashCode(cl); |
| 2274 | } |
| 2275 | |
| 2276 | /** |
| 2277 | * Returns the identity hash code of the original referent. |
| 2278 | */ |
| 2279 | public int hashCode() { |
| 2280 | return hash; |
| 2281 | } |
| 2282 | |
| 2283 | /** |
| 2284 | * Returns true if the given object is this identical |
| 2285 | * WeakClassKey instance, or, if this object's referent has not |
| 2286 | * been cleared, if the given object is another WeakClassKey |
| 2287 | * instance with the identical non-null referent as this one. |
| 2288 | */ |
| 2289 | public boolean equals(Object obj) { |
| 2290 | if (obj == this) { |
| 2291 | return true; |
| 2292 | } |
| 2293 | |
| 2294 | if (obj instanceof WeakClassKey) { |
| 2295 | Object referent = get(); |
| 2296 | return (referent != null) && |
| 2297 | (referent == ((WeakClassKey) obj).get()); |
| 2298 | } else { |
| 2299 | return false; |
| 2300 | } |
| 2301 | } |
| 2302 | } |
| 2303 | } |