@Override public void writeToParcel(Parcel dest, int flags) { Postman.writeToParcel(this, dest); } }
/** * Reads an object of the specified class from the given Parcel. * * @param clazz The class of the object to be read. * @param parcel The source Parcel. * @param <T> The type of the Object. * * @return A new instance of the specified type instantiated from the Parcel. * * @throws PostmanException if there is no {@link Parceler} associated with the given type. */ public static <T> T readFromParcel(Class<T> clazz, Parcel parcel) throws PostmanException { Parceler<T> parceler = getParcelerForClass(clazz); return parceler.readFromParcel(parcel); }
/** * Creates of new array of the specified type with the specified size. * * @param clazz The class associated with the type of array to create. * @param size The desired size of the array. * @param <T> The type of the array to create. * * @return A new array of the specified type with the specified size. * * @throws PostmanException if there is no {@link Parceler} associated with the given type. * @see Creator#newArray(int) */ public static <T> T[] newArray(Class<T> clazz, int size) throws PostmanException { Parceler<T> parceler = getParcelerForClass(clazz); return parceler.newArray(size); }
/** * Write the specified Object to a {@link Parcel}. * * @param object The Object to be written to the Parcel. * @param parcel The destination Parcel. * @param <T> The type of the Object. * * @throws PostmanException if there is no {@link Parceler} associated with the given type. */ public static <T> void writeToParcel(T object, Parcel parcel) throws PostmanException { @SuppressWarnings("unchecked") Parceler<T> parceler = getParcelerForClass((Class<T>) object.getClass()); parceler.writeToParcel(object, parcel); }
@Test public void testNonParceledClassThrowsPostmanException() { Object o = new Object(); boolean exceptionCaught = false; try { Postman.writeToParcel(o, Parcel.obtain()); } catch (PostmanException e) { exceptionCaught = true; assertTrue("expected cause to be of type ClassNotFoundException but found " + e.getCause().getClass().getCanonicalName(), e.getCause() instanceof ClassNotFoundException); } assertTrue(exceptionCaught); }
/** * Get a {@link Creator} that can be used for the {@code CREATOR} field of a {@link * Parcelable}. * * @param clazz The class associated with the type the the Creator will create. * @param <T> The type the Creator will create. * * @return A fully implemented Creator for the specified type. * * @throws PostmanException if there is no {@link Parceler} associated with the given type. */ public static <T> Parcelable.Creator<T> getCreator(Class<T> clazz) throws PostmanException { @SuppressWarnings("unchecked") Parcelable.Creator<T> creator = (Parcelable.Creator<T>) creatorMap.get(clazz); if (creator == null) { creator = newCreator(clazz); creatorMap.put(clazz, creator); } return creator; }
private static <T> Parceler<T> getParcelerForClass(Class<T> clazz) { @SuppressWarnings("unchecked") Parceler<T> parceler = (com.workday.postman.parceler.Parceler<T>) parcelerMap.get(clazz); if (parceler == null) { String name = ConcreteTypeNames.constructClassName(clazz, Names.PARCELER_SUFFIX); try { @SuppressWarnings("unchecked") Parceler<T> newParceler = (Parceler<T>) Class.forName(name).newInstance(); parceler = newParceler; } catch (InstantiationException | IllegalAccessException e) { String message = "Postman experienced an internal error. Please report this issue."; throw new PostmanException(message, e); } catch (ClassNotFoundException e) { String message = String.format( "No %s was found for class %s. Check that (1) you annotated the class " + "with @%s, (2) the Postman processor ran, and (3) ProGuard did " + "not remove the Parcelers.", Parceler.class.getSimpleName(), clazz.getCanonicalName(), Parceled.class.getSimpleName()); throw new PostmanException(message, e); } parcelerMap.put(clazz, parceler); } return parceler; }
/** * Reads an object of the specified class from the given Parcel. * * @param clazz The class of the object to be read. * @param parcel The source Parcel. * @param <T> The type of the Object. * * @return A new instance of the specified type instantiated from the Parcel. * * @throws PostmanException if there is no {@link Parceler} associated with the given type. */ public static <T> T readFromParcel(Class<T> clazz, Parcel parcel) throws PostmanException { Parceler<T> parceler = getParcelerForClass(clazz); return parceler.readFromParcel(parcel); }
/** * Creates of new array of the specified type with the specified size. * * @param clazz The class associated with the type of array to create. * @param size The desired size of the array. * @param <T> The type of the array to create. * * @return A new array of the specified type with the specified size. * * @throws PostmanException if there is no {@link Parceler} associated with the given type. * @see Creator#newArray(int) */ public static <T> T[] newArray(Class<T> clazz, int size) throws PostmanException { Parceler<T> parceler = getParcelerForClass(clazz); return parceler.newArray(size); }
/** * Write the specified Object to a {@link Parcel}. * * @param object The Object to be written to the Parcel. * @param parcel The destination Parcel. * @param <T> The type of the Object. * * @throws PostmanException if there is no {@link Parceler} associated with the given type. */ public static <T> void writeToParcel(T object, Parcel parcel) throws PostmanException { @SuppressWarnings("unchecked") Parceler<T> parceler = getParcelerForClass((Class<T>) object.getClass()); parceler.writeToParcel(object, parcel); }
@Override public void writeToParcel(Parcel dest, int flags) { Postman.writeToParcel(this, dest); } }
/** * Get a {@link Creator} that can be used for the {@code CREATOR} field of a {@link * Parcelable}. * * @param clazz The class associated with the type the the Creator will create. * @param <T> The type the Creator will create. * * @return A fully implemented Creator for the specified type. * * @throws PostmanException if there is no {@link Parceler} associated with the given type. */ public static <T> Parcelable.Creator<T> getCreator(Class<T> clazz) throws PostmanException { @SuppressWarnings("unchecked") Parcelable.Creator<T> creator = (Parcelable.Creator<T>) creatorMap.get(clazz); if (creator == null) { creator = newCreator(clazz); creatorMap.put(clazz, creator); } return creator; }
private static <T> Parceler<T> getParcelerForClass(Class<T> clazz) { @SuppressWarnings("unchecked") Parceler<T> parceler = (com.workday.postman.parceler.Parceler<T>) parcelerMap.get(clazz); if (parceler == null) { String name = ConcreteTypeNames.constructClassName(clazz, Names.PARCELER_SUFFIX); try { @SuppressWarnings("unchecked") Parceler<T> newParceler = (Parceler<T>) Class.forName(name).newInstance(); parceler = newParceler; } catch (InstantiationException | IllegalAccessException e) { String message = "Postman experienced an internal error. Please report this issue."; throw new PostmanException(message, e); } catch (ClassNotFoundException e) { String message = String.format( "No %s was found for class %s. Check that (1) you annotated the class " + "with @%s, (2) the Postman processor ran, and (3) ProGuard did " + "not remove the Parcelers.", Parceler.class.getSimpleName(), clazz.getCanonicalName(), Parceled.class.getSimpleName()); throw new PostmanException(message, e); } parcelerMap.put(clazz, parceler); } return parceler; }
@Override public void writeToParcel(Parcel dest, int flags) { Postman.writeToParcel(this, dest); }
@Override public void writeToParcel(Parcel dest, int flags) { Postman.writeToParcel(this, dest); }
@Override public void writeToParcel(Parcel dest, int flags) { Postman.writeToParcel(this, dest); }