< 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;

 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;

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

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