< prev index next >

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

Print this page

  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 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.invoke.MethodHandle;
  29 import java.lang.invoke.MethodHandles;
  30 import java.lang.invoke.MethodType;
  31 import java.lang.reflect.Constructor;
  32 import java.lang.reflect.Field;

  33 import java.lang.reflect.InvocationTargetException;
  34 import java.lang.reflect.RecordComponent;
  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.PrivilegedActionException;
  48 import java.security.PrivilegedExceptionAction;
  49 import java.security.ProtectionDomain;
  50 import java.util.ArrayList;
  51 import java.util.Arrays;
  52 import java.util.Collections;
  53 import java.util.Comparator;
  54 import java.util.HashSet;
  55 import java.util.Map;
  56 import java.util.Set;
  57 import java.util.concurrent.ConcurrentHashMap;
  58 import jdk.internal.misc.Unsafe;
  59 import jdk.internal.reflect.CallerSensitive;
  60 import jdk.internal.reflect.Reflection;
  61 import jdk.internal.reflect.ReflectionFactory;
  62 import jdk.internal.access.SharedSecrets;
  63 import jdk.internal.access.JavaSecurityAccess;

  64 import sun.reflect.misc.ReflectUtil;
  65 import static java.io.ObjectStreamField.*;
  66 
  67 /**
  68  * Serialization's descriptor for classes.  It contains the name and
  69  * serialVersionUID of the class.  The ObjectStreamClass for a specific class
  70  * loaded in this Java VM can be found/created using the lookup method.
  71  *
  72  * <p>The algorithm to compute the SerialVersionUID is described in
  73  * <a href="{@docRoot}/../specs/serialization/class.html#stream-unique-identifiers">
  74  *    <cite>Java Object Serialization Specification,</cite> Section 4.6, "Stream Unique Identifiers"</a>.
  75  *
  76  * @author      Mike Warres
  77  * @author      Roger Riggs
  78  * @see ObjectStreamField
  79  * @see <a href="{@docRoot}/../specs/serialization/class.html">
  80  *      <cite>Java Object Serialization Specification,</cite> Section 4, "Class Descriptors"</a>
  81  * @since   1.1
  82  */
  83 public final class ObjectStreamClass implements Serializable {

 426                     readResolveMethod = getInheritableMethod(
 427                         cl, "readResolve", null, Object.class);
 428                     return null;
 429                 }
 430             });
 431         } else {
 432             suid = 0L;
 433             fields = NO_FIELDS;
 434         }
 435 
 436         try {
 437             fieldRefl = getReflector(fields, this);
 438         } catch (InvalidClassException ex) {
 439             // field mismatches impossible when matching local fields vs. self
 440             throw new InternalError(ex);
 441         }
 442 
 443         if (deserializeEx == null) {
 444             if (isEnum) {
 445                 deserializeEx = new ExceptionInfo(name, "enum type");


 446             } else if (cons == null && !isRecord) {
 447                 deserializeEx = new ExceptionInfo(name, "no valid constructor");
 448             }
 449         }
 450         if (isRecord && canonicalCtr == null) {
 451             deserializeEx = new ExceptionInfo(name, "record canonical constructor not found");
 452         } else {
 453             for (int i = 0; i < fields.length; i++) {
 454                 if (fields[i].getField() == null) {
 455                     defaultSerializeEx = new ExceptionInfo(
 456                         name, "unmatched serializable field(s) declared");
 457                 }
 458             }
 459         }
 460         initialized = true;
 461     }
 462 
 463     /**
 464      * Creates blank class descriptor which should be initialized via a
 465      * subsequent call to initProxy(), initNonProxy() or readNonProxy().

1413         ObjectStreamClass desc = new ObjectStreamClass();
1414         if (isProxy) {
1415             desc.initProxy(cl, null, superDesc);
1416         } else {
1417             desc.initNonProxy(this, cl, null, superDesc);
1418         }
1419         return desc;
1420     }
1421 
1422     /**
1423      * Returns public no-arg constructor of given class, or null if none found.
1424      * Access checks are disabled on the returned constructor (if any), since
1425      * the defining class may still be non-public.
1426      */
1427     private static Constructor<?> getExternalizableConstructor(Class<?> cl) {
1428         try {
1429             Constructor<?> cons = cl.getDeclaredConstructor((Class<?>[]) null);
1430             cons.setAccessible(true);
1431             return ((cons.getModifiers() & Modifier.PUBLIC) != 0) ?
1432                 cons : null;
1433         } catch (NoSuchMethodException ex) {
1434             return null;
1435         }
1436     }
1437 
1438     /**
1439      * Returns subclass-accessible no-arg constructor of first non-serializable
1440      * superclass, or null if none found.  Access checks are disabled on the
1441      * returned constructor (if any).
1442      */
1443     private static Constructor<?> getSerializableConstructor(Class<?> cl) {
1444         return reflFactory.newConstructorForSerialization(cl);
1445     }
1446 
1447     /**
1448      * Returns the canonical constructor for the given record class, or null if
1449      * the not found ( which should never happen for correctly generated record
1450      * classes ).
1451      */
1452     @SuppressWarnings("removal")
1453     private static MethodHandle canonicalRecordCtr(Class<?> cls) {

  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 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.invoke.MethodHandle;
  29 import java.lang.invoke.MethodHandles;
  30 import java.lang.invoke.MethodType;
  31 import java.lang.reflect.Constructor;
  32 import java.lang.reflect.Field;
  33 import java.lang.reflect.InaccessibleObjectException;
  34 import java.lang.reflect.InvocationTargetException;
  35 import java.lang.reflect.RecordComponent;
  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.PrivilegedActionException;
  49 import java.security.PrivilegedExceptionAction;
  50 import java.security.ProtectionDomain;
  51 import java.util.ArrayList;
  52 import java.util.Arrays;
  53 import java.util.Collections;
  54 import java.util.Comparator;
  55 import java.util.HashSet;
  56 import java.util.Map;
  57 import java.util.Set;
  58 import java.util.concurrent.ConcurrentHashMap;
  59 import jdk.internal.misc.Unsafe;
  60 import jdk.internal.reflect.CallerSensitive;
  61 import jdk.internal.reflect.Reflection;
  62 import jdk.internal.reflect.ReflectionFactory;
  63 import jdk.internal.access.SharedSecrets;
  64 import jdk.internal.access.JavaSecurityAccess;
  65 import jdk.internal.value.PrimitiveClass;
  66 import sun.reflect.misc.ReflectUtil;
  67 import static java.io.ObjectStreamField.*;
  68 
  69 /**
  70  * Serialization's descriptor for classes.  It contains the name and
  71  * serialVersionUID of the class.  The ObjectStreamClass for a specific class
  72  * loaded in this Java VM can be found/created using the lookup method.
  73  *
  74  * <p>The algorithm to compute the SerialVersionUID is described in
  75  * <a href="{@docRoot}/../specs/serialization/class.html#stream-unique-identifiers">
  76  *    <cite>Java Object Serialization Specification,</cite> Section 4.6, "Stream Unique Identifiers"</a>.
  77  *
  78  * @author      Mike Warres
  79  * @author      Roger Riggs
  80  * @see ObjectStreamField
  81  * @see <a href="{@docRoot}/../specs/serialization/class.html">
  82  *      <cite>Java Object Serialization Specification,</cite> Section 4, "Class Descriptors"</a>
  83  * @since   1.1
  84  */
  85 public final class ObjectStreamClass implements Serializable {

 428                     readResolveMethod = getInheritableMethod(
 429                         cl, "readResolve", null, Object.class);
 430                     return null;
 431                 }
 432             });
 433         } else {
 434             suid = 0L;
 435             fields = NO_FIELDS;
 436         }
 437 
 438         try {
 439             fieldRefl = getReflector(fields, this);
 440         } catch (InvalidClassException ex) {
 441             // field mismatches impossible when matching local fields vs. self
 442             throw new InternalError(ex);
 443         }
 444 
 445         if (deserializeEx == null) {
 446             if (isEnum) {
 447                 deserializeEx = new ExceptionInfo(name, "enum type");
 448             } else if (cl.isValue() && writeReplaceMethod == null) {
 449                 deserializeEx = new ExceptionInfo(name, "value class");
 450             } else if (cons == null && !isRecord) {
 451                 deserializeEx = new ExceptionInfo(name, "no valid constructor");
 452             }
 453         }
 454         if (isRecord && canonicalCtr == null) {
 455             deserializeEx = new ExceptionInfo(name, "record canonical constructor not found");
 456         } else {
 457             for (int i = 0; i < fields.length; i++) {
 458                 if (fields[i].getField() == null) {
 459                     defaultSerializeEx = new ExceptionInfo(
 460                         name, "unmatched serializable field(s) declared");
 461                 }
 462             }
 463         }
 464         initialized = true;
 465     }
 466 
 467     /**
 468      * Creates blank class descriptor which should be initialized via a
 469      * subsequent call to initProxy(), initNonProxy() or readNonProxy().

1417         ObjectStreamClass desc = new ObjectStreamClass();
1418         if (isProxy) {
1419             desc.initProxy(cl, null, superDesc);
1420         } else {
1421             desc.initNonProxy(this, cl, null, superDesc);
1422         }
1423         return desc;
1424     }
1425 
1426     /**
1427      * Returns public no-arg constructor of given class, or null if none found.
1428      * Access checks are disabled on the returned constructor (if any), since
1429      * the defining class may still be non-public.
1430      */
1431     private static Constructor<?> getExternalizableConstructor(Class<?> cl) {
1432         try {
1433             Constructor<?> cons = cl.getDeclaredConstructor((Class<?>[]) null);
1434             cons.setAccessible(true);
1435             return ((cons.getModifiers() & Modifier.PUBLIC) != 0) ?
1436                 cons : null;
1437         } catch (NoSuchMethodException | InaccessibleObjectException ex) {
1438             return null;
1439         }
1440     }
1441 
1442     /**
1443      * Returns subclass-accessible no-arg constructor of first non-serializable
1444      * superclass, or null if none found.  Access checks are disabled on the
1445      * returned constructor (if any).
1446      */
1447     private static Constructor<?> getSerializableConstructor(Class<?> cl) {
1448         return reflFactory.newConstructorForSerialization(cl);
1449     }
1450 
1451     /**
1452      * Returns the canonical constructor for the given record class, or null if
1453      * the not found ( which should never happen for correctly generated record
1454      * classes ).
1455      */
1456     @SuppressWarnings("removal")
1457     private static MethodHandle canonicalRecordCtr(Class<?> cls) {
< prev index next >