org.jooby.funzy.Try.unwrap()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(4.8k)|赞(0)|评价(0)|浏览(202)

本文整理了Java中org.jooby.funzy.Try.unwrap()方法的一些代码示例,展示了Try.unwrap()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Try.unwrap()方法的具体详情如下:
包路径:org.jooby.funzy.Try
类名称:Try
方法名:unwrap

Try.unwrap介绍

[英]In case of failure unwrap the exception provided by calling Throwable#getCause(). Useful for clean/shorter stackstrace. Example for java.lang.reflect.InvocationTargetException:

Try.run(() -> ).unwrap(InvocationTargetException.class) 
.throwException(); 
}

[中]如果失败,请打开调用Throwable#getCause()提供的异常。适用于干净/较短的堆叠距离。java示例。朗,反思一下。InvocationTargetException:

Try.run(() -> ).unwrap(InvocationTargetException.class) 
.throwException(); 
}

代码示例

代码示例来源:origin: jooby-project/jooby

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

代码示例来源:origin: jooby-project/jooby

method.setAccessible(true);
  method.invoke(owner);
 }).unwrap(InvocationTargetException.class)
   .throwException();
});

代码示例来源:origin: jooby-project/jooby

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

代码示例来源:origin: jooby-project/jooby

Files.copy(from, to);
}).unwrap(InvocationTargetException.class)
  .throwException();

代码示例来源:origin: org.jooby/funzy

@Override public <X extends Throwable> Value<V> unwrap(Class<? extends X> type) {
 return (Value<V>) super.unwrap(type);
}

代码示例来源:origin: org.jooby/funzy

@Override public Value<V> unwrap(final Throwing.Predicate<Throwable> predicate) {
 return (Value<V>) super.unwrap(predicate);
}

代码示例来源:origin: org.jooby/funzy

/**
 * 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);
}

代码示例来源:origin: org.jooby/jooby-aws

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

代码示例来源:origin: org.jooby/jooby

method.setAccessible(true);
  method.invoke(owner);
 }).unwrap(InvocationTargetException.class)
   .throwException();
});

代码示例来源:origin: org.jooby/jooby-jetty

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

相关文章