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

Print this page




1235 
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++) {


2052             if (obj == null) {
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,




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


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