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]; 1276 switch (f.getTypeCode()) { 1277 case 'Z': 1278 case 'B': 1279 f.setOffset(primDataSize++); 1280 break; 1281 1282 case 'C': 1283 case 'S': 1284 f.setOffset(primDataSize); 1869 // REMIND: dynamically generate these? 1870 private static class FieldReflector { 1871 1872 /** handle for performing unsafe operations */ 1873 private static final Unsafe unsafe = Unsafe.getUnsafe(); 1874 1875 /** fields to operate on */ 1876 private final ObjectStreamField[] fields; 1877 /** number of primitive fields */ 1878 private final int numPrimFields; 1879 /** unsafe field keys for reading fields - may contain dupes */ 1880 private final long[] readKeys; 1881 /** unsafe fields keys for writing fields - no dupes */ 1882 private final long[] writeKeys; 1883 /** field data offsets */ 1884 private final int[] offsets; 1885 /** field type codes */ 1886 private final char[] typeCodes; 1887 /** field types */ 1888 private final Class<?>[] types; 1889 1890 /** 1891 * Constructs FieldReflector capable of setting/getting values from the 1892 * subset of fields whose ObjectStreamFields contain non-null 1893 * reflective Field objects. ObjectStreamFields with null Fields are 1894 * treated as filler, for which get operations return default values 1895 * and set operations discard given values. 1896 */ 1897 FieldReflector(ObjectStreamField[] fields) { 1898 this.fields = fields; 1899 int nfields = fields.length; 1900 readKeys = new long[nfields]; 1901 writeKeys = new long[nfields]; 1902 offsets = new int[nfields]; 1903 typeCodes = new char[nfields]; 1904 ArrayList<Class<?>> typeList = new ArrayList<>(); 1905 Set<Long> usedKeys = new HashSet<>(); 1906 1907 1908 for (int i = 0; i < nfields; i++) { 1909 ObjectStreamField f = fields[i]; 1910 Field rf = f.getField(); 1911 long key = (rf != null) ? 1912 unsafe.objectFieldOffset(rf) : Unsafe.INVALID_FIELD_OFFSET; 1913 readKeys[i] = key; 1914 writeKeys[i] = usedKeys.add(key) ? 1915 key : Unsafe.INVALID_FIELD_OFFSET; 1916 offsets[i] = f.getOffset(); 1917 typeCodes[i] = f.getTypeCode(); 1918 if (!f.isPrimitive()) { 1919 typeList.add((rf != null) ? rf.getType() : null); 1920 } 1921 } 1922 1923 types = typeList.toArray(new Class<?>[typeList.size()]); 1924 numPrimFields = nfields - types.length; 1925 } 1926 1927 /** 1928 * Returns list of ObjectStreamFields representing fields operated on 1929 * by this reflector. The shared/unshared values and Field objects 1930 * contained by ObjectStreamFields in the list reflect their bindings 1931 * to locally defined serializable fields. 1932 */ 1933 ObjectStreamField[] getFields() { 1934 return fields; 1935 } 1936 1937 /** 1938 * Fetches the serializable primitive field values of object obj and 1939 * marshals them into byte array buf starting at offset 0. The caller 1940 * is responsible for ensuring that obj is of the proper type. 1941 */ 1942 void getPrimFieldValues(Object obj, byte[] buf) { 1943 if (obj == null) { 1944 throw new NullPointerException(); 1945 } 1946 /* assuming checkDefaultSerialize() has been called on the class | 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 * Returns true if the field reflector has one, or more, underlying final 1266 * fields. 1267 */ 1268 boolean hasFinalField() { 1269 return fieldRefl.hasFinal(); 1270 } 1271 1272 /** 1273 * Calculates and sets serializable field offsets, as well as primitive 1274 * data size and object field count totals. Throws InvalidClassException 1275 * if fields are illegally ordered. 1276 */ 1277 private void computeFieldOffsets() throws InvalidClassException { 1278 primDataSize = 0; 1279 numObjFields = 0; 1280 int firstObjIndex = -1; 1281 1282 for (int i = 0; i < fields.length; i++) { 1283 ObjectStreamField f = fields[i]; 1284 switch (f.getTypeCode()) { 1285 case 'Z': 1286 case 'B': 1287 f.setOffset(primDataSize++); 1288 break; 1289 1290 case 'C': 1291 case 'S': 1292 f.setOffset(primDataSize); 1877 // REMIND: dynamically generate these? 1878 private static class FieldReflector { 1879 1880 /** handle for performing unsafe operations */ 1881 private static final Unsafe unsafe = Unsafe.getUnsafe(); 1882 1883 /** fields to operate on */ 1884 private final ObjectStreamField[] fields; 1885 /** number of primitive fields */ 1886 private final int numPrimFields; 1887 /** unsafe field keys for reading fields - may contain dupes */ 1888 private final long[] readKeys; 1889 /** unsafe fields keys for writing fields - no dupes */ 1890 private final long[] writeKeys; 1891 /** field data offsets */ 1892 private final int[] offsets; 1893 /** field type codes */ 1894 private final char[] typeCodes; 1895 /** field types */ 1896 private final Class<?>[] types; 1897 /** true if at least one underlying field is final */ 1898 private final boolean hasFinal; 1899 1900 /** 1901 * Constructs FieldReflector capable of setting/getting values from the 1902 * subset of fields whose ObjectStreamFields contain non-null 1903 * reflective Field objects. ObjectStreamFields with null Fields are 1904 * treated as filler, for which get operations return default values 1905 * and set operations discard given values. 1906 */ 1907 FieldReflector(ObjectStreamField[] fields) { 1908 this.fields = fields; 1909 int nfields = fields.length; 1910 readKeys = new long[nfields]; 1911 writeKeys = new long[nfields]; 1912 offsets = new int[nfields]; 1913 typeCodes = new char[nfields]; 1914 ArrayList<Class<?>> typeList = new ArrayList<>(); 1915 Set<Long> usedKeys = new HashSet<>(); 1916 1917 boolean hf = false; 1918 for (int i = 0; i < nfields; i++) { 1919 ObjectStreamField f = fields[i]; 1920 Field rf = f.getField(); 1921 long key; 1922 if (rf != null) { 1923 key = unsafe.objectFieldOffset(rf); 1924 if (!hf && isFinal(rf)) { 1925 hf = true; 1926 } 1927 } else { 1928 key = Unsafe.INVALID_FIELD_OFFSET; 1929 } 1930 readKeys[i] = key; 1931 writeKeys[i] = usedKeys.add(key) ? 1932 key : Unsafe.INVALID_FIELD_OFFSET; 1933 offsets[i] = f.getOffset(); 1934 typeCodes[i] = f.getTypeCode(); 1935 if (!f.isPrimitive()) { 1936 typeList.add((rf != null) ? rf.getType() : null); 1937 } 1938 } 1939 1940 hasFinal = hf; 1941 types = typeList.toArray(new Class<?>[typeList.size()]); 1942 numPrimFields = nfields - types.length; 1943 } 1944 1945 private static boolean isFinal(Field field) { 1946 return (field.getModifiers() & Modifier.FINAL) != 0; 1947 } 1948 1949 public boolean hasFinal() { 1950 return hasFinal; 1951 } 1952 1953 /** 1954 * Returns list of ObjectStreamFields representing fields operated on 1955 * by this reflector. The shared/unshared values and Field objects 1956 * contained by ObjectStreamFields in the list reflect their bindings 1957 * to locally defined serializable fields. 1958 */ 1959 ObjectStreamField[] getFields() { 1960 return fields; 1961 } 1962 1963 /** 1964 * Fetches the serializable primitive field values of object obj and 1965 * marshals them into byte array buf starting at offset 0. The caller 1966 * is responsible for ensuring that obj is of the proper type. 1967 */ 1968 void getPrimFieldValues(Object obj, byte[] buf) { 1969 if (obj == null) { 1970 throw new NullPointerException(); 1971 } 1972 /* assuming checkDefaultSerialize() has been called on the class |