< prev index next >

## src/java.base/share/classes/java/io/ObjectStreamClass.java

Print this page

      76  *
      77  * <p>The algorithm to compute the SerialVersionUID is described in
      78  * <a href="{@docRoot}/../specs/serialization/class.html#stream-unique-identifiers">
      79  *     Object Serialization Specification, Section 4.6, Stream Unique Identifiers</a>.
      80  *
      81  * @author      Mike Warres
      82  * @author      Roger Riggs
      83  * @see ObjectStreamField
      84  * @see <a href="{@docRoot}/../specs/serialization/class.html">
      85  *     Object Serialization Specification, Section 4, Class Descriptors</a>
      86  * @since   1.1
      87  */
      88 public class ObjectStreamClass implements Serializable {
      89
      90     /** serialPersistentFields value indicating no serializable fields */
      91     public static final ObjectStreamField[] NO_FIELDS =
      92         new ObjectStreamField[0];
      93
      94     @java.io.Serial
      95     private static final long serialVersionUID = -6120832682080437368L;

      96     @java.io.Serial
      97     private static final ObjectStreamField[] serialPersistentFields =
      98         NO_FIELDS;
      99
     100     /** reflection factory for obtaining serialization constructors */
     101     private static final ReflectionFactory reflFactory =
     102         AccessController.doPrivileged(
     103             new ReflectionFactory.GetReflectionFactoryAction());
     104
     105     private static class Caches {
     106         /** cache mapping local classes -> descriptors */
     107         static final ConcurrentMap<WeakClassKey,Reference<?>> localDescs =
     108             new ConcurrentHashMap<>();
     109
     110         /** cache mapping field group/local desc pairs -> field reflectors */
     111         static final ConcurrentMap<FieldReflectorKey,Reference<?>> reflectors =
     112             new ConcurrentHashMap<>();
     113
     114         /** queue for WeakReferences to local classes */
     115         private static final ReferenceQueue<Class<?>> localDescsQueue =
    2521          * Returns true if the given object is this identical
    2522          * WeakClassKey instance, or, if this object's referent has not
    2523          * been cleared, if the given object is another WeakClassKey
    2524          * instance with the identical non-null referent as this one.
    2525          */
    2526         public boolean equals(Object obj) {
    2527             if (obj == this) {
    2528                 return true;
    2529             }
    2530
    2531             if (obj instanceof WeakClassKey) {
    2532                 Object referent = get();
    2533                 return (referent != null) &&
    2534                        (referent == ((WeakClassKey) obj).get());
    2535             } else {
    2536                 return false;
    2537             }
    2538         }
    2539     }
    2540
    2541     // a LRA cache of record deserialization constructors

    2542     @SuppressWarnings("serial")
    2543     private static final class DeserializationConstructorsCache
    2544         extends ConcurrentHashMap<DeserializationConstructorsCache.Key, MethodHandle>  {
    2545
    2546         // keep max. 10 cached entries - when the 11th element is inserted the oldest
    2547         // is removed and 10 remains - 11 is the biggest map size where internal
    2548         // table of 16 elements is sufficient (inserting 12th element would resize it to 32)
    2549         private static final int MAX_SIZE = 10;
    2550         private Key.Impl first, last; // first and last in FIFO queue
    2551
    2552         DeserializationConstructorsCache() {
    2553             // start small - if there is more than one shape of ObjectStreamClass
    2554             // deserialized, there will typically be two (current version and previous version)
    2555             super(2);
    2556         }
    2557
    2558         MethodHandle get(ObjectStreamField[] fields) {
    2559             return get(new Key.Lookup(fields));
    2560         }
    2561
      76  *
      77  * <p>The algorithm to compute the SerialVersionUID is described in
      78  * <a href="{@docRoot}/../specs/serialization/class.html#stream-unique-identifiers">
      79  *     Object Serialization Specification, Section 4.6, Stream Unique Identifiers</a>.
      80  *
      81  * @author      Mike Warres
      82  * @author      Roger Riggs
      83  * @see ObjectStreamField
      84  * @see <a href="{@docRoot}/../specs/serialization/class.html">
      85  *     Object Serialization Specification, Section 4, Class Descriptors</a>
      86  * @since   1.1
      87  */
      88 public class ObjectStreamClass implements Serializable {
      89
      90     /** serialPersistentFields value indicating no serializable fields */
      91     public static final ObjectStreamField[] NO_FIELDS =
      92         new ObjectStreamField[0];
      93
      94     @java.io.Serial
      95     private static final long serialVersionUID = -6120832682080437368L;
      96     /**
      97      * {@code ObjectStreamClass} has no fields for default serialization.
      98      */
      99     @java.io.Serial
     100     private static final ObjectStreamField[] serialPersistentFields =
     101         NO_FIELDS;
     102
     103     /** reflection factory for obtaining serialization constructors */
     104     private static final ReflectionFactory reflFactory =
     105         AccessController.doPrivileged(
     106             new ReflectionFactory.GetReflectionFactoryAction());
     107
     108     private static class Caches {
     109         /** cache mapping local classes -> descriptors */
     110         static final ConcurrentMap<WeakClassKey,Reference<?>> localDescs =
     111             new ConcurrentHashMap<>();
     112
     113         /** cache mapping field group/local desc pairs -> field reflectors */
     114         static final ConcurrentMap<FieldReflectorKey,Reference<?>> reflectors =
     115             new ConcurrentHashMap<>();
     116
     117         /** queue for WeakReferences to local classes */
     118         private static final ReferenceQueue<Class<?>> localDescsQueue =
    2524          * Returns true if the given object is this identical
    2525          * WeakClassKey instance, or, if this object's referent has not
    2526          * been cleared, if the given object is another WeakClassKey
    2527          * instance with the identical non-null referent as this one.
    2528          */
    2529         public boolean equals(Object obj) {
    2530             if (obj == this) {
    2531                 return true;
    2532             }
    2533
    2534             if (obj instanceof WeakClassKey) {
    2535                 Object referent = get();
    2536                 return (referent != null) &&
    2537                        (referent == ((WeakClassKey) obj).get());
    2538             } else {
    2539                 return false;
    2540             }
    2541         }
    2542     }
    2543
    2544     /**
    2545      * A LRA cache of record deserialization constructors.
    2546      */
    2547     @SuppressWarnings("serial")
    2548     private static final class DeserializationConstructorsCache
    2549         extends ConcurrentHashMap<DeserializationConstructorsCache.Key, MethodHandle>  {
    2550
    2551         // keep max. 10 cached entries - when the 11th element is inserted the oldest
    2552         // is removed and 10 remains - 11 is the biggest map size where internal
    2553         // table of 16 elements is sufficient (inserting 12th element would resize it to 32)
    2554         private static final int MAX_SIZE = 10;
    2555         private Key.Impl first, last; // first and last in FIFO queue
    2556
    2557         DeserializationConstructorsCache() {
    2558             // start small - if there is more than one shape of ObjectStreamClass
    2559             // deserialized, there will typically be two (current version and previous version)
    2560             super(2);
    2561         }
    2562
    2563         MethodHandle get(ObjectStreamField[] fields) {
    2564             return get(new Key.Lookup(fields));
    2565         }
    2566

< prev index next >