ReflectionUtils { public static void invokePreDestroy(List<?> objects) { for (Object object : objects) { Method method = findMethod(object.getClass(), PreDestroy.class); if (method != null) { try { invoke(method, object); } catch (InvocationTargetException e) { if (e.getCause() instanceof Error) { throw (Error) e.getCause(); } else { logger.error("Failure destroying object {}: {}", object.getClass().getName(), e.getCause().getMessage()); } } } } }  static String getSimpleName(Class<?> klass); static String getPrettyName(Class<?> klass); static @Nullable Method findMethod(Class<?> klass, Class<? extends Annotation> annotation,
      Class<?>... signature); static Object invoke(Method method, Object self); static Object readField(Class<?> klass, String name); static Object readField(Class<?> klass, @Nullable Object self, String name); static M forceAccessible(M member); static void invokePostConstruct(List<?> objects); static void invokePreDestroy(List<?> objects); static final Function<Object, String> TO_SIMPLECLASSNAME; static final Function<Object, String> TO_CLASSNAME; static final Function<Object, String> TO_PRETTYCLASSNAME; }
@Test public void testPreDestroy_notFound() { List<?> objs = singletonList("x"); ReflectionUtils.invokePreDestroy(objs); }
@Test public void testPreDestroy_exception() { List<?> objs = singletonList(new Bad()); ReflectionUtils.invokePreDestroy(objs); }
@Test(expected = Error.class) public void testPreDestroy_error() { List<?> objs = singletonList(new SuperBad()); ReflectionUtils.invokePreDestroy(objs); }