From c32e8d9b55cec5e93530a486c08936965a2b1025 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=BE=BD=E9=93=96?= Date: Wed, 25 Sep 2019 10:05:38 +0800 Subject: [PATCH 1/6] =?UTF-8?q?1=E3=80=81=E5=88=87=E9=9D=A2after=E5=88=87?= =?UTF-8?q?=E7=82=B9=EF=BC=8C=E5=B0=86=E7=9B=AE=E6=A0=87=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E8=BF=94=E5=9B=9E=E5=80=BC=EF=BC=8C=E5=BC=80?= =?UTF-8?q?=E6=94=BE=E7=BB=99=E5=88=87=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hutool-aop/src/main/java/cn/hutool/aop/aspects/Aspect.java | 3 ++- .../src/main/java/cn/hutool/aop/aspects/SimpleAspect.java | 2 +- .../main/java/cn/hutool/aop/aspects/TimeIntervalAspect.java | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/hutool-aop/src/main/java/cn/hutool/aop/aspects/Aspect.java b/hutool-aop/src/main/java/cn/hutool/aop/aspects/Aspect.java index ebb020b3f..aea0d826b 100644 --- a/hutool-aop/src/main/java/cn/hutool/aop/aspects/Aspect.java +++ b/hutool-aop/src/main/java/cn/hutool/aop/aspects/Aspect.java @@ -26,9 +26,10 @@ public interface Aspect{ * @param target 目标对象 * @param method 目标方法 * @param args 参数 + * @param returnVal 目标方法执行返回值 * @return 是否允许返回值(接下来的操作) */ - boolean after(Object target, Method method, Object[] args); + boolean after(Object target, Method method, Object[] args, Object returnVal); /** * 目标方法抛出异常时的操作 diff --git a/hutool-aop/src/main/java/cn/hutool/aop/aspects/SimpleAspect.java b/hutool-aop/src/main/java/cn/hutool/aop/aspects/SimpleAspect.java index b327e9c02..4e5d2bc93 100644 --- a/hutool-aop/src/main/java/cn/hutool/aop/aspects/SimpleAspect.java +++ b/hutool-aop/src/main/java/cn/hutool/aop/aspects/SimpleAspect.java @@ -20,7 +20,7 @@ public class SimpleAspect implements Aspect, Serializable{ } @Override - public boolean after(Object target, Method method, Object[] args) { + public boolean after(Object target, Method method, Object[] args, Object returnVal) { //继承此类后实现此方法 return true; } diff --git a/hutool-aop/src/main/java/cn/hutool/aop/aspects/TimeIntervalAspect.java b/hutool-aop/src/main/java/cn/hutool/aop/aspects/TimeIntervalAspect.java index 4ea3fac91..df239984b 100644 --- a/hutool-aop/src/main/java/cn/hutool/aop/aspects/TimeIntervalAspect.java +++ b/hutool-aop/src/main/java/cn/hutool/aop/aspects/TimeIntervalAspect.java @@ -22,7 +22,7 @@ public class TimeIntervalAspect extends SimpleAspect{ } @Override - public boolean after(Object target, Method method, Object[] args) { + public boolean after(Object target, Method method, Object[] args, Object returnVal) { Console.log("Method [{}.{}] execute spend [{}]ms", target.getClass().getName(), method.getName(), interval.intervalMs()); return true; } From 34cc3dcb57738a0fffe5f92f44f9c5415ba618ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=BE=BD=E9=93=96?= Date: Wed, 25 Sep 2019 10:16:40 +0800 Subject: [PATCH 2/6] =?UTF-8?q?1=E3=80=81=E5=88=87=E9=9D=A2after=E5=88=87?= =?UTF-8?q?=E7=82=B9=EF=BC=8C=E5=B0=86=E7=9B=AE=E6=A0=87=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E8=BF=94=E5=9B=9E=E5=80=BC=EF=BC=8C=E5=BC=80?= =?UTF-8?q?=E6=94=BE=E7=BB=99=E5=88=87=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hutool-aop/src/main/java/cn/hutool/aop/aspects/Aspect.java | 1 + .../main/java/cn/hutool/aop/interceptor/CglibInterceptor.java | 2 +- .../src/main/java/cn/hutool/aop/interceptor/JdkInterceptor.java | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/hutool-aop/src/main/java/cn/hutool/aop/aspects/Aspect.java b/hutool-aop/src/main/java/cn/hutool/aop/aspects/Aspect.java index aea0d826b..a01009e32 100644 --- a/hutool-aop/src/main/java/cn/hutool/aop/aspects/Aspect.java +++ b/hutool-aop/src/main/java/cn/hutool/aop/aspects/Aspect.java @@ -6,6 +6,7 @@ import java.lang.reflect.Method; * 切面接口 * * @author looly + * @author ted.L * @since 4.18 */ public interface Aspect{ 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 b4144753a..c7cccb5ba 100644 --- a/hutool-aop/src/main/java/cn/hutool/aop/interceptor/CglibInterceptor.java +++ b/hutool-aop/src/main/java/cn/hutool/aop/interceptor/CglibInterceptor.java @@ -52,7 +52,7 @@ public class CglibInterceptor implements MethodInterceptor, Serializable { } } } - if (aspect.after(target, method, args)) { + if (aspect.after(target, method, args, result)) { return result; } return null; diff --git a/hutool-aop/src/main/java/cn/hutool/aop/interceptor/JdkInterceptor.java b/hutool-aop/src/main/java/cn/hutool/aop/interceptor/JdkInterceptor.java index 0969eb11f..7c39c476d 100644 --- a/hutool-aop/src/main/java/cn/hutool/aop/interceptor/JdkInterceptor.java +++ b/hutool-aop/src/main/java/cn/hutool/aop/interceptor/JdkInterceptor.java @@ -53,7 +53,7 @@ public class JdkInterceptor implements InvocationHandler, Serializable{ } } } - if (aspect.after(target, method, args)) { + if (aspect.after(target, method, args, result)) { return result; } return null; From 84875f0ff94799a1c41ce2f6a588664e5283cf63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=BE=BD=E9=93=96?= Date: Wed, 25 Sep 2019 10:17:25 +0800 Subject: [PATCH 3/6] =?UTF-8?q?1=E3=80=81=E5=88=87=E9=9D=A2after=E5=88=87?= =?UTF-8?q?=E7=82=B9=EF=BC=8C=E5=B0=86=E7=9B=AE=E6=A0=87=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E8=BF=94=E5=9B=9E=E5=80=BC=EF=BC=8C=E5=BC=80?= =?UTF-8?q?=E6=94=BE=E7=BB=99=E5=88=87=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/aop/interceptor/CglibInterceptor.java | 1 + .../src/main/java/cn/hutool/aop/interceptor/JdkInterceptor.java | 1 + 2 files changed, 2 insertions(+) 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 c7cccb5ba..2c9e253ee 100644 --- a/hutool-aop/src/main/java/cn/hutool/aop/interceptor/CglibInterceptor.java +++ b/hutool-aop/src/main/java/cn/hutool/aop/interceptor/CglibInterceptor.java @@ -13,6 +13,7 @@ import net.sf.cglib.proxy.MethodProxy; * Cglib实现的动态代理切面 * * @author looly + * @author ted.L * */ public class CglibInterceptor implements MethodInterceptor, Serializable { diff --git a/hutool-aop/src/main/java/cn/hutool/aop/interceptor/JdkInterceptor.java b/hutool-aop/src/main/java/cn/hutool/aop/interceptor/JdkInterceptor.java index 7c39c476d..52b433016 100644 --- a/hutool-aop/src/main/java/cn/hutool/aop/interceptor/JdkInterceptor.java +++ b/hutool-aop/src/main/java/cn/hutool/aop/interceptor/JdkInterceptor.java @@ -13,6 +13,7 @@ import cn.hutool.core.util.ReflectUtil; * JDK实现的动态代理切面 * * @author Looly + * @author ted.L * */ public class JdkInterceptor implements InvocationHandler, Serializable{ From 1df8f968d02df090843fbb29c48a9c1331010d17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=BE=BD=E9=93=96?= Date: Wed, 25 Sep 2019 11:04:46 +0800 Subject: [PATCH 4/6] =?UTF-8?q?1=E3=80=81=E5=88=87=E9=9D=A2after=E5=88=87?= =?UTF-8?q?=E7=82=B9=EF=BC=8C=E5=B0=86=E7=9B=AE=E6=A0=87=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E8=BF=94=E5=9B=9E=E5=80=BC=EF=BC=8C=E5=BC=80?= =?UTF-8?q?=E6=94=BE=E7=BB=99=E5=88=87=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aop/aspects/TimeIntervalAspect.java | 29 +++--- .../test/java/cn/hutool/aop/test/AopTest.java | 89 +++++++++++-------- 2 files changed, 66 insertions(+), 52 deletions(-) diff --git a/hutool-aop/src/main/java/cn/hutool/aop/aspects/TimeIntervalAspect.java b/hutool-aop/src/main/java/cn/hutool/aop/aspects/TimeIntervalAspect.java index df239984b..3c6fa66cb 100644 --- a/hutool-aop/src/main/java/cn/hutool/aop/aspects/TimeIntervalAspect.java +++ b/hutool-aop/src/main/java/cn/hutool/aop/aspects/TimeIntervalAspect.java @@ -4,26 +4,27 @@ import java.lang.reflect.Method; import cn.hutool.core.date.TimeInterval; import cn.hutool.core.lang.Console; +import cn.hutool.core.util.StrUtil; /** * 通过日志打印方法的执行时间的切面 * @author Looly * */ -public class TimeIntervalAspect extends SimpleAspect{ - private static final long serialVersionUID = 1L; +public class TimeIntervalAspect extends SimpleAspect { + private static final long serialVersionUID = 1L; - private TimeInterval interval = new TimeInterval(); + private TimeInterval interval = new TimeInterval(); - @Override - public boolean before(Object target, Method method, Object[] args) { - interval.start(); - return true; - } - - @Override - public boolean after(Object target, Method method, Object[] args, Object returnVal) { - Console.log("Method [{}.{}] execute spend [{}]ms", target.getClass().getName(), method.getName(), interval.intervalMs()); - return true; - } + @Override + public boolean before(Object target, Method method, Object[] args) { + interval.start(); + return true; + } + + @Override + public boolean after(Object target, Method method, Object[] args, Object returnVal) { + Console.log("Method [{}.{}] execute spend [{}]ms return value [{}]", target.getClass().getName(), method.getName(), interval.intervalMs(), StrUtil.toString(returnVal)); + return true; + } } diff --git a/hutool-aop/src/test/java/cn/hutool/aop/test/AopTest.java b/hutool-aop/src/test/java/cn/hutool/aop/test/AopTest.java index 31f12325a..6ca1b9800 100644 --- a/hutool-aop/src/test/java/cn/hutool/aop/test/AopTest.java +++ b/hutool-aop/src/test/java/cn/hutool/aop/test/AopTest.java @@ -14,47 +14,60 @@ import cn.hutool.aop.aspects.TimeIntervalAspect; */ public class AopTest { - @Test - public void aopTest() { - Animal cat = ProxyUtil.proxy(new Cat(), TimeIntervalAspect.class); - String result = cat.eat(); - Assert.assertEquals("猫吃鱼", result); - } + @Test + public void aopTest() { + Animal cat = ProxyUtil.proxy(new Cat(), TimeIntervalAspect.class); + String result = cat.eat(); + Assert.assertEquals("猫吃鱼", result); + cat.seize(); + } - @Test - public void aopByCglibTest() { - Dog dog = ProxyUtil.proxy(new Dog(), TimeIntervalAspect.class); - String result = dog.eat(); - Assert.assertEquals("狗吃肉", result); - } + @Test + public void aopByCglibTest() { + Dog dog = ProxyUtil.proxy(new Dog(), TimeIntervalAspect.class); + String result = dog.eat(); + Assert.assertEquals("狗吃肉", result); + dog.seize(); + } - static interface Animal { - String eat(); - } + interface Animal { + String eat(); - /** - * 有接口 - * - * @author looly - * - */ - static class Cat implements Animal { + void seize(); + } - @Override - public String eat() { - return "猫吃鱼"; - } - } + /** + * 有接口 + * + * @author looly + * + */ + static class Cat implements Animal { - /** - * 无接口 - * - * @author looly - * - */ - static class Dog { - public String eat() { - return "狗吃肉"; - } - } + @Override + public String eat() { + return "猫吃鱼"; + } + + @Override + public void seize() { + System.out.println("抓了条鱼"); + } + } + + /** + * 无接口 + * + * @author looly + * + */ + static class Dog { + public String eat() { + return "狗吃肉"; + } + + public void seize() { + System.out.println("抓了只鸡"); + } + } } From 130a348afc9aeb4676a7f42b7ccb898db82acce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=BE=BD=E9=93=96?= Date: Wed, 25 Sep 2019 11:09:21 +0800 Subject: [PATCH 5/6] =?UTF-8?q?1=E3=80=81=E5=88=87=E9=9D=A2after=E5=88=87?= =?UTF-8?q?=E7=82=B9=EF=BC=8C=E5=B0=86=E7=9B=AE=E6=A0=87=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E8=BF=94=E5=9B=9E=E5=80=BC=EF=BC=8C=E5=BC=80?= =?UTF-8?q?=E6=94=BE=E7=BB=99=E5=88=87=E7=82=B9=202=E3=80=81SimpleAspect?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E6=84=8F=E4=B9=89=EF=BC=8C=E5=BA=94=E4=BF=AE=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E6=8A=BD=E8=B1=A1=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/hutool/aop/aspects/SimpleAspect.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hutool-aop/src/main/java/cn/hutool/aop/aspects/SimpleAspect.java b/hutool-aop/src/main/java/cn/hutool/aop/aspects/SimpleAspect.java index 4e5d2bc93..316723c20 100644 --- a/hutool-aop/src/main/java/cn/hutool/aop/aspects/SimpleAspect.java +++ b/hutool-aop/src/main/java/cn/hutool/aop/aspects/SimpleAspect.java @@ -8,9 +8,10 @@ import java.lang.reflect.Method; * 可以继承此类实现自己需要的方法即可 * * @author Looly + * @author ted.L * */ -public class SimpleAspect implements Aspect, Serializable{ +public abstract class SimpleAspect implements Aspect, Serializable{ private static final long serialVersionUID = 1L; @Override From 22a51ba86de31ad0f72befa70bc479ed83abaeac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=BE=BD=E9=93=96?= Date: Wed, 25 Sep 2019 11:36:03 +0800 Subject: [PATCH 6/6] =?UTF-8?q?1=E3=80=81=E5=88=87=E9=9D=A2after=E5=88=87?= =?UTF-8?q?=E7=82=B9=EF=BC=8C=E5=B0=86=E7=9B=AE=E6=A0=87=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E8=BF=94=E5=9B=9E=E5=80=BC=EF=BC=8C=E5=BC=80?= =?UTF-8?q?=E6=94=BE=E7=BB=99=E5=88=87=E7=82=B9=202=E3=80=81SimpleAspect?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E6=84=8F=E4=B9=89=EF=BC=8C=E5=BA=94=E4=BF=AE=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E6=8A=BD=E8=B1=A1=E7=B1=BB=203=E3=80=81=E4=BF=AE=E6=94=B9Aspec?= =?UTF-8?q?t#afterException=20=E6=B2=A1=E6=9C=89=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=80=BC=E8=BF=9B=E8=A1=8C=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E6=8A=9B=E5=87=BA=E7=9A=84=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E3=80=82=204=E3=80=81=E4=BF=AE=E6=94=B9CglibInterceptor#interc?= =?UTF-8?q?ept=20=E5=92=8C=20JdkInterceptor#intercept=20=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=B8=AD=20if=20else=20=E4=B8=BA=20if=20=EF=BC=8C=E5=87=8F?= =?UTF-8?q?=E5=B0=91else=E5=88=A4=E6=96=AD,=E5=B9=B6=E6=94=AF=E6=8C=81=20?= =?UTF-8?q?=E7=AC=AC3=E7=82=B9=E7=9A=84=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/hutool/aop/aspects/Aspect.java | 6 +++++- .../java/cn/hutool/aop/aspects/SimpleAspect.java | 12 ++++++++++++ .../cn/hutool/aop/interceptor/CglibInterceptor.java | 11 +++++++---- .../cn/hutool/aop/interceptor/JdkInterceptor.java | 10 ++++++---- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/hutool-aop/src/main/java/cn/hutool/aop/aspects/Aspect.java b/hutool-aop/src/main/java/cn/hutool/aop/aspects/Aspect.java index a01009e32..5ab798b5c 100644 --- a/hutool-aop/src/main/java/cn/hutool/aop/aspects/Aspect.java +++ b/hutool-aop/src/main/java/cn/hutool/aop/aspects/Aspect.java @@ -23,7 +23,11 @@ public interface Aspect{ /** * 目标方法执行后的操作 - * + * 如果 target.method 抛出异常且 + * @see Aspect#afterException 返回true,则不会执行此操作 + * 如果 + * @see Aspect#afterException 返回false,则无论target.method是否抛出异常,均会执行此操作 + * * @param target 目标对象 * @param method 目标方法 * @param args 参数 diff --git a/hutool-aop/src/main/java/cn/hutool/aop/aspects/SimpleAspect.java b/hutool-aop/src/main/java/cn/hutool/aop/aspects/SimpleAspect.java index 316723c20..c118a7056 100644 --- a/hutool-aop/src/main/java/cn/hutool/aop/aspects/SimpleAspect.java +++ b/hutool-aop/src/main/java/cn/hutool/aop/aspects/SimpleAspect.java @@ -14,18 +14,30 @@ import java.lang.reflect.Method; public abstract class SimpleAspect implements Aspect, Serializable{ private static final long serialVersionUID = 1L; + /** + * @see Aspect#before(Object, Method, Object[]) + * @return 是否继续执行接下来的操作 默认值true + */ @Override public boolean before(Object target, Method method, Object[] args) { //继承此类后实现此方法 return true; } + /** + * @see Aspect#after(Object, Method, Object[], Object) + * @return 是否允许返回值(接下来的操作) 默认值true + */ @Override public boolean after(Object target, Method method, Object[] args, Object returnVal) { //继承此类后实现此方法 return true; } + /** + * @see Aspect#afterException(Object, Method, Object[], Throwable) + * @return 是否允许抛出异常 默认值true + */ @Override public boolean afterException(Object target, Method method, Object[] args, Throwable e) { //继承此类后实现此方法 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 2c9e253ee..8c53df4cd 100644 --- a/hutool-aop/src/main/java/cn/hutool/aop/interceptor/CglibInterceptor.java +++ b/hutool-aop/src/main/java/cn/hutool/aop/interceptor/CglibInterceptor.java @@ -46,11 +46,14 @@ public class CglibInterceptor implements MethodInterceptor, Serializable { result = proxy.invokeSuper(obj, args); } catch (UtilException e) { final Throwable cause = e.getCause(); - if (e.getCause() instanceof InvocationTargetException) { - aspect.afterException(target, method, args, ((InvocationTargetException) cause).getTargetException()); - } else { - throw e;// 其它异常属于代理的异常,直接抛出 + if (!(e.getCause() instanceof InvocationTargetException)) { + // 其它异常属于代理的异常,直接抛出 + throw e; } + if(aspect.afterException(target, method, args, ((InvocationTargetException) cause).getTargetException())){ + throw e; + } + } } if (aspect.after(target, method, args, result)) { diff --git a/hutool-aop/src/main/java/cn/hutool/aop/interceptor/JdkInterceptor.java b/hutool-aop/src/main/java/cn/hutool/aop/interceptor/JdkInterceptor.java index 52b433016..73b89ba83 100644 --- a/hutool-aop/src/main/java/cn/hutool/aop/interceptor/JdkInterceptor.java +++ b/hutool-aop/src/main/java/cn/hutool/aop/interceptor/JdkInterceptor.java @@ -47,10 +47,12 @@ public class JdkInterceptor implements InvocationHandler, Serializable{ result = ReflectUtil.invoke(target, method, args); } catch (UtilException e) { final Throwable cause = e.getCause(); - if (e.getCause() instanceof InvocationTargetException) { - aspect.afterException(target, method, args, ((InvocationTargetException) cause).getTargetException()); - } else { - throw e;// 其它异常属于代理的异常,直接抛出 + if (!(e.getCause() instanceof InvocationTargetException)) { + // 其它异常属于代理的异常,直接抛出 + throw e; + } + if(aspect.afterException(target, method, args, ((InvocationTargetException) cause).getTargetException())){ + throw e; } } }