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); } }; }
@Test public void genericCheckedExceptionThrownDirectly() { Exception ex = new WhateverException(); assertThatThrownBy(() -> Sneaky.throwAnyway(ex)) .isSameAs(ex); }
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); } }; }
@Test public void errorThrownDirectly() { Error error = new OutOfMemoryError(); assertThatThrownBy(() -> Sneaky.throwAnyway(error)) .isSameAs(error); }
@SuppressWarnings("unchecked") private static <S> Supplier<S> constructorOf(Class<S> impl) { try { return (Supplier<S>) Unchecked.supplier(MethodHandles.lookup().findConstructor(impl, MethodType.methodType(void.class))::invoke); } catch (ReflectiveOperationException e) { throw new IllegalArgumentException("Couldn't find public constructor of " + impl, e); } }
@Override public void set(String property, Object value) { Unchecked.biFunction(properties.get(property).setter::invoke).apply(instance, value); }
@Override T newInstance(Object... params) { return Unchecked.<Object[], T>function(constructor::newInstance).apply(params); }
/** * Install a given {@link JdbiPlugin} instance that will configure any * provided {@link Handle} instances. * @param plugin the plugin to install * @return this */ public Jdbi installPlugin(JdbiPlugin plugin) { Unchecked.consumer(plugin::customizeJdbi).accept(this); plugins.add(plugin); return this; }
@Test public void invocationTargetExceptionIsUnwrapped() { Throwable cause = new WhateverException(); InvocationTargetException ite = new InvocationTargetException(cause); assertThatThrownBy(() -> Sneaky.throwAnyway(ite)) .isSameAs(cause); }
@Test public void ioExceptionProperlyWrapped() { IOException ioex = new IOException(); assertThatThrownBy(() -> Sneaky.throwAnyway(ioex)) .isInstanceOf(UncheckedIOException.class) .hasCause(ioex); }
private static Optional<Supplier<?>> nullaryMethodOf(Class<?> impl, String methodName) { try { return Optional.of(Unchecked.supplier(MethodHandles.lookup() .unreflect(impl.getMethod(methodName))::invoke)); } catch (ReflectiveOperationException e) { return Optional.empty(); } }
@Override public void set(String property, Object value) { Unchecked.biFunction(properties.get(property).setter::invoke).apply(b, value); }
@Override public T build() { return defn.cast(Unchecked.function(builderBuild::invoke).apply(b)); } };
@Override T newInstance(Object... params) { return type.cast(Unchecked.<Object, Object[], Object>biFunction(method::invoke).apply(null, params)); }
@Override public Object invoke(Object target, Object[] args, HandleSupplier handle) { return Unchecked.<Object[], Object>function(methodHandle.bindTo(target)::invokeWithArguments).apply(args); }
DefaultMethodHandler(Method method) { Class<?> declaringClass = method.getDeclaringClass(); methodHandle = Unchecked.biFunction(lookupFor(declaringClass)::unreflectSpecial).apply(method, declaringClass); }