diff --git a/hutool-core/src/main/java/cn/hutool/core/exceptions/CheckedUtil.java b/hutool-core/src/main/java/cn/hutool/core/exceptions/CheckedUtil.java new file mode 100644 index 000000000..58ba7444c --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/exceptions/CheckedUtil.java @@ -0,0 +1,297 @@ +package cn.hutool.core.exceptions; + +import cn.hutool.core.lang.func.*; + +import java.util.Objects; + +/** + * 方便的执行会抛出受检查类型异常的方法调用或者代码段 + *
+ * 该工具通过函数式的方式将那些需要抛出受检查异常的表达式或者代码段转化成一个 cn.hutool.core.lang.func.Func* 对象 + *
+ * + *+ * //代码中如果遇到一个方法调用声明了受检查异常那么我们的代码就必须这样写 + * Map+ * + * @author conder + */ +public class CheckedUtil { + + /** + * 接收一个可以转化成 cn.hutool.core.lang.func.Func 的Lambda表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常 + * 如此一来,代码中就不用显示的try-catch转化成运行时异常 + * + * @param expression Lambda表达式 + * @paramdescribedObject = null; + * try { + * describe = BeanUtils.describe(new Object()); + * } catch (IllegalAccessException e) { + * throw new RuntimeException(e); + * } catch (InvocationTargetException e) { + * throw new RuntimeException(e); + * } catch (NoSuchMethodException e) { + * throw new RuntimeException(e); + * } + * // use describedObject ... + * + * //上面的代码增加了异常块使得代码不那么流畅,现在可以这样写: + * Map describedObject = CheckedUtil.uncheck(BeanUtils::describe).call(new Object()); + * // use describedObject ... + * + * CheckedUtil.uncheck 方法接受任意可以转化成 cn.hutool.core.lang.func.Func* 函数式接口的 Lambda 表达式。返回对应的函数式对象。 + * 上述代码可以理解为: + * Func0
运行时传入的参数类型
+ * @param FuncRt uncheck(Func expression) {
+ return uncheck(expression, new RuntimeException());
+ }
+
+ /**
+ * 接收一个可以转化成 cn.hutool.core.lang.func.Func0 的Lambda表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常
+ * 如此一来,代码中就不用显示的try-catch转化成运行时异常
+ *
+ * @param expression 运行时传入的参数类型
+ * @param 运行时传入的参数类型
+ * @param Func1Rt uncheck(Func1 expression) {
+ return uncheck(expression, new RuntimeException());
+ }
+
+
+ /**
+ * 接收一个可以转化成 cn.hutool.core.lang.func.VoidFunc 的Lambda表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常
+ * 如此一来,代码中就不用显示的try-catch转化成运行时异常
+ *
+ * @param expression 运行时传入的参数类型
+ * @param 运行时传入的参数类型
+ * @return cn.hutool.core.lang.func.VoidFunc
+ */
+ public static VoidFuncRt uncheck(VoidFunc expression) {
+ return uncheck(expression, new RuntimeException());
+ }
+
+ /**
+ * 接收一个可以转化成 cn.hutool.core.lang.func.VoidFunc0 的Lambda表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常
+ * 如此一来,代码中就不用显示的try-catch转化成运行时异常
+ *
+ * @param expression 运行时传入的参数类型
+ * @return cn.hutool.core.lang.func.VoidFunc0
+ */
+ public static VoidFunc0Rt uncheck(VoidFunc0 expression) {
+ return uncheck(expression, new RuntimeException());
+ }
+
+ /**
+ * 接收一个可以转化成 cn.hutool.core.lang.func.VoidFunc1 的Lambda表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常
+ * 如此一来,代码中就不用显示的try-catch转化成运行时异常
+ *
+ * @param expression 运行时传入的参数类型
+ * @param 运行时传入的参数类型
+ * @return cn.hutool.core.lang.func.VoidFunc1
+ */
+ public static VoidFunc1Rt uncheck(VoidFunc1 expression) {
+ return uncheck(expression, new RuntimeException());
+ }
+
+
+ /**
+ * 接收一个可以转化成 cn.hutool.core.lang.func.Func的Lambda表达式,和一个RuntimeException,当执行表达式抛出任何异常的时候,都会转化成运行时异常
+ * 如此一来,代码中就不用显示的try-catch转化成运行时异常
+ *
+ * @param expression Lambda表达式
+ * @param rte 期望抛出的运行时异常
+ * @param 运行时传入的参数类型
+ * @param FuncRt uncheck(Func expression, RuntimeException rte) {
+ Objects.requireNonNull(expression, "expression can not be null");
+ return t -> {
+ try {
+ return expression.call(t);
+ } catch (Exception e) {
+ if (rte == null) {
+ throw new RuntimeException(e);
+ } else {
+ rte.initCause(e);
+ throw rte;
+ }
+ }
+ };
+ }
+
+ /**
+ * 接收一个可以转化成 cn.hutool.core.lang.func.Func0的Lambda表达式,和一个RuntimeException,当执行表达式抛出任何异常的时候,都会转化成运行时异常
+ * 如此一来,代码中就不用显示的try-catch转化成运行时异常
+ *
+ * @param expression Lambda表达式
+ * @param rte 期望抛出的运行时异常
+ * @param 运行时传入的参数类型
+ * @param Func1Rt uncheck(Func1 expression, RuntimeException rte) {
+ Objects.requireNonNull(expression, "expression can not be null");
+ return t -> {
+ try {
+ return expression.call(t);
+ } catch (Exception e) {
+ if (rte == null) {
+ throw new RuntimeException(e);
+ } else {
+ rte.initCause(e);
+ throw rte;
+ }
+ }
+ };
+ }
+
+ /**
+ * 接收一个可以转化成 cn.hutool.core.lang.func.VoidFunc的Lambda表达式,和一个RuntimeException,当执行表达式抛出任何异常的时候,都会转化成运行时异常
+ * 如此一来,代码中就不用显示的try-catch转化成运行时异常
+ *
+ * @param expression Lambda表达式
+ * @param rte 期望抛出的运行时异常
+ * @param 运行时传入的参数类型
+ * @return cn.hutool.core.lang.func.VoidFunc
+ */
+ public static VoidFuncRt uncheck(VoidFunc expression, RuntimeException rte) {
+ Objects.requireNonNull(expression, "expression can not be null");
+ return t -> {
+ try {
+ expression.call(t);
+ } catch (Exception e) {
+ if (rte == null) {
+ throw new RuntimeException(e);
+ } else {
+ rte.initCause(e);
+ throw rte;
+ }
+ }
+ };
+ }
+
+
+ /**
+ * 接收一个可以转化成 cn.hutool.core.lang.func.VoidFunc0的Lambda表达式,和一个RuntimeException,当执行表达式抛出任何异常的时候,都会转化成运行时异常
+ * 如此一来,代码中就不用显示的try-catch转化成运行时异常
+ *
+ * @param expression Lambda表达式
+ * @param rte 期望抛出的运行时异常
+ * @return cn.hutool.core.lang.func.VoidFunc0
+ */
+ public static VoidFunc0Rt uncheck(VoidFunc0 expression, RuntimeException rte) {
+ Objects.requireNonNull(expression, "expression can not be null");
+ return () -> {
+ try {
+ expression.call();
+ } catch (Exception e) {
+ if (rte == null) {
+ throw new RuntimeException(e);
+ } else {
+ rte.initCause(e);
+ throw rte;
+ }
+ }
+ };
+ }
+
+
+ /**
+ * 接收一个可以转化成 cn.hutool.core.lang.func.VoidFunc1的Lambda表达式,和一个RuntimeException,当执行表达式抛出任何异常的时候,都会转化成运行时异常
+ * 如此一来,代码中就不用显示的try-catch转化成运行时异常
+ *
+ * @param expression Lambda表达式
+ * @param rte 期望抛出的运行时异常
+ * @param 运行时传入的参数类型
+ * @return cn.hutool.core.lang.func.VoidFunc1
+ */
+ public static VoidFunc1Rt uncheck(VoidFunc1 expression, RuntimeException rte) {
+ Objects.requireNonNull(expression, "expression can not be null");
+ return t -> {
+ try {
+ expression.call(t);
+ } catch (Exception e) {
+ if (rte == null) {
+ throw new RuntimeException(e);
+ } else {
+ rte.initCause(e);
+ throw rte;
+ }
+ }
+ };
+ }
+
+ public interface FuncRt extends Func {
+ R call(P... parameters) throws RuntimeException;
+ }
+
+ public interface Func0Rt extends Func1 {
+ R call(P parameter) throws RuntimeException;
+ }
+
+ public interface VoidFuncRt extends VoidFunc {
+ void call(P... parameters) throws RuntimeException;
+ }
+
+ public interface VoidFunc0Rt extends VoidFunc0 {
+ void call() throws RuntimeException;
+ }
+
+ public interface VoidFunc1Rt extends VoidFunc1 {
+ void call(P parameter) throws RuntimeException;
+ }
+
+
+}
diff --git a/hutool-core/src/test/java/cn/hutool/core/exceptions/CheckedUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/exceptions/CheckedUtilTest.java
new file mode 100644
index 000000000..10b007b23
--- /dev/null
+++ b/hutool-core/src/test/java/cn/hutool/core/exceptions/CheckedUtilTest.java
@@ -0,0 +1,65 @@
+package cn.hutool.core.exceptions;
+
+import cn.hutool.core.lang.func.Func1;
+import cn.hutool.core.lang.func.VoidFunc0;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.FileInputStream;
+
+/**
+ * 方便的执行会抛出受检查类型异常的方法调用或者代码段
+ *
+ * 该工具通过函数式的方式将那些需要抛出受检查异常的表达式或者代码段转化成一个标准的java8 functional 对象
+ *