@Override public void tryRun() throws Throwable { if (dep == null) { return; } Try.run(() -> { Optional<Method> shutdown = Arrays.stream(dep.getClass().getMethods()) .filter(m -> m.getName().startsWith("shutdown") && m.getParameterCount() == 0 && Modifier.isPublic(m.getModifiers())) .findFirst(); if (shutdown.isPresent()) { log.debug("stopping {}", dep); shutdown.get().invoke(dep); } else { log.debug("no shutdown method found for: {}", dep); } }).unwrap(InvocationTargetException.class) .onComplete(() -> this.dep = null) .throwException(); }
method.setAccessible(true); method.invoke(owner); }).unwrap(InvocationTargetException.class) .throwException(); });
private void tryOption(final Object source, final Config config, final Method option) { Try.run(() -> { String optionName = option.getName().replace("set", ""); Object optionValue = config.getAnyRef(optionName); Class<?> optionType = Primitives.wrap(option.getParameterTypes()[0]); if (Number.class.isAssignableFrom(optionType) && optionValue instanceof String) { // either a byte or time unit try { optionValue = config.getBytes(optionName); } catch (ConfigException.BadValue ex) { optionValue = config.getDuration(optionName, TimeUnit.MILLISECONDS); } if (optionType == Integer.class) { // to int optionValue = ((Number) optionValue).intValue(); } } log.debug("{}.{}({})", source.getClass().getSimpleName(), option.getName(), optionValue); option.invoke(source, optionValue); }).unwrap(InvocationTargetException.class) .throwException(); }
Files.copy(from, to); }).unwrap(InvocationTargetException.class) .throwException();
@Override public <X extends Throwable> Value<V> unwrap(Class<? extends X> type) { return (Value<V>) super.unwrap(type); }
@Override public Value<V> unwrap(final Throwing.Predicate<Throwable> predicate) { return (Value<V>) super.unwrap(predicate); }
/** * In case of failure unwrap the exception provided by calling {@link Throwable#getCause()}. * Useful for clean/shorter stackstrace. * * Example for {@link java.lang.reflect.InvocationTargetException}: * * <pre>{@code * Try.run(() -> { * Method m = ...; * m.invoke(...); //might throw InvocationTargetException * }).unwrap(InvocationTargetException.class) * .throwException(); * }</pre> * * @param type Exception filter. * @param <X> Exception type. * @return This try for success or a new failure with exception unwrap. */ public <X extends Throwable> Try unwrap(Class<? extends X> type) { return unwrap(type::isInstance); }
@Override public void tryRun() throws Throwable { if (dep == null) { return; } Try.run(() -> { Optional<Method> shutdown = Arrays.stream(dep.getClass().getMethods()) .filter(m -> m.getName().startsWith("shutdown") && m.getParameterCount() == 0 && Modifier.isPublic(m.getModifiers())) .findFirst(); if (shutdown.isPresent()) { log.debug("stopping {}", dep); shutdown.get().invoke(dep); } else { log.debug("no shutdown method found for: {}", dep); } }).unwrap(InvocationTargetException.class) .onComplete(() -> this.dep = null) .throwException(); }
method.setAccessible(true); method.invoke(owner); }).unwrap(InvocationTargetException.class) .throwException(); });
private void tryOption(final Object source, final Config config, final Method option) { Try.run(() -> { String optionName = option.getName().replace("set", ""); Object optionValue = config.getAnyRef(optionName); Class<?> optionType = Primitives.wrap(option.getParameterTypes()[0]); if (Number.class.isAssignableFrom(optionType) && optionValue instanceof String) { // either a byte or time unit try { optionValue = config.getBytes(optionName); } catch (ConfigException.BadValue ex) { optionValue = config.getDuration(optionName, TimeUnit.MILLISECONDS); } if (optionType == Integer.class) { // to int optionValue = ((Number) optionValue).intValue(); } } log.debug("{}.{}({})", source.getClass().getSimpleName(), option.getName(), optionValue); option.invoke(source, optionValue); }).unwrap(InvocationTargetException.class) .throwException(); }