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

Print this page




1236     /**
1237      * Sets the serializable primitive fields of object obj using values
1238      * unmarshalled from byte array buf starting at offset 0.  It is the
1239      * responsibility of the caller to ensure that obj is of the proper type if
1240      * non-null.
1241      */
1242     void setPrimFieldValues(Object obj, byte[] buf) {
1243         fieldRefl.setPrimFieldValues(obj, buf);
1244     }
1245 
1246     /**
1247      * Fetches the serializable object field values of object obj and stores
1248      * them in array vals starting at offset 0.  It is the responsibility of
1249      * the caller to ensure that obj is of the proper type if non-null.
1250      */
1251     void getObjFieldValues(Object obj, Object[] vals) {
1252         fieldRefl.getObjFieldValues(obj, vals);
1253     }
1254 
1255     /**









1256      * Sets the serializable object fields of object obj using values from
1257      * array vals starting at offset 0.  It is the responsibility of the caller
1258      * to ensure that obj is of the proper type if non-null.
1259      */
1260     void setObjFieldValues(Object obj, Object[] vals) {
1261         fieldRefl.setObjFieldValues(obj, vals);
1262     }
1263 
1264     /**
1265      * Calculates and sets serializable field offsets, as well as primitive
1266      * data size and object field count totals.  Throws InvalidClassException
1267      * if fields are illegally ordered.
1268      */
1269     private void computeFieldOffsets() throws InvalidClassException {
1270         primDataSize = 0;
1271         numObjFields = 0;
1272         int firstObjIndex = -1;
1273 
1274         for (int i = 0; i < fields.length; i++) {
1275             ObjectStreamField f = fields[i];


2053                 throw new NullPointerException();
2054             }
2055             /* assuming checkDefaultSerialize() has been called on the class
2056              * descriptor this FieldReflector was obtained from, no field keys
2057              * in array should be equal to Unsafe.INVALID_FIELD_OFFSET.
2058              */
2059             for (int i = numPrimFields; i < fields.length; i++) {
2060                 switch (typeCodes[i]) {
2061                     case 'L':
2062                     case '[':
2063                         vals[offsets[i]] = unsafe.getObject(obj, readKeys[i]);
2064                         break;
2065 
2066                     default:
2067                         throw new InternalError();
2068                 }
2069             }
2070         }
2071 
2072         /**









2073          * Sets the serializable object fields of object obj using values from
2074          * array vals starting at offset 0.  The caller is responsible for
2075          * ensuring that obj is of the proper type; however, attempts to set a
2076          * field with a value of the wrong type will trigger an appropriate
2077          * ClassCastException.
2078          */
2079         void setObjFieldValues(Object obj, Object[] vals) {




2080             if (obj == null) {
2081                 throw new NullPointerException();
2082             }
2083             for (int i = numPrimFields; i < fields.length; i++) {
2084                 long key = writeKeys[i];
2085                 if (key == Unsafe.INVALID_FIELD_OFFSET) {
2086                     continue;           // discard value
2087                 }
2088                 switch (typeCodes[i]) {
2089                     case 'L':
2090                     case '[':
2091                         Object val = vals[offsets[i]];
2092                         if (val != null &&
2093                             !types[i - numPrimFields].isInstance(val))
2094                         {
2095                             Field f = fields[i].getField();
2096                             throw new ClassCastException(
2097                                 "cannot assign instance of " +
2098                                 val.getClass().getName() + " to field " +
2099                                 f.getDeclaringClass().getName() + "." +
2100                                 f.getName() + " of type " +
2101                                 f.getType().getName() + " in instance of " +
2102                                 obj.getClass().getName());
2103                         }

2104                         unsafe.putObject(obj, key, val);
2105                         break;
2106 
2107                     default:
2108                         throw new InternalError();
2109                 }
2110             }
2111         }
2112     }
2113 
2114     /**
2115      * Matches given set of serializable fields with serializable fields
2116      * described by the given local class descriptor, and returns a
2117      * FieldReflector instance capable of setting/getting values from the
2118      * subset of fields that match (non-matching fields are treated as filler,
2119      * for which get operations return default values and set operations
2120      * discard given values).  Throws InvalidClassException if unresolvable
2121      * type conflicts exist between the two sets of fields.
2122      */
2123     private static FieldReflector getReflector(ObjectStreamField[] fields,




1236     /**
1237      * Sets the serializable primitive fields of object obj using values
1238      * unmarshalled from byte array buf starting at offset 0.  It is the
1239      * responsibility of the caller to ensure that obj is of the proper type if
1240      * non-null.
1241      */
1242     void setPrimFieldValues(Object obj, byte[] buf) {
1243         fieldRefl.setPrimFieldValues(obj, buf);
1244     }
1245 
1246     /**
1247      * Fetches the serializable object field values of object obj and stores
1248      * them in array vals starting at offset 0.  It is the responsibility of
1249      * the caller to ensure that obj is of the proper type if non-null.
1250      */
1251     void getObjFieldValues(Object obj, Object[] vals) {
1252         fieldRefl.getObjFieldValues(obj, vals);
1253     }
1254 
1255     /**
1256      * Checks that the given values, from array vals starting at offset 0,
1257      * are assignable to the given serializable object fields.
1258      * @throws ClassCastException if any value is not assignable
1259      */
1260     void checkObjFieldValueTypes(Object obj, Object[] vals) {
1261         fieldRefl.checkObjectFieldValueTypes(obj, vals);
1262     }
1263 
1264     /**
1265      * Sets the serializable object fields of object obj using values from
1266      * array vals starting at offset 0.  It is the responsibility of the caller
1267      * to ensure that obj is of the proper type if non-null.
1268      */
1269     void setObjFieldValues(Object obj, Object[] vals) {
1270         fieldRefl.setObjFieldValues(obj, vals);
1271     }
1272 
1273     /**
1274      * Calculates and sets serializable field offsets, as well as primitive
1275      * data size and object field count totals.  Throws InvalidClassException
1276      * if fields are illegally ordered.
1277      */
1278     private void computeFieldOffsets() throws InvalidClassException {
1279         primDataSize = 0;
1280         numObjFields = 0;
1281         int firstObjIndex = -1;
1282 
1283         for (int i = 0; i < fields.length; i++) {
1284             ObjectStreamField f = fields[i];


2062                 throw new NullPointerException();
2063             }
2064             /* assuming checkDefaultSerialize() has been called on the class
2065              * descriptor this FieldReflector was obtained from, no field keys
2066              * in array should be equal to Unsafe.INVALID_FIELD_OFFSET.
2067              */
2068             for (int i = numPrimFields; i < fields.length; i++) {
2069                 switch (typeCodes[i]) {
2070                     case 'L':
2071                     case '[':
2072                         vals[offsets[i]] = unsafe.getObject(obj, readKeys[i]);
2073                         break;
2074 
2075                     default:
2076                         throw new InternalError();
2077                 }
2078             }
2079         }
2080 
2081         /**
2082          * Checks that the given values, from array vals starting at offset 0,
2083          * are assignable to the given serializable object fields.
2084          * @throws ClassCastException if any value is not assignable
2085          */
2086         void checkObjectFieldValueTypes(Object obj, Object[] vals) {
2087             setObjFieldValues(obj, vals, true);
2088         }
2089 
2090         /**
2091          * Sets the serializable object fields of object obj using values from
2092          * array vals starting at offset 0.  The caller is responsible for
2093          * ensuring that obj is of the proper type; however, attempts to set a
2094          * field with a value of the wrong type will trigger an appropriate
2095          * ClassCastException.
2096          */
2097         void setObjFieldValues(Object obj, Object[] vals) {
2098             setObjFieldValues(obj, vals, false);
2099         }
2100 
2101         private void setObjFieldValues(Object obj, Object[] vals, boolean dryRun) {
2102             if (obj == null) {
2103                 throw new NullPointerException();
2104             }
2105             for (int i = numPrimFields; i < fields.length; i++) {
2106                 long key = writeKeys[i];
2107                 if (key == Unsafe.INVALID_FIELD_OFFSET) {
2108                     continue;           // discard value
2109                 }
2110                 switch (typeCodes[i]) {
2111                     case 'L':
2112                     case '[':
2113                         Object val = vals[offsets[i]];
2114                         if (val != null &&
2115                             !types[i - numPrimFields].isInstance(val))
2116                         {
2117                             Field f = fields[i].getField();
2118                             throw new ClassCastException(
2119                                 "cannot assign instance of " +
2120                                 val.getClass().getName() + " to field " +
2121                                 f.getDeclaringClass().getName() + "." +
2122                                 f.getName() + " of type " +
2123                                 f.getType().getName() + " in instance of " +
2124                                 obj.getClass().getName());
2125                         }
2126                         if (!dryRun)
2127                             unsafe.putObject(obj, key, val);
2128                         break;
2129 
2130                     default:
2131                         throw new InternalError();
2132                 }
2133             }
2134         }
2135     }
2136 
2137     /**
2138      * Matches given set of serializable fields with serializable fields
2139      * described by the given local class descriptor, and returns a
2140      * FieldReflector instance capable of setting/getting values from the
2141      * subset of fields that match (non-matching fields are treated as filler,
2142      * for which get operations return default values and set operations
2143      * discard given values).  Throws InvalidClassException if unresolvable
2144      * type conflicts exist between the two sets of fields.
2145      */
2146     private static FieldReflector getReflector(ObjectStreamField[] fields,