|
18 | 18 |
|
19 | 19 | import java.lang.annotation.Annotation;
|
20 | 20 | import java.lang.reflect.AnnotatedElement;
|
| 21 | +import java.lang.reflect.Executable; |
21 | 22 | import java.lang.reflect.Method;
|
22 | 23 | import java.lang.reflect.Parameter;
|
23 | 24 | import java.util.ArrayList;
|
| 25 | +import java.util.Arrays; |
24 | 26 | import java.util.Collections;
|
25 | 27 | import java.util.HashSet;
|
26 | 28 | import java.util.List;
|
|
29 | 31 | import java.util.concurrent.ConcurrentHashMap;
|
30 | 32 |
|
31 | 33 | import org.springframework.core.MethodClassKey;
|
| 34 | +import org.springframework.core.ResolvableType; |
32 | 35 | import org.springframework.core.annotation.AnnotationConfigurationException;
|
33 | 36 | import org.springframework.core.annotation.MergedAnnotation;
|
34 | 37 | import org.springframework.core.annotation.MergedAnnotations;
|
@@ -103,11 +106,75 @@ final class UniqueSecurityAnnotationScanner<A extends Annotation> extends Abstra
|
103 | 106 | this.types = types;
|
104 | 107 | }
|
105 | 108 |
|
| 109 | + private List<MergedAnnotation<A>> findParameterOnInterface(Method method, Class<?> superOrIfc, Parameter current) { |
| 110 | + List<MergedAnnotation<A>> directAnnotations = Collections.emptyList(); |
| 111 | + for (Method candidate : superOrIfc.getMethods()) { |
| 112 | + if (isOverrideFor(method, candidate)) { |
| 113 | + for (Parameter parameter : candidate.getParameters()) { |
| 114 | + if (parameter.getName().equals(current.getName())) { |
| 115 | + directAnnotations = findDirectAnnotations(parameter); |
| 116 | + if (!directAnnotations.isEmpty()) { |
| 117 | + return directAnnotations; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + return directAnnotations; |
| 124 | + } |
| 125 | + |
| 126 | + private List<MergedAnnotation<A>> findParameterAnnotations(Parameter current) { |
| 127 | + List<MergedAnnotation<A>> directAnnotations = findDirectAnnotations(current); |
| 128 | + if (directAnnotations.isEmpty()) { |
| 129 | + Executable executable = current.getDeclaringExecutable(); |
| 130 | + if (executable instanceof Method method) { |
| 131 | + Class<?> clazz = method.getDeclaringClass(); |
| 132 | + while (clazz != null) { |
| 133 | + for (Class<?> ifc : clazz.getInterfaces()) { |
| 134 | + directAnnotations = findParameterOnInterface(method, ifc, current); |
| 135 | + if (!directAnnotations.isEmpty()) { |
| 136 | + return directAnnotations; |
| 137 | + } |
| 138 | + } |
| 139 | + clazz = clazz.getSuperclass(); |
| 140 | + if (clazz == Object.class) { |
| 141 | + clazz = null; |
| 142 | + } |
| 143 | + if (clazz != null) { |
| 144 | + directAnnotations = findParameterOnInterface(method, clazz, current); |
| 145 | + if (!directAnnotations.isEmpty()) { |
| 146 | + return directAnnotations; |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + return directAnnotations; |
| 153 | + } |
| 154 | + |
| 155 | + private boolean isOverrideFor(Method method, Method candidate) { |
| 156 | + if (!candidate.getName().equals(method.getName()) |
| 157 | + || candidate.getParameterCount() != method.getParameterCount()) { |
| 158 | + return false; |
| 159 | + } |
| 160 | + Class<?>[] paramTypes = method.getParameterTypes(); |
| 161 | + if (Arrays.equals(candidate.getParameterTypes(), paramTypes)) { |
| 162 | + return true; |
| 163 | + } |
| 164 | + for (int i = 0; i < paramTypes.length; i++) { |
| 165 | + if (paramTypes[i] != ResolvableType.forMethodParameter(candidate, i, method.getDeclaringClass()) |
| 166 | + .resolve()) { |
| 167 | + return false; |
| 168 | + } |
| 169 | + } |
| 170 | + return true; |
| 171 | + } |
| 172 | + |
106 | 173 | @Override
|
107 | 174 | MergedAnnotation<A> merge(AnnotatedElement element, Class<?> targetClass) {
|
108 | 175 | if (element instanceof Parameter parameter) {
|
109 | 176 | return this.uniqueParameterAnnotationCache.computeIfAbsent(parameter, (p) -> {
|
110 |
| - List<MergedAnnotation<A>> annotations = findDirectAnnotations(p); |
| 177 | + List<MergedAnnotation<A>> annotations = findParameterAnnotations(p); |
111 | 178 | return requireUnique(p, annotations);
|
112 | 179 | });
|
113 | 180 | }
|
|
0 commit comments