// writes fields of a GlobalParcelable to a parcel // does not include the first parcelled item -- the class name protected static void dehydrate(GlobalParcelable model, Parcel out) throws IllegalArgumentException, IllegalAccessException { Log.i("GlobalParcelable", "dehydrating... " + model.getClass().toString()); // get the fields Field[] fields = model.getClass().getDeclaredFields(); // sort the fields so it is in deterministic order Arrays.sort(fields, compareMemberByName); // populate the fields for (Field field : fields) { field.setAccessible(true); if (field.getType().equals(int.class)) { out.writeInt(field.getInt(model)); } else if (field.getType().equals(double.class)) { out.writeDouble(field.getDouble(model)); } else if (field.getType().equals(float.class)) { out.writeFloat(field.getFloat(model)); } else if (field.getType().equals(long.class)) { out.writeLong(field.getLong(model)); } else if (field.getType().equals(String.class)) { out.writeString((String) field.get(model)); } else if (field.getType().equals(boolean.class)) { out.writeByte(field.getBoolean(model) ? (byte) 1 : (byte) 0); } else if (field.getType().equals(Date.class)) { Date date = (Date) field.get(model); if (date != null) { out.writeLong(date.getTime()); } else { out.writeLong(0); } } else if (GlobalParcelable.class.isAssignableFrom(field.getType())) { // why did this happen? Log.e( "GlobalParcelable", "GlobalParcelable F*ck up: " + " (" + field.getType().toString() + ")"); out.writeParcelable((GlobalParcelable) field.get(model), 0); } else { // wtf Log.e( "GlobalParcelable", "Could not write field to parcel: " + " (" + field.getType().toString() + ")"); } } }
// reads the parcelled items and put them into this object's fields // must be run after getting the first parcelled item -- the class name protected static void rehydrate(GlobalParcelable model, Parcel in) throws IllegalArgumentException, IllegalAccessException { Log.i("GlobalParcelable", "rehydrating... " + model.getClass().toString()); // get the fields Field[] fields = model.getClass().getDeclaredFields(); // sort the fields so it is in deterministic order Arrays.sort(fields, compareMemberByName); // populate the fields for (Field field : fields) { field.setAccessible(true); if (field.getType().equals(int.class)) { field.set(model, in.readInt()); } else if (field.getType().equals(double.class)) { field.set(model, in.readDouble()); } else if (field.getType().equals(float.class)) { field.set(model, in.readFloat()); } else if (field.getType().equals(long.class)) { field.set(model, in.readLong()); } else if (field.getType().equals(String.class)) { field.set(model, in.readString()); } else if (field.getType().equals(boolean.class)) { field.set(model, in.readByte() == 1); } else if (field.getType().equals(Date.class)) { Date date = new Date(in.readLong()); field.set(model, date); } else if (GlobalParcelable.class.isAssignableFrom(field.getType())) { Log.e( "GlobalParcelable", "read GlobalParcelable: " + " (" + field.getType().toString() + ")"); field.set(model, in.readParcelable(field.getType().getClassLoader())); } else { // wtf Log.e( "GlobalParcelable", "Could not read field from parcel: " + field.getName() + " (" + field.getType().toString() + ")"); } } }