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

Print this page




1940                     default:
1941                         throw new InternalError();
1942                 }
1943             }
1944         }
1945 
1946         /**
1947          * Sets the serializable primitive fields of object obj using values
1948          * unmarshalled from byte array buf starting at offset 0.  The caller
1949          * is responsible for ensuring that obj is of the proper type.
1950          */
1951         void setPrimFieldValues(Object obj, byte[] buf) {
1952             if (obj == null) {
1953                 throw new NullPointerException();
1954             }
1955             for (int i = 0; i < numPrimFields; i++) {
1956                 long key = writeKeys[i];
1957                 if (key == Unsafe.INVALID_FIELD_OFFSET) {
1958                     continue;           // discard value
1959                 }
















1960                 int off = offsets[i];
1961                 switch (typeCodes[i]) {
1962                     case 'Z':
1963                         unsafe.putBoolean(obj, key, Bits.getBoolean(buf, off));
1964                         break;
1965 
1966                     case 'B':
1967                         unsafe.putByte(obj, key, buf[off]);
1968                         break;
1969 
1970                     case 'C':
1971                         unsafe.putChar(obj, key, Bits.getChar(buf, off));
1972                         break;
1973 
1974                     case 'S':
1975                         unsafe.putShort(obj, key, Bits.getShort(buf, off));
1976                         break;
1977 
1978                     case 'I':
1979                         unsafe.putInt(obj, key, Bits.getInt(buf, off));
1980                         break;
1981 
1982                     case 'F':
1983                         unsafe.putFloat(obj, key, Bits.getFloat(buf, off));
1984                         break;
1985 
1986                     case 'J':
1987                         unsafe.putLong(obj, key, Bits.getLong(buf, off));
1988                         break;
1989 
1990                     case 'D':
1991                         unsafe.putDouble(obj, key, Bits.getDouble(buf, off));
1992                         break;
1993 
1994                     default:
1995                         throw new InternalError();
1996                 }
1997             }












































1998         }

1999 
2000         /**
2001          * Fetches the serializable object field values of object obj and
2002          * stores them in array vals starting at offset 0.  The caller is
2003          * responsible for ensuring that obj is of the proper type.
2004          */
2005         void getObjFieldValues(Object obj, Object[] vals) {
2006             if (obj == null) {
2007                 throw new NullPointerException();
2008             }
2009             /* assuming checkDefaultSerialize() has been called on the class
2010              * descriptor this FieldReflector was obtained from, no field keys
2011              * in array should be equal to Unsafe.INVALID_FIELD_OFFSET.
2012              */
2013             for (int i = numPrimFields; i < fields.length; i++) {
2014                 switch (typeCodes[i]) {
2015                     case 'L':
2016                     case '[':
2017                         vals[offsets[i]] = unsafe.getObject(obj, readKeys[i]);
2018                         break;


2038                 long key = writeKeys[i];
2039                 if (key == Unsafe.INVALID_FIELD_OFFSET) {
2040                     continue;           // discard value
2041                 }
2042                 switch (typeCodes[i]) {
2043                     case 'L':
2044                     case '[':
2045                         Object val = vals[offsets[i]];
2046                         if (val != null &&
2047                             !types[i - numPrimFields].isInstance(val))
2048                         {
2049                             Field f = fields[i].getField();
2050                             throw new ClassCastException(
2051                                 "cannot assign instance of " +
2052                                 val.getClass().getName() + " to field " +
2053                                 f.getDeclaringClass().getName() + "." +
2054                                 f.getName() + " of type " +
2055                                 f.getType().getName() + " in instance of " +
2056                                 obj.getClass().getName());
2057                         }




2058                         unsafe.putObject(obj, key, val);

2059                         break;
2060 
2061                     default:
2062                         throw new InternalError();
2063                 }
2064             }
2065         }
2066     }
2067 
2068     /**
2069      * Matches given set of serializable fields with serializable fields
2070      * described by the given local class descriptor, and returns a
2071      * FieldReflector instance capable of setting/getting values from the
2072      * subset of fields that match (non-matching fields are treated as filler,
2073      * for which get operations return default values and set operations
2074      * discard given values).  Throws InvalidClassException if unresolvable
2075      * type conflicts exist between the two sets of fields.
2076      */
2077     private static FieldReflector getReflector(ObjectStreamField[] fields,
2078                                                ObjectStreamClass localDesc)




1940                     default:
1941                         throw new InternalError();
1942                 }
1943             }
1944         }
1945 
1946         /**
1947          * Sets the serializable primitive fields of object obj using values
1948          * unmarshalled from byte array buf starting at offset 0.  The caller
1949          * is responsible for ensuring that obj is of the proper type.
1950          */
1951         void setPrimFieldValues(Object obj, byte[] buf) {
1952             if (obj == null) {
1953                 throw new NullPointerException();
1954             }
1955             for (int i = 0; i < numPrimFields; i++) {
1956                 long key = writeKeys[i];
1957                 if (key == Unsafe.INVALID_FIELD_OFFSET) {
1958                     continue;           // discard value
1959                 }
1960                 int modifiers = fields[i].getField().getModifiers();
1961                 if (Modifier.isFinal(modifiers)) {
1962                     setFinalPrimFieldValue(obj, i, buf);
1963                 } else {
1964                     setPrimFieldValue(obj, i, buf);
1965                 }
1966             }
1967         }
1968 
1969         /**
1970          * Used by setPrimFieldValues to set a non-final primitive field of
1971          * object obj. The value is unmarshalled from byte arary buf.
1972          */
1973         void setPrimFieldValue(Object obj, int i, byte[] buf) {
1974             long key = writeKeys[i];
1975             assert key != Unsafe.INVALID_FIELD_OFFSET;
1976             int off = offsets[i];
1977             switch (typeCodes[i]) {
1978                 case 'Z':
1979                     unsafe.putBoolean(obj, key, Bits.getBoolean(buf, off));
1980                     break;
1981 
1982                 case 'B':
1983                     unsafe.putByte(obj, key, buf[off]);
1984                     break;
1985 
1986                 case 'C':
1987                     unsafe.putChar(obj, key, Bits.getChar(buf, off));
1988                     break;
1989 
1990                 case 'S':
1991                     unsafe.putShort(obj, key, Bits.getShort(buf, off));
1992                     break;
1993 
1994                 case 'I':
1995                     unsafe.putInt(obj, key, Bits.getInt(buf, off));
1996                     break;
1997 
1998                 case 'F':
1999                     unsafe.putFloat(obj, key, Bits.getFloat(buf, off));
2000                     break;
2001 
2002                 case 'J':
2003                     unsafe.putLong(obj, key, Bits.getLong(buf, off));
2004                     break;
2005 
2006                 case 'D':
2007                     unsafe.putDouble(obj, key, Bits.getDouble(buf, off));
2008                     break;
2009 
2010                 default:
2011                     throw new InternalError();
2012             }
2013         }
2014 
2015         /**
2016          * Used by setPrimFieldValues to set a final primitive field of
2017          * object obj. The value is unmarshalled from byte arary buf.
2018          */
2019         void setFinalPrimFieldValue(Object obj, int i, byte[] buf) {
2020             long key = writeKeys[i];
2021             assert key != Unsafe.INVALID_FIELD_OFFSET;
2022             int off = offsets[i];
2023             switch (typeCodes[i]) {
2024                 case 'Z':
2025                     unsafe.putBooleanVolatile(obj, key, Bits.getBoolean(buf, off));
2026                     break;
2027 
2028                 case 'B':
2029                     unsafe.putByteVolatile(obj, key, buf[off]);
2030                     break;
2031 
2032                 case 'C':
2033                     unsafe.putCharVolatile(obj, key, Bits.getChar(buf, off));
2034                     break;
2035 
2036                 case 'S':
2037                     unsafe.putShortVolatile(obj, key, Bits.getShort(buf, off));
2038                     break;
2039 
2040                 case 'I':
2041                     unsafe.putIntVolatile(obj, key, Bits.getInt(buf, off));
2042                     break;
2043 
2044                 case 'F':
2045                     unsafe.putFloatVolatile(obj, key, Bits.getFloat(buf, off));
2046                     break;
2047 
2048                 case 'J':
2049                     unsafe.putLongVolatile(obj, key, Bits.getLong(buf, off));
2050                     break;
2051 
2052                 case 'D':
2053                     unsafe.putDoubleVolatile(obj, key, Bits.getDouble(buf, off));
2054                     break;
2055 
2056                 default:
2057                     throw new InternalError();
2058             }
2059         }
2060 
2061         /**
2062          * Fetches the serializable object field values of object obj and
2063          * stores them in array vals starting at offset 0.  The caller is
2064          * responsible for ensuring that obj is of the proper type.
2065          */
2066         void getObjFieldValues(Object obj, Object[] vals) {
2067             if (obj == null) {
2068                 throw new NullPointerException();
2069             }
2070             /* assuming checkDefaultSerialize() has been called on the class
2071              * descriptor this FieldReflector was obtained from, no field keys
2072              * in array should be equal to Unsafe.INVALID_FIELD_OFFSET.
2073              */
2074             for (int i = numPrimFields; i < fields.length; i++) {
2075                 switch (typeCodes[i]) {
2076                     case 'L':
2077                     case '[':
2078                         vals[offsets[i]] = unsafe.getObject(obj, readKeys[i]);
2079                         break;


2099                 long key = writeKeys[i];
2100                 if (key == Unsafe.INVALID_FIELD_OFFSET) {
2101                     continue;           // discard value
2102                 }
2103                 switch (typeCodes[i]) {
2104                     case 'L':
2105                     case '[':
2106                         Object val = vals[offsets[i]];
2107                         if (val != null &&
2108                             !types[i - numPrimFields].isInstance(val))
2109                         {
2110                             Field f = fields[i].getField();
2111                             throw new ClassCastException(
2112                                 "cannot assign instance of " +
2113                                 val.getClass().getName() + " to field " +
2114                                 f.getDeclaringClass().getName() + "." +
2115                                 f.getName() + " of type " +
2116                                 f.getType().getName() + " in instance of " +
2117                                 obj.getClass().getName());
2118                         }
2119                         int modifiers = fields[i].getField().getModifiers();
2120                         if (Modifier.isFinal(modifiers)) {
2121                             unsafe.putObjectVolatile(obj, key, val);
2122                         } else {
2123                             unsafe.putObject(obj, key, val);
2124                         }
2125                         break;
2126 
2127                     default:
2128                         throw new InternalError();
2129                 }
2130             }
2131         }
2132     }
2133 
2134     /**
2135      * Matches given set of serializable fields with serializable fields
2136      * described by the given local class descriptor, and returns a
2137      * FieldReflector instance capable of setting/getting values from the
2138      * subset of fields that match (non-matching fields are treated as filler,
2139      * for which get operations return default values and set operations
2140      * discard given values).  Throws InvalidClassException if unresolvable
2141      * type conflicts exist between the two sets of fields.
2142      */
2143     private static FieldReflector getReflector(ObjectStreamField[] fields,
2144                                                ObjectStreamClass localDesc)