@Test @SuppressWarnings({"PMD.AvoidCatchingNPE"}) public void testCloneExceptionNPE() { NullPointerException raw = null; try { new Urllib(null); Assert.fail("Exception expected"); } catch (NullPointerException e) { raw = e; } @SuppressWarnings("ThrowableResultOfMethodCallIgnored") NullPointerException cloned = ExceptionUtils.cloneException(raw); assert cloned != null; Assert.assertEquals(raw.getMessage(), cloned.getMessage()); Assert.assertArrayEquals(raw.getStackTrace(), cloned.getStackTrace()); Assert.assertEquals(raw.getClass(), 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);  }