public static <X, T> Function<X, T> function(CheckedFunction<X, T> checkedFunction) { return x -> { try { return checkedFunction.apply(x); } catch (Throwable t) { throw Sneaky.throwAnyway(t); } }; }
public static <X, Y, T> BiFunction<X, Y, T> biFunction(CheckedBiFunction<X, Y, T> checkedBiFunction) { return (x, y) -> { try { return checkedBiFunction.apply(x, y); } catch (Throwable t) { throw Sneaky.throwAnyway(t); } }; }
public static <T> Consumer<T> consumer(CheckedConsumer<T> checkedConsumer) { return (x) -> { try { checkedConsumer.accept(x); } catch (Throwable t) { throw Sneaky.throwAnyway(t); } }; }
public static <T> SneakyCallable<T> callable(CheckedCallable<T> checkedCallable) { return () -> { try { return checkedCallable.call(); } catch (Throwable t) { throw Sneaky.throwAnyway(t); } }; }
public static <T> Supplier<T> supplier(CheckedSupplier<T> checkedSupplier) { return () -> { try { return checkedSupplier.get(); } catch (Throwable t) { throw Sneaky.throwAnyway(t); } }; }
@SuppressWarnings("PMD.PreserveStackTrace") private Handler buildHandler(Class<? extends Handler> handlerType, Class<?> sqlObjectType, Method method) { try { return handlerType.getConstructor(Class.class, Method.class).newInstance(sqlObjectType, method); } catch (InvocationTargetException e) { throw Sneaky.throwAnyway(e.getCause()); } catch (ReflectiveOperationException ignored) { // fall-through } try { return handlerType.getConstructor(Method.class).newInstance(method); } catch (InvocationTargetException e) { throw Sneaky.throwAnyway(e.getCause()); } catch (ReflectiveOperationException ignored) { // fall-through } try { return handlerType.getConstructor().newInstance(); } catch (InvocationTargetException e) { throw Sneaky.throwAnyway(e.getCause()); } catch (ReflectiveOperationException e) { throw new IllegalStateException("Handler class " + handlerType + " cannot be instantiated. " + "Expected a constructor with parameters (Class, Method), (Method), or ().", e); } } }
private static <T extends TemplateEngine> Supplier<T> tryConstructor(Class<T> clazz, Object... args) { return () -> { try { Object[] nonNullArgs = Arrays.stream(args).filter(Objects::nonNull).toArray(Object[]::new); Class[] argClasses = Arrays.stream(nonNullArgs).map(Object::getClass).toArray(Class[]::new); MethodType type = MethodType.methodType(void.class, argClasses); return (T) MethodHandles.lookup().findConstructor(clazz, type).invokeWithArguments(nonNullArgs); } catch (NoSuchMethodException ignored) { return null; } catch (Throwable t) { throw Sneaky.throwAnyway(t); } }; } }
private static <T extends SqlParser> Supplier<T> tryConstructor(Class<T> clazz, Object... args) { return () -> { try { Object[] nonNullArgs = Arrays.stream(args).filter(Objects::nonNull).toArray(Object[]::new); Class[] argClasses = Arrays.stream(nonNullArgs).map(Object::getClass).toArray(Class[]::new); MethodType type = MethodType.methodType(void.class, argClasses); return (T) MethodHandles.lookup().findConstructor(clazz, type).invokeWithArguments(nonNullArgs); } catch (NoSuchMethodException ignored) { return null; } catch (Throwable t) { throw Sneaky.throwAnyway(t); } }; } }
/** * Will <b>always</b> throw an exception, so the caller should also always throw the dummy return value to make sure the control flow remains clear. */ @CheckReturnValue @Nonnull public static DummyException throwAnyway(Throwable t) { if (t instanceof Error) { throw (Error) t; } if (t instanceof RuntimeException) { throw (RuntimeException) t; } if (t instanceof IOException) { throw new UncheckedIOException((IOException) t); } if (t instanceof InterruptedException) { Thread.currentThread().interrupt(); } if (t instanceof InvocationTargetException) { throw throwAnyway(t.getCause()); } throwEvadingChecks(t); // never reached return null; }
@Test public void errorThrownDirectly() { Error error = new OutOfMemoryError(); assertThatThrownBy(() -> Sneaky.throwAnyway(error)) .isSameAs(error); }
@Test public void ioExceptionProperlyWrapped() { IOException ioex = new IOException(); assertThatThrownBy(() -> Sneaky.throwAnyway(ioex)) .isInstanceOf(UncheckedIOException.class) .hasCause(ioex); }
@Test public void runtimeExceptionThrownDirectly() { RuntimeException rex = new IllegalArgumentException(); assertThatThrownBy(() -> Sneaky.throwAnyway(rex)) .isSameAs(rex); }
@Test public void invocationTargetExceptionIsUnwrapped() { Throwable cause = new WhateverException(); InvocationTargetException ite = new InvocationTargetException(cause); assertThatThrownBy(() -> Sneaky.throwAnyway(ite)) .isSameAs(cause); }
@Test public void genericCheckedExceptionThrownDirectly() { Exception ex = new WhateverException(); assertThatThrownBy(() -> Sneaky.throwAnyway(ex)) .isSameAs(ex); }