/** * uncheck(Class::forName, "xxx"); */ public static <T, R, E extends Exception> R uncheck(FunctionWithExceptions<T, R, E> function, T t) { try { return function.apply(t); } catch (Exception exception) { throwAsUnchecked(exception); return null; } }
/** * .forEach(rethrowConsumer(name -> System.out.println(Class.forName(name)))); or .forEach(rethrowConsumer(ClassNameUtil::println)); */ public static <T, E extends Exception> Consumer<T> rethrowConsumer(ConsumerWithExceptions<T, E> consumer) throws E { return t -> { try { consumer.accept(t); } catch (Exception exception) { throwAsUnchecked(exception); } }; }
/** * uncheck(() -> Class.forName("xxx")); */ public static void uncheck(RunnableWithExceptions t) { try { t.run(); } catch (Exception exception) { throwAsUnchecked(exception); } }
private static Object getBodyParam(ParamStruct paramStruct) { Type argType = paramStruct.argType; Request request = paramStruct.request; if (ReflectKit.isPrimitive(argType)) { return ReflectKit.convert(argType, request.bodyToString()); } else { String json = request.bodyToString(); return StringKit.isNotEmpty(json) ? JsonKit.formJson(json, argType) : null; } }
/** * uncheck(() -> Class.forName("xxx")); */ public static <R, E extends Exception> R uncheck(SupplierWithExceptions<R, E> supplier) { try { return supplier.get(); } catch (Exception exception) { throwAsUnchecked(exception); return null; } }
public static <T> T copy(Object origin, Class<T> destCls) { T dest = ReflectKit.newInstance(destCls); copy(origin, dest); return dest; }
public static <T, U, E extends Exception> BiConsumer<T, U> rethrowBiConsumer(BiConsumerWithExceptions<T, U, E> biConsumer) throws E { return (t, u) -> { try { biConsumer.accept(t, u); } catch (Exception exception) { throwAsUnchecked(exception); } }; }
/** * MD5加密 * * @param data 明文字节数组 * @return 密文字节数组 */ static byte[] md5ToByte(byte[] data) { return hashTemplate(data, "MD5"); }
/** * HmacSHA256加密 * * @param data 明文字节数组 * @param key 秘钥 * @return 密文字节数组 */ public static byte[] hmacSHA256ToByte(byte[] data, byte[] key) { return hmacTemplate(data, key, "HmacSHA256"); }
private static Object getBodyParam(ParamStruct paramStruct) { Type argType = paramStruct.argType; Request request = paramStruct.request; if (ReflectKit.isPrimitive(argType)) { return ReflectKit.convert(argType, request.bodyToString()); } else { String json = request.bodyToString(); return StringKit.isNotEmpty(json) ? JsonKit.formJson(json, argType) : null; } }
/** * uncheck(Class::forName, "xxx"); */ public static <T, R, E extends Exception> R uncheck(FunctionWithExceptions<T, R, E> function, T t) { try { return function.apply(t); } catch (Exception exception) { throwAsUnchecked(exception); return null; } }
/** * uncheck(() -> Class.forName("xxx")); */ public static <R, E extends Exception> R uncheck(SupplierWithExceptions<R, E> supplier) { try { return supplier.get(); } catch (Exception exception) { throwAsUnchecked(exception); return null; } }
public static <T> T copy(Object origin, Class<T> destCls) { T dest = ReflectKit.newInstance(destCls); copy(origin, dest); return dest; }
/** * .forEach(rethrowConsumer(name -> System.out.println(Class.forName(name)))); or .forEach(rethrowConsumer(ClassNameUtil::println)); */ public static <T, E extends Exception> Consumer<T> rethrowConsumer(ConsumerWithExceptions<T, E> consumer) throws E { return t -> { try { consumer.accept(t); } catch (Exception exception) { throwAsUnchecked(exception); } }; }
/** * uncheck(() -> Class.forName("xxx")); */ public static void uncheck(RunnableWithExceptions t) { try { t.run(); } catch (Exception exception) { throwAsUnchecked(exception); } }
public static <T, U, E extends Exception> BiConsumer<T, U> rethrowBiConsumer(BiConsumerWithExceptions<T, U, E> biConsumer) throws E { return (t, u) -> { try { biConsumer.accept(t, u); } catch (Exception exception) { throwAsUnchecked(exception); } }; }
/** * .map(rethrowFunction(name -> Class.forName(name))) or .map(rethrowFunction(Class::forName)) */ public static <T, R, E extends Exception> Function<T, R> rethrowFunction(FunctionWithExceptions<T, R, E> function) throws E { return t -> { try { return function.apply(t); } catch (Exception exception) { throwAsUnchecked(exception); return null; } }; }
/** * rethrowSupplier(() -> new StringJoiner(new String(new byte[]{77, 97, 114, 107}, "UTF-8"))), */ public static <T, E extends Exception> Supplier<T> rethrowSupplier(SupplierWithExceptions<T, E> function) throws E { return () -> { try { return function.get(); } catch (Exception exception) { throwAsUnchecked(exception); return null; } }; }
/** * .map(rethrowFunction(name -> Class.forName(name))) or .map(rethrowFunction(Class::forName)) */ public static <T, R, E extends Exception> Function<T, R> rethrowFunction(FunctionWithExceptions<T, R, E> function) throws E { return t -> { try { return function.apply(t); } catch (Exception exception) { throwAsUnchecked(exception); return null; } }; }
/** * rethrowSupplier(() -> new StringJoiner(new String(new byte[]{77, 97, 114, 107}, "UTF-8"))), */ public static <T, E extends Exception> Supplier<T> rethrowSupplier(SupplierWithExceptions<T, E> function) throws E { return () -> { try { return function.get(); } catch (Exception exception) { throwAsUnchecked(exception); return null; } }; }