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; }
@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); }
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; }