@Test public void testCloneExceptionWonky() { ExceptionFactory.WonkyException raw = ExceptionFactory.getWonkyException(); @SuppressWarnings("ThrowableResultOfMethodCallIgnored") ConnectException cloned = ExceptionUtils.cloneException(raw); assert cloned != null; Assert.assertEquals(raw.getMessage(), cloned.getMessage()); Assert.assertArrayEquals(raw.getStackTrace(), cloned.getStackTrace()); Assert.assertEquals( "Cloning an uninstantiable Exception should return an instance of its super class", raw.getClass().getSuperclass(), cloned.getClass()); }
@Nullable @VisibleForTesting static <T extends Throwable> T cloneException(T wrapMe) { Class<?> newClass = wrapMe.getClass(); while (newClass != null) { try { T returnMe = (T) newClass.getConstructor(String.class).newInstance(wrapMe.getMessage()); returnMe.setStackTrace(wrapMe.getStackTrace()); return returnMe; } catch (InvocationTargetException e) { newClass = newClass.getSuperclass(); } catch (NoSuchMethodException e) { newClass = newClass.getSuperclass(); } catch (InstantiationException e) { newClass = newClass.getSuperclass(); } catch (IllegalAccessException e) { newClass = newClass.getSuperclass(); } } return null; }
ExceptionUtils { @Nullable @VisibleForTesting static <T extends Throwable> T cloneException(T wrapMe) { Class<?> newClass = wrapMe.getClass(); while (newClass != null) { try { T returnMe = (T) newClass.getConstructor(String.class).newInstance(wrapMe.getMessage()); returnMe.setStackTrace(wrapMe.getStackTrace()); return returnMe; } catch (InvocationTargetException e) { newClass = newClass.getSuperclass(); } catch (NoSuchMethodException e) { newClass = newClass.getSuperclass(); } catch (InstantiationException e) { newClass = newClass.getSuperclass(); } catch (IllegalAccessException e) { newClass = newClass.getSuperclass(); } } return null; } }
ExceptionUtils { @Nullable @VisibleForTesting static <T extends Throwable> T cloneException(T wrapMe) { Class<?> newClass = wrapMe.getClass(); while (newClass != null) { try { T returnMe = (T) newClass.getConstructor(String.class).newInstance(wrapMe.getMessage()); returnMe.setStackTrace(wrapMe.getStackTrace()); return returnMe; } catch (InvocationTargetException e) { newClass = newClass.getSuperclass(); } catch (NoSuchMethodException e) { newClass = newClass.getSuperclass(); } catch (InstantiationException e) { newClass = newClass.getSuperclass(); } catch (IllegalAccessException e) { newClass = newClass.getSuperclass(); } } return null; }  }
ExceptionUtils { @Nullable @VisibleForTesting static <T extends Throwable> T cloneException(T wrapMe) { Class<?> newClass = wrapMe.getClass(); while (newClass != null) { try { T returnMe = (T) newClass.getConstructor(String.class).newInstance(wrapMe.getMessage()); returnMe.setStackTrace(wrapMe.getStackTrace()); return returnMe; } catch (InvocationTargetException e) { newClass = newClass.getSuperclass(); } catch (NoSuchMethodException e) { newClass = newClass.getSuperclass(); } catch (InstantiationException e) { newClass = newClass.getSuperclass(); } catch (IllegalAccessException e) { newClass = newClass.getSuperclass(); } } return null; }  static void blameBankdroid(Throwable exception); }
ExceptionUtils { @Nullable @VisibleForTesting static <T extends Throwable> T cloneException(T wrapMe) { Class<?> newClass = wrapMe.getClass(); while (newClass != null) { try { T returnMe = (T) newClass.getConstructor(String.class).newInstance(wrapMe.getMessage()); returnMe.setStackTrace(wrapMe.getStackTrace()); return returnMe; } catch (InvocationTargetException e) { newClass = newClass.getSuperclass(); } catch (NoSuchMethodException e) { newClass = newClass.getSuperclass(); } catch (InstantiationException e) { newClass = newClass.getSuperclass(); } catch (IllegalAccessException e) { newClass = newClass.getSuperclass(); } } return null; }  static void blameBankdroid(Throwable exception);  }