From 74452eb77507d02bd51fc1822c6dbd6242e218a5 Mon Sep 17 00:00:00 2001 From: libin Date: Thu, 12 Oct 2023 18:36:30 +0800 Subject: [PATCH 1/3] =?UTF-8?q?TypeUtil.getClass=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=BC=BA=E8=BD=AC=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/core/util/TypeUtil.java | 5 +- .../cn/hutool/core/util/TypeUtilTest.java | 53 ++++++++++++------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/util/TypeUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/TypeUtil.java index 2c75ff749..70d92ca41 100755 --- a/hutool-core/src/main/java/cn/hutool/core/util/TypeUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/TypeUtil.java @@ -40,7 +40,10 @@ public class TypeUtil { } else if (type instanceof ParameterizedType) { return (Class) ((ParameterizedType) type).getRawType(); } else if (type instanceof TypeVariable) { - return (Class) ((TypeVariable) type).getBounds()[0]; + Type[] bounds = ((TypeVariable) type).getBounds(); + if (bounds.length == 1) { + return getClass(bounds[0]); + } } else if (type instanceof WildcardType) { final Type[] upperBounds = ((WildcardType) type).getUpperBounds(); if (upperBounds.length == 1) { diff --git a/hutool-core/src/test/java/cn/hutool/core/util/TypeUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/TypeUtilTest.java index 81c9f030d..a67ed9cd8 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/TypeUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/TypeUtilTest.java @@ -1,48 +1,61 @@ package cn.hutool.core.util; -import lombok.Data; -import org.junit.Assert; -import org.junit.Test; - import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; +import lombok.Data; +import org.junit.Assert; +import org.junit.Test; public class TypeUtilTest { - + @Test public void getEleTypeTest() { Method method = ReflectUtil.getMethod(TestClass.class, "getList"); Type type = TypeUtil.getReturnType(method); Assert.assertEquals("java.util.List", type.toString()); - + Type type2 = TypeUtil.getTypeArgument(type); Assert.assertEquals(String.class, type2); } - + @Test public void getParamTypeTest() { Method method = ReflectUtil.getMethod(TestClass.class, "intTest", Integer.class); Type type = TypeUtil.getParamType(method, 0); Assert.assertEquals(Integer.class, type); - + Type returnType = TypeUtil.getReturnType(method); Assert.assertEquals(Integer.class, returnType); } - + + @Test + public void getClasses() { + Method method = ReflectUtil.getMethod(Parent.class, "getLevel"); + Type returnType = TypeUtil.getReturnType(method); + Class clazz = TypeUtil.getClass(returnType); + Assert.assertEquals(Level1.class, clazz); + + method = ReflectUtil.getMethod(Level1.class, "getId"); + returnType = TypeUtil.getReturnType(method); + clazz = TypeUtil.getClass(returnType); + Assert.assertEquals(Object.class, clazz); + } + public static class TestClass { - public List getList(){ + public List getList() { return new ArrayList<>(); } - + public Integer intTest(Integer integer) { return 1; } + } @Test - public void getTypeArgumentTest(){ + public void getTypeArgumentTest() { // 测试不继承父类,而是实现泛型接口时是否可以获取成功。 final Type typeArgument = TypeUtil.getTypeArgument(IPService.class); Assert.assertEquals(String.class, typeArgument); @@ -59,25 +72,29 @@ public class TypeUtilTest { } @Test - public void getActualTypesTest(){ + public void getActualTypesTest() { // 测试多层级泛型参数是否能获取成功 - Type idType = TypeUtil.getActualType(Level3.class, - ReflectUtil.getField(Level3.class, "id")); + Type idType = TypeUtil.getActualType(Level3.class, ReflectUtil.getField(Level3.class, "id")); Assert.assertEquals(Long.class, idType); } - public static class Level3 extends Level2{ + public static class Level3 extends Level2 { } - public static class Level2 extends Level1{ + public static class Level2 extends Level1 { } @Data - public static class Level1{ + public static class Level1 { private T id; } + @Data + public static class Parent, B extends Long> { + private T level; + } + } From 7d3527bc3f8c56d66ffeffcaaf904197ee3de878 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 12 Oct 2023 21:00:33 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8Daop=E7=9A=84afterExceptio?= =?UTF-8?q?n=E6=97=A0=E6=B3=95=E7=94=9F=E6=95=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 ++- .../cn/hutool/aop/interceptor/CglibInterceptor.java | 10 +++++++--- .../hutool/aop/interceptor/SpringCglibInterceptor.java | 9 +++++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65ceaf82a..7ba249d73 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.23(2023-10-08) +# 5.8.23(2023-10-12) ### 🐣新特性 * 【json 】 改进TemporalAccessorSerializer支持dayOfMonth和month枚举名(issue#I82AM8@Gitee) @@ -14,6 +14,7 @@ * 【cron 】 修复Cron表达式range解析错误问题(issue#I82CSH@Gitee) * 【core 】 修复VersionComparator在极端数据排序时候违反了自反性问题(issue#I81N3H@Gitee) * 【json 】 修复JSONStrFormatter:format函数对于转义符号处理逻辑错误问题(issue#I84V6I@Gitee) +* 【core 】 修复aop的afterException无法生效问题(issue#3329@Github) ------------------------------------------------------------------------------------------------------------- # 5.8.22(2023-09-13) diff --git a/hutool-aop/src/main/java/cn/hutool/aop/interceptor/CglibInterceptor.java b/hutool-aop/src/main/java/cn/hutool/aop/interceptor/CglibInterceptor.java index f0f7f9e90..39381f725 100755 --- a/hutool-aop/src/main/java/cn/hutool/aop/interceptor/CglibInterceptor.java +++ b/hutool-aop/src/main/java/cn/hutool/aop/interceptor/CglibInterceptor.java @@ -43,10 +43,14 @@ public class CglibInterceptor implements MethodInterceptor, Serializable { try { // result = proxy.invokeSuper(obj, args); result = proxy.invoke(target, args); - } catch (InvocationTargetException e) { + } catch (final Throwable e) { + Throwable throwable = e; + if(throwable instanceof InvocationTargetException){ + throwable = ((InvocationTargetException) throwable).getTargetException(); + } // 异常回调(只捕获业务代码导致的异常,而非反射导致的异常) - if (aspect.afterException(target, method, args, e.getTargetException())) { - throw e; + if (aspect.afterException(target, method, args, throwable)) { + throw throwable; } } } diff --git a/hutool-aop/src/main/java/cn/hutool/aop/interceptor/SpringCglibInterceptor.java b/hutool-aop/src/main/java/cn/hutool/aop/interceptor/SpringCglibInterceptor.java index 8db483469..7a3697e77 100755 --- a/hutool-aop/src/main/java/cn/hutool/aop/interceptor/SpringCglibInterceptor.java +++ b/hutool-aop/src/main/java/cn/hutool/aop/interceptor/SpringCglibInterceptor.java @@ -48,9 +48,14 @@ public class SpringCglibInterceptor implements MethodInterceptor, Serializable { try { // result = proxy.invokeSuper(obj, args); result = proxy.invoke(target, args); - } catch (InvocationTargetException e) { + } catch (Throwable e) { + Throwable throwable = e; + if(throwable instanceof InvocationTargetException){ + throwable = ((InvocationTargetException) throwable).getTargetException(); + } + // 异常回调(只捕获业务代码导致的异常,而非反射导致的异常) - if (aspect.afterException(target, method, args, e.getTargetException())) { + if (aspect.afterException(target, method, args, throwable)) { throw e; } } From fd9e1efa2860ded5cf2d1be5954fd87a2a5830fe Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 12 Oct 2023 21:28:56 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8DTypeUtil.getClass?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=BC=BA=E8=BD=AC=E6=8A=A5=E9=94=99=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ba249d73..450ac5f0c 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * 【core 】 修复VersionComparator在极端数据排序时候违反了自反性问题(issue#I81N3H@Gitee) * 【json 】 修复JSONStrFormatter:format函数对于转义符号处理逻辑错误问题(issue#I84V6I@Gitee) * 【core 】 修复aop的afterException无法生效问题(issue#3329@Github) +* 【core 】 修复TypeUtil.getClass方法强转报错问题(pr#1092@Github) ------------------------------------------------------------------------------------------------------------- # 5.8.22(2023-09-13)