< prev index next >

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

Print this page




2118             }
2119         }
2120 
2121         /**
2122          * Fetches the serializable object field values of object obj and
2123          * stores them in array vals starting at offset 0.  The caller is
2124          * responsible for ensuring that obj is of the proper type.
2125          */
2126         void getObjFieldValues(Object obj, Object[] vals) {
2127             if (obj == null) {
2128                 throw new NullPointerException();
2129             }
2130             /* assuming checkDefaultSerialize() has been called on the class
2131              * descriptor this FieldReflector was obtained from, no field keys
2132              * in array should be equal to Unsafe.INVALID_FIELD_OFFSET.
2133              */
2134             for (int i = numPrimFields; i < fields.length; i++) {
2135                 switch (typeCodes[i]) {
2136                     case 'L':
2137                     case '[':
2138                         vals[offsets[i]] = unsafe.getObject(obj, readKeys[i]);
2139                         break;
2140 
2141                     default:
2142                         throw new InternalError();
2143                 }
2144             }
2145         }
2146 
2147         /**
2148          * Checks that the given values, from array vals starting at offset 0,
2149          * are assignable to the given serializable object fields.
2150          * @throws ClassCastException if any value is not assignable
2151          */
2152         void checkObjectFieldValueTypes(Object obj, Object[] vals) {
2153             setObjFieldValues(obj, vals, true);
2154         }
2155 
2156         /**
2157          * Sets the serializable object fields of object obj using values from
2158          * array vals starting at offset 0.  The caller is responsible for


2173                 if (key == Unsafe.INVALID_FIELD_OFFSET) {
2174                     continue;           // discard value
2175                 }
2176                 switch (typeCodes[i]) {
2177                     case 'L':
2178                     case '[':
2179                         Object val = vals[offsets[i]];
2180                         if (val != null &&
2181                             !types[i - numPrimFields].isInstance(val))
2182                         {
2183                             Field f = fields[i].getField();
2184                             throw new ClassCastException(
2185                                 "cannot assign instance of " +
2186                                 val.getClass().getName() + " to field " +
2187                                 f.getDeclaringClass().getName() + "." +
2188                                 f.getName() + " of type " +
2189                                 f.getType().getName() + " in instance of " +
2190                                 obj.getClass().getName());
2191                         }
2192                         if (!dryRun)
2193                             unsafe.putObject(obj, key, val);
2194                         break;
2195 
2196                     default:
2197                         throw new InternalError();
2198                 }
2199             }
2200         }
2201     }
2202 
2203     /**
2204      * Matches given set of serializable fields with serializable fields
2205      * described by the given local class descriptor, and returns a
2206      * FieldReflector instance capable of setting/getting values from the
2207      * subset of fields that match (non-matching fields are treated as filler,
2208      * for which get operations return default values and set operations
2209      * discard given values).  Throws InvalidClassException if unresolvable
2210      * type conflicts exist between the two sets of fields.
2211      */
2212     private static FieldReflector getReflector(ObjectStreamField[] fields,
2213                                                ObjectStreamClass localDesc)




2118             }
2119         }
2120 
2121         /**
2122          * Fetches the serializable object field values of object obj and
2123          * stores them in array vals starting at offset 0.  The caller is
2124          * responsible for ensuring that obj is of the proper type.
2125          */
2126         void getObjFieldValues(Object obj, Object[] vals) {
2127             if (obj == null) {
2128                 throw new NullPointerException();
2129             }
2130             /* assuming checkDefaultSerialize() has been called on the class
2131              * descriptor this FieldReflector was obtained from, no field keys
2132              * in array should be equal to Unsafe.INVALID_FIELD_OFFSET.
2133              */
2134             for (int i = numPrimFields; i < fields.length; i++) {
2135                 switch (typeCodes[i]) {
2136                     case 'L':
2137                     case '[':
2138                         vals[offsets[i]] = unsafe.getReference(obj, readKeys[i]);
2139                         break;
2140 
2141                     default:
2142                         throw new InternalError();
2143                 }
2144             }
2145         }
2146 
2147         /**
2148          * Checks that the given values, from array vals starting at offset 0,
2149          * are assignable to the given serializable object fields.
2150          * @throws ClassCastException if any value is not assignable
2151          */
2152         void checkObjectFieldValueTypes(Object obj, Object[] vals) {
2153             setObjFieldValues(obj, vals, true);
2154         }
2155 
2156         /**
2157          * Sets the serializable object fields of object obj using values from
2158          * array vals starting at offset 0.  The caller is responsible for


2173                 if (key == Unsafe.INVALID_FIELD_OFFSET) {
2174                     continue;           // discard value
2175                 }
2176                 switch (typeCodes[i]) {
2177                     case 'L':
2178                     case '[':
2179                         Object val = vals[offsets[i]];
2180                         if (val != null &&
2181                             !types[i - numPrimFields].isInstance(val))
2182                         {
2183                             Field f = fields[i].getField();
2184                             throw new ClassCastException(
2185                                 "cannot assign instance of " +
2186                                 val.getClass().getName() + " to field " +
2187                                 f.getDeclaringClass().getName() + "." +
2188                                 f.getName() + " of type " +
2189                                 f.getType().getName() + " in instance of " +
2190                                 obj.getClass().getName());
2191                         }
2192                         if (!dryRun)
2193                             unsafe.putReference(obj, key, val);
2194                         break;
2195 
2196                     default:
2197                         throw new InternalError();
2198                 }
2199             }
2200         }
2201     }
2202 
2203     /**
2204      * Matches given set of serializable fields with serializable fields
2205      * described by the given local class descriptor, and returns a
2206      * FieldReflector instance capable of setting/getting values from the
2207      * subset of fields that match (non-matching fields are treated as filler,
2208      * for which get operations return default values and set operations
2209      * discard given values).  Throws InvalidClassException if unresolvable
2210      * type conflicts exist between the two sets of fields.
2211      */
2212     private static FieldReflector getReflector(ObjectStreamField[] fields,
2213                                                ObjectStreamClass localDesc)


< prev index next >