Просмотр исходного кода

修复无法获取重写了接口default方法上注解的问题

zhou-hao 7 лет назад
Родитель
Сommit
defa990afb

+ 30 - 2
hsweb-commons/hsweb-commons-utils/src/main/java/org/hswebframework/web/AopUtils.java

@@ -21,11 +21,12 @@ import org.aspectj.lang.JoinPoint;
 import org.aspectj.lang.reflect.MethodSignature;
 import org.springframework.core.annotation.AnnotationUtils;
 import org.springframework.util.ClassUtils;
+import org.springframework.util.ReflectionUtils;
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
-import java.util.LinkedHashMap;
-import java.util.Map;
+import java.util.*;
+import java.util.stream.Stream;
 
 public final class AopUtils {
 
@@ -40,6 +41,33 @@ public final class AopUtils {
         }
         m = ClassUtils.getMostSpecificMethod(m, targetClass);
         a = AnnotationUtils.findAnnotation(m, annClass);
+        if (a == null) {
+            List<Class> supers = new ArrayList<>();
+            supers.addAll(Arrays.asList(targetClass.getInterfaces()));
+            if (targetClass.getSuperclass() != Object.class) {
+                supers.add(targetClass.getSuperclass());
+            }
+
+            for (Class aClass : supers) {
+                if(aClass==null){
+                    continue;
+                }
+                Method ims[] = new Method[1];
+
+                ReflectionUtils.doWithMethods(aClass, im -> {
+                    if (im.getName().equals(method.getName()) && im.getParameterCount() == method.getParameterCount()) {
+                        ims[0] = im;
+                    }
+                });
+
+                if (ims[0] != null) {
+                    a = findMethodAnnotation(aClass, ims[0], annClass);
+                    if (a != null) {
+                        return a;
+                    }
+                }
+            }
+        }
         return a;
     }