From 78e1fb8bc24fac6cb5316b9b70595726d423f215 Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Tue, 28 Jun 2022 18:50:43 +0800 Subject: [PATCH] =?UTF-8?q?aop=E4=BC=98=E5=8C=96=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=BB=A3=E7=90=86=E6=8E=A5=E5=8F=A3,=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BB=A3=E7=90=86=E6=94=AF=E6=8C=81=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E5=99=A8=E7=BB=A7=E6=89=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aop/interceptor/InterceptorFactory.java | 13 +++++-- .../asgc/neutrino/core/util/ReflectUtil.java | 30 ++++++++++++++++ .../fun/asgc/neutrino/core/aop/Mammal.java | 34 +++++++++++++++++++ .../fun/asgc/neutrino/core/aop/Test3.java | 9 +++++ 4 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Mammal.java diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java index 95c549dc..8c3a5126 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java @@ -24,9 +24,7 @@ package fun.asgc.neutrino.core.aop.interceptor; import fun.asgc.neutrino.core.aop.Intercept; import fun.asgc.neutrino.core.cache.Cache; import fun.asgc.neutrino.core.cache.MemoryCache; -import fun.asgc.neutrino.core.util.ArrayUtil; -import fun.asgc.neutrino.core.util.Assert; -import fun.asgc.neutrino.core.util.LockUtil; +import fun.asgc.neutrino.core.util.*; import java.lang.reflect.Method; import java.util.*; @@ -67,6 +65,15 @@ public class InterceptorFactory { () -> { List interceptors = new ArrayList<>(); interceptors.addAll(globalInterceptorList); + // 如果被代理方法所属类是一个接口,那么该接口所有继承接口链路上的注解都对该方法生效 + if (ClassUtil.isInterface(targetMethod.getDeclaringClass())) { + List> interfaceList = ReflectUtil.getInterfaceAll(targetMethod.getDeclaringClass()); + if (CollectionUtil.notEmpty(interfaceList)) { + for (Class clazz : interfaceList) { + addInterceptorByAnnotation(interceptors, clazz.getAnnotation(Intercept.class)); + } + } + } addInterceptorByAnnotation(interceptors, targetMethod.getDeclaringClass().getAnnotation(Intercept.class)); addInterceptorByAnnotation(interceptors, targetMethod.getAnnotation(Intercept.class)); methodInterceptorListMap.put(targetMethod, interceptors); diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ReflectUtil.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ReflectUtil.java index 85d7265b..9948ea6f 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ReflectUtil.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/ReflectUtil.java @@ -23,6 +23,7 @@ package fun.asgc.neutrino.core.util; import com.google.common.collect.Sets; +import fun.asgc.neutrino.core.base.Orderly; import fun.asgc.neutrino.core.cache.Cache; import fun.asgc.neutrino.core.cache.MemoryCache; import fun.asgc.neutrino.core.type.TypeMatchLevel; @@ -377,4 +378,33 @@ public class ReflectUtil { } return false; } + + /** + * 获取该类的所有接口,并保持按照接口层级的顺序(顶级在最前、其次是第二级、...) + * @param clazz + * @return + */ + public static List> getInterfaceAll(Class clazz) { + List>> list = new ArrayList<>(); + if (ArrayUtil.notEmpty(clazz.getInterfaces())) { + addTo(list, clazz.getInterfaces(), 0); + int current = 0; + while (current < list.size()) { + Orderly> peek = list.get(current); + if (ArrayUtil.notEmpty(peek.getData().getInterfaces())) { + addTo(list, peek.getData().getInterfaces(), peek.getOrder() + 1); + } + current++; + } + } + return list.stream().sorted().map(Orderly::getData).collect(Collectors.toList()); + } + + private static void addTo(List>> list, Class[] classes, int level) { + if (ArrayUtil.notEmpty(classes)) { + for (Class clazz : classes) { + list.add(new Orderly>().setData(clazz).setOrder(Integer.MAX_VALUE - level)); + } + } + } } diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Mammal.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Mammal.java new file mode 100644 index 00000000..08307b7b --- /dev/null +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Mammal.java @@ -0,0 +1,34 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.aop; + +/** + * 哺乳动物 + * @author: aoshiguchen + * @date: 2022/6/28 + */ +public interface Mammal extends Animal { + /** + * 爬行 + */ + void crawl(); +} diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Test3.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Test3.java index 7209341f..7c23ba2b 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Test3.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Test3.java @@ -21,6 +21,7 @@ */ package fun.asgc.neutrino.core.aop; +import fun.asgc.neutrino.core.util.ReflectUtil; import org.junit.Test; /** @@ -36,4 +37,12 @@ public class Test3 { System.out.println(animal); System.out.println(animal.say("aa")); } + + @Test + public void test2() { + Mammal mammal = Aop.get(Mammal.class); + mammal.crawl(); + + System.out.println(ReflectUtil.getInterfaceAll(Mammal.class)); + } }