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

Print this page

        

@@ -1251,10 +1251,19 @@
     void getObjFieldValues(Object obj, Object[] vals) {
         fieldRefl.getObjFieldValues(obj, vals);
     }
 
     /**
+     * Checks that the given values, from array vals starting at offset 0,
+     * are assignable to the given serializable object fields.
+     * @throws ClassCastException if any value is not assignable
+     */
+    void checkObjFieldValueTypes(Object obj, Object[] vals) {
+        fieldRefl.checkObjectFieldValueTypes(obj, vals);
+    }
+
+    /**
      * Sets the serializable object fields of object obj using values from
      * array vals starting at offset 0.  It is the responsibility of the caller
      * to ensure that obj is of the proper type if non-null.
      */
     void setObjFieldValues(Object obj, Object[] vals) {

@@ -2068,17 +2077,30 @@
                 }
             }
         }
 
         /**
+         * Checks that the given values, from array vals starting at offset 0,
+         * are assignable to the given serializable object fields.
+         * @throws ClassCastException if any value is not assignable
+         */
+        void checkObjectFieldValueTypes(Object obj, Object[] vals) {
+            setObjFieldValues(obj, vals, true);
+        }
+
+        /**
          * Sets the serializable object fields of object obj using values from
          * array vals starting at offset 0.  The caller is responsible for
          * ensuring that obj is of the proper type; however, attempts to set a
          * field with a value of the wrong type will trigger an appropriate
          * ClassCastException.
          */
         void setObjFieldValues(Object obj, Object[] vals) {
+            setObjFieldValues(obj, vals, false);
+        }
+
+        private void setObjFieldValues(Object obj, Object[] vals, boolean dryRun) {
             if (obj == null) {
                 throw new NullPointerException();
             }
             for (int i = numPrimFields; i < fields.length; i++) {
                 long key = writeKeys[i];

@@ -2099,10 +2121,11 @@
                                 f.getDeclaringClass().getName() + "." +
                                 f.getName() + " of type " +
                                 f.getType().getName() + " in instance of " +
                                 obj.getClass().getName());
                         }
+                        if (!dryRun)
                         unsafe.putObject(obj, key, val);
                         break;
 
                     default:
                         throw new InternalError();