< prev index next >

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

Print this page




  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  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.UndeclaredThrowableException;
  36 import java.lang.reflect.Member;
  37 import java.lang.reflect.Method;
  38 import java.lang.reflect.Modifier;
  39 import java.lang.reflect.Proxy;
  40 import java.security.AccessControlContext;
  41 import java.security.AccessController;
  42 import java.security.MessageDigest;
  43 import java.security.NoSuchAlgorithmException;
  44 import java.security.PermissionCollection;
  45 import java.security.Permissions;
  46 import java.security.PrivilegedAction;
  47 import java.security.ProtectionDomain;
  48 import java.util.ArrayList;
  49 import java.util.Arrays;
  50 import java.util.Collections;
  51 import java.util.Comparator;
  52 import java.util.HashSet;
  53 import java.util.Set;


 456             }
 457             return entry;
 458         }
 459 
 460         /**
 461          * Returns the thread that created this EntryFuture.
 462          */
 463         Thread getOwner() {
 464             return owner;
 465         }
 466     }
 467 
 468     /**
 469      * Creates local class descriptor representing given class.
 470      */
 471     private ObjectStreamClass(final Class<?> cl) {
 472         this.cl = cl;
 473         name = cl.getName();
 474         isProxy = Proxy.isProxyClass(cl);
 475         isEnum = Enum.class.isAssignableFrom(cl);

 476         serializable = Serializable.class.isAssignableFrom(cl);
 477         externalizable = Externalizable.class.isAssignableFrom(cl);
 478 
 479         Class<?> superCl = cl.getSuperclass();
 480         superDesc = (superCl != null) ? lookup(superCl, false) : null;
 481         localDesc = this;
 482 
 483         if (serializable) {
 484             AccessController.doPrivileged(new PrivilegedAction<>() {
 485                 public Void run() {
 486                     if (isEnum) {
 487                         suid = Long.valueOf(0);
 488                         fields = NO_FIELDS;
 489                         return null;
 490                     }
 491                     if (cl.isArray()) {
 492                         fields = NO_FIELDS;
 493                         return null;
 494                     }
 495 


 523                     readResolveMethod = getInheritableMethod(
 524                         cl, "readResolve", null, Object.class);
 525                     return null;
 526                 }
 527             });
 528         } else {
 529             suid = Long.valueOf(0);
 530             fields = NO_FIELDS;
 531         }
 532 
 533         try {
 534             fieldRefl = getReflector(fields, this);
 535         } catch (InvalidClassException ex) {
 536             // field mismatches impossible when matching local fields vs. self
 537             throw new InternalError(ex);
 538         }
 539 
 540         if (deserializeEx == null) {
 541             if (isEnum) {
 542                 deserializeEx = new ExceptionInfo(name, "enum type");


 543             } else if (cons == null) {
 544                 deserializeEx = new ExceptionInfo(name, "no valid constructor");
 545             }
 546         }
 547         for (int i = 0; i < fields.length; i++) {
 548             if (fields[i].getField() == null) {
 549                 defaultSerializeEx = new ExceptionInfo(
 550                     name, "unmatched serializable field(s) declared");
 551             }
 552         }
 553         initialized = true;
 554     }
 555 
 556     /**
 557      * Creates blank class descriptor which should be initialized via a
 558      * subsequent call to initProxy(), initNonProxy() or readNonProxy().
 559      */
 560     ObjectStreamClass() {
 561     }
 562 


1485         ObjectStreamClass desc = new ObjectStreamClass();
1486         if (isProxy) {
1487             desc.initProxy(cl, null, superDesc);
1488         } else {
1489             desc.initNonProxy(this, cl, null, superDesc);
1490         }
1491         return desc;
1492     }
1493 
1494     /**
1495      * Returns public no-arg constructor of given class, or null if none found.
1496      * Access checks are disabled on the returned constructor (if any), since
1497      * the defining class may still be non-public.
1498      */
1499     private static Constructor<?> getExternalizableConstructor(Class<?> cl) {
1500         try {
1501             Constructor<?> cons = cl.getDeclaredConstructor((Class<?>[]) null);
1502             cons.setAccessible(true);
1503             return ((cons.getModifiers() & Modifier.PUBLIC) != 0) ?
1504                 cons : null;
1505         } catch (NoSuchMethodException ex) {
1506             return null;
1507         }
1508     }
1509 
1510     /**
1511      * Returns subclass-accessible no-arg constructor of first non-serializable
1512      * superclass, or null if none found.  Access checks are disabled on the
1513      * returned constructor (if any).
1514      */
1515     private static Constructor<?> getSerializableConstructor(Class<?> cl) {
1516         return reflFactory.newConstructorForSerialization(cl);
1517     }
1518 
1519     /**
1520      * Returns non-static, non-abstract method with given signature provided it
1521      * is defined by or accessible (via inheritance) by the given class, or
1522      * null if no match found.  Access checks are disabled on the returned
1523      * method (if any).
1524      */
1525     private static Method getInheritableMethod(Class<?> cl, String name,




  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  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.InaccessibleObjectException;
  35 import java.lang.reflect.InvocationTargetException;
  36 import java.lang.reflect.UndeclaredThrowableException;
  37 import java.lang.reflect.Member;
  38 import java.lang.reflect.Method;
  39 import java.lang.reflect.Modifier;
  40 import java.lang.reflect.Proxy;
  41 import java.security.AccessControlContext;
  42 import java.security.AccessController;
  43 import java.security.MessageDigest;
  44 import java.security.NoSuchAlgorithmException;
  45 import java.security.PermissionCollection;
  46 import java.security.Permissions;
  47 import java.security.PrivilegedAction;
  48 import java.security.ProtectionDomain;
  49 import java.util.ArrayList;
  50 import java.util.Arrays;
  51 import java.util.Collections;
  52 import java.util.Comparator;
  53 import java.util.HashSet;
  54 import java.util.Set;


 457             }
 458             return entry;
 459         }
 460 
 461         /**
 462          * Returns the thread that created this EntryFuture.
 463          */
 464         Thread getOwner() {
 465             return owner;
 466         }
 467     }
 468 
 469     /**
 470      * Creates local class descriptor representing given class.
 471      */
 472     private ObjectStreamClass(final Class<?> cl) {
 473         this.cl = cl;
 474         name = cl.getName();
 475         isProxy = Proxy.isProxyClass(cl);
 476         isEnum = Enum.class.isAssignableFrom(cl);
 477         boolean isInlineClass = cl.isValue();
 478         serializable = Serializable.class.isAssignableFrom(cl);
 479         externalizable = Externalizable.class.isAssignableFrom(cl);
 480 
 481         Class<?> superCl = cl.getSuperclass();
 482         superDesc = (superCl != null) ? lookup(superCl, false) : null;
 483         localDesc = this;
 484 
 485         if (serializable) {
 486             AccessController.doPrivileged(new PrivilegedAction<>() {
 487                 public Void run() {
 488                     if (isEnum) {
 489                         suid = Long.valueOf(0);
 490                         fields = NO_FIELDS;
 491                         return null;
 492                     }
 493                     if (cl.isArray()) {
 494                         fields = NO_FIELDS;
 495                         return null;
 496                     }
 497 


 525                     readResolveMethod = getInheritableMethod(
 526                         cl, "readResolve", null, Object.class);
 527                     return null;
 528                 }
 529             });
 530         } else {
 531             suid = Long.valueOf(0);
 532             fields = NO_FIELDS;
 533         }
 534 
 535         try {
 536             fieldRefl = getReflector(fields, this);
 537         } catch (InvalidClassException ex) {
 538             // field mismatches impossible when matching local fields vs. self
 539             throw new InternalError(ex);
 540         }
 541 
 542         if (deserializeEx == null) {
 543             if (isEnum) {
 544                 deserializeEx = new ExceptionInfo(name, "enum type");
 545             } else if (isInlineClass && writeReplaceMethod == null) {
 546                 deserializeEx = new ExceptionInfo(name, "inline class");
 547             } else if (cons == null) {
 548                 deserializeEx = new ExceptionInfo(name, "no valid constructor");
 549             }
 550         }
 551         for (int i = 0; i < fields.length; i++) {
 552             if (fields[i].getField() == null) {
 553                 defaultSerializeEx = new ExceptionInfo(
 554                     name, "unmatched serializable field(s) declared");
 555             }
 556         }
 557         initialized = true;
 558     }
 559 
 560     /**
 561      * Creates blank class descriptor which should be initialized via a
 562      * subsequent call to initProxy(), initNonProxy() or readNonProxy().
 563      */
 564     ObjectStreamClass() {
 565     }
 566 


1489         ObjectStreamClass desc = new ObjectStreamClass();
1490         if (isProxy) {
1491             desc.initProxy(cl, null, superDesc);
1492         } else {
1493             desc.initNonProxy(this, cl, null, superDesc);
1494         }
1495         return desc;
1496     }
1497 
1498     /**
1499      * Returns public no-arg constructor of given class, or null if none found.
1500      * Access checks are disabled on the returned constructor (if any), since
1501      * the defining class may still be non-public.
1502      */
1503     private static Constructor<?> getExternalizableConstructor(Class<?> cl) {
1504         try {
1505             Constructor<?> cons = cl.getDeclaredConstructor((Class<?>[]) null);
1506             cons.setAccessible(true);
1507             return ((cons.getModifiers() & Modifier.PUBLIC) != 0) ?
1508                 cons : null;
1509         } catch (NoSuchMethodException | InaccessibleObjectException ex) {
1510             return null;
1511         }
1512     }
1513 
1514     /**
1515      * Returns subclass-accessible no-arg constructor of first non-serializable
1516      * superclass, or null if none found.  Access checks are disabled on the
1517      * returned constructor (if any).
1518      */
1519     private static Constructor<?> getSerializableConstructor(Class<?> cl) {
1520         return reflFactory.newConstructorForSerialization(cl);
1521     }
1522 
1523     /**
1524      * Returns non-static, non-abstract method with given signature provided it
1525      * is defined by or accessible (via inheritance) by the given class, or
1526      * null if no match found.  Access checks are disabled on the returned
1527      * method (if any).
1528      */
1529     private static Method getInheritableMethod(Class<?> cl, String name,


< prev index next >