From 1d9eb522186c00db8435b02845791aaf2ea13ae9 Mon Sep 17 00:00:00 2001 From: jiangzeyin Date: Thu, 10 Jan 2019 11:34:15 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E8=BD=AC=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hutool/core/exceptions/ExceptionUtil.java | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/exceptions/ExceptionUtil.java b/hutool-core/src/main/java/cn/hutool/core/exceptions/ExceptionUtil.java index e1b75458dc..fdca8d951d 100644 --- a/hutool-core/src/main/java/cn/hutool/core/exceptions/ExceptionUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/exceptions/ExceptionUtil.java @@ -254,26 +254,59 @@ public class ExceptionUtil { * @since 4.3.2 */ public static boolean isFromOrSuppressedThrowable(Throwable throwable, Class exceptionClass) { - return convertFromOrSuppressedThrowable(throwable, exceptionClass) != null; + return convertFromOrSuppressedThrowable(throwable, exceptionClass, true) != null; + } + + /** + * 判断指定异常是否来自或者包含指定异常 + * + * @param throwable 异常 + * @param exceptionClass 定义的引起异常的类 + * @param checkCause 判断cause + * @return true 来自或者包含 + * @since 4.4.1 + */ + public static boolean isFromOrSuppressedThrowable(Throwable throwable, Class exceptionClass, boolean checkCause) { + return convertFromOrSuppressedThrowable(throwable, exceptionClass, checkCause) != null; } /** * 转化指定异常为来自或者包含指定异常 * - *@param 异常类型 + * @param 异常类型 * @param throwable 异常 * @param exceptionClass 定义的引起异常的类 * @return 结果为null 不是来自或者包含 * @since 4.3.2 */ - @SuppressWarnings("unchecked") public static T convertFromOrSuppressedThrowable(Throwable throwable, Class exceptionClass) { + return convertFromOrSuppressedThrowable(throwable,exceptionClass,true); + } + + /** + * 转化指定异常为来自或者包含指定异常 + * + * @param 异常类型 + * @param throwable 异常 + * @param exceptionClass 定义的引起异常的类 + * @return 结果为null 不是来自或者包含 + * @param checkCause 判断cause + * @since 4.4.1 + */ + @SuppressWarnings("unchecked") + public static T convertFromOrSuppressedThrowable(Throwable throwable, Class exceptionClass, boolean checkCause) { if (throwable == null || exceptionClass == null) { return null; } if (exceptionClass.isAssignableFrom(throwable.getClass())) { return (T) throwable; } + if(checkCause) { + Throwable cause = throwable.getCause(); + if(cause != throwable && exceptionClass.isAssignableFrom(cause.getClass())){ + return (T) throwable; + } + } Throwable[] throwables = throwable.getSuppressed(); if (ArrayUtil.isNotEmpty(throwables)) { for (Throwable throwable1 : throwables) { -- Gitee From 41b992bea754151d658ccc87a29db5db34d65e8c Mon Sep 17 00:00:00 2001 From: jiangzeyin Date: Thu, 10 Jan 2019 11:43:14 +0800 Subject: [PATCH 2/2] add test --- .../hutool/core/exceptions/ExceptionUtil.java | 2 +- .../core/exceptions/ExceptionUtilTest.java | 67 ++++++++++--------- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/exceptions/ExceptionUtil.java b/hutool-core/src/main/java/cn/hutool/core/exceptions/ExceptionUtil.java index fdca8d951d..98e22814f7 100644 --- a/hutool-core/src/main/java/cn/hutool/core/exceptions/ExceptionUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/exceptions/ExceptionUtil.java @@ -304,7 +304,7 @@ public class ExceptionUtil { if(checkCause) { Throwable cause = throwable.getCause(); if(cause != throwable && exceptionClass.isAssignableFrom(cause.getClass())){ - return (T) throwable; + return (T) cause; } } Throwable[] throwables = throwable.getSuppressed(); diff --git a/hutool-core/src/test/java/cn/hutool/core/exceptions/ExceptionUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/exceptions/ExceptionUtilTest.java index c7cf09b588..dc6bae5153 100644 --- a/hutool-core/src/test/java/cn/hutool/core/exceptions/ExceptionUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/exceptions/ExceptionUtilTest.java @@ -1,30 +1,37 @@ -package cn.hutool.core.exceptions; - -import java.io.IOException; - -import org.junit.Assert; -import org.junit.Test; - -import cn.hutool.core.exceptions.ExceptionUtil; -import cn.hutool.core.io.IORuntimeException; - -/** - * 异常工具单元测试 - * @author looly - * - */ -public class ExceptionUtilTest { - - @Test - public void wrapTest() { - IORuntimeException e = ExceptionUtil.wrap(new IOException(), IORuntimeException.class); - Assert.assertNotNull(e); - } - - @Test - public void getRootTest() { - //查找入口方法 - StackTraceElement ele = ExceptionUtil.getRootStackElement(); - Assert.assertEquals("main", ele.getMethodName()); - } -} +package cn.hutool.core.exceptions; + +import cn.hutool.core.io.IORuntimeException; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; + +/** + * 异常工具单元测试 + * + * @author looly + */ +public class ExceptionUtilTest { + + @Test + public void wrapTest() { + IORuntimeException e = ExceptionUtil.wrap(new IOException(), IORuntimeException.class); + Assert.assertNotNull(e); + } + + @Test + public void getRootTest() { + //查找入口方法 + StackTraceElement ele = ExceptionUtil.getRootStackElement(); + Assert.assertEquals("main", ele.getMethodName()); + } + + @Test + public void convertTest() { +// RuntimeException e = new RuntimeException(); + IOException ioException = new IOException(); + IllegalArgumentException argumentException = new IllegalArgumentException(ioException); + IOException ioException1 = ExceptionUtil.convertFromOrSuppressedThrowable(argumentException, IOException.class, true); + Assert.assertNotNull(ioException1); + } +} -- Gitee