Skip to content

Commit 934e011

Browse files
committed
Update DefaultInterceptorManager.java
1 parent 260f213 commit 934e011

File tree

1 file changed

+64
-62
lines changed

1 file changed

+64
-62
lines changed

projects/stage-1/middleware-frameworks/my-interceptor/src/main/java/org/geektimes/interceptor/DefaultInterceptorManager.java

+64-62
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
import static java.util.stream.Collectors.toList;
3636
import static org.geektimes.commons.lang.util.AnnotationUtils.findAnnotation;
3737
import static org.geektimes.commons.lang.util.AnnotationUtils.isMetaAnnotation;
38-
import static org.geektimes.interceptor.util.InterceptorUtils.searchAnnotation;
39-
import static org.geektimes.interceptor.util.InterceptorUtils.sortInterceptors;
38+
import static org.geektimes.interceptor.util.InterceptorUtils.*;
4039

4140
/**
4241
* Default {@link InterceptorManager}
@@ -47,10 +46,9 @@
4746
public class DefaultInterceptorManager implements InterceptorManager {
4847

4948
/**
50-
* The supported annotation types of interceptor binding mapping
51-
* the {@link Interceptor @Interceptor} {@link Class classes}.
49+
* The supported annotation types of interceptor binding.
5250
*/
53-
private final Map<Class<? extends Annotation>, Set<Class<?>>> bindingInterceptorClasses;
51+
private final Set<Class<? extends Annotation>> interceptorBindingTypes;
5452

5553
/**
5654
* The {@link InterceptorInfo} Repository
@@ -63,18 +61,29 @@ public class DefaultInterceptorManager implements InterceptorManager {
6361
private final Map<InterceptorBindings, SortedSet<Object>> bindingInterceptors;
6462

6563
/**
66-
* The cache for {@link Method} or {@link Constructor} mapping the prioritized {@link Interceptor @Interceptor} instances
64+
* The map the {@link InterceptorBinding interceptorBindings} or synthetic annotations to annotation types
6765
*/
68-
private final Map<Executable, List<Object>> interceptorsCache;
66+
private final Map<Annotation, Class<? extends Annotation>> interceptorBindings;
6967

70-
private final Map<Executable, List<InterceptorInfo>> interceptorInfoCache;
68+
/**
69+
* The cache for {@link Method} or {@link Constructor} mapping the prioritized {@link Interceptor @Interceptor}
70+
* instances
71+
*/
72+
private final Map<Executable, List<Object>> executableInterceptors;
73+
74+
/**
75+
* The cache for {@link Method} or {@link Constructor} mapping the prioritized {@link Interceptor @Interceptor}
76+
* {@link Class classes}
77+
*/
78+
private final Map<Executable, List<Class<?>>> executableInterceptorClasses;
7179

7280
public DefaultInterceptorManager() {
73-
this.bindingInterceptorClasses = new HashMap<>();
81+
this.interceptorBindings = new HashMap<>();
82+
this.interceptorBindingTypes = new HashSet<>();
7483
this.interceptorInfoRepository = new TreeMap<>(PriorityComparator.INSTANCE);
7584
this.bindingInterceptors = new HashMap<>();
76-
this.interceptorsCache = new HashMap<>();
77-
this.interceptorInfoCache = new HashMap<>();
85+
this.executableInterceptors = new HashMap<>();
86+
this.executableInterceptorClasses = new HashMap<>();
7887
registerDefaultInterceptorBindingType();
7988
}
8089

@@ -128,7 +137,7 @@ public InterceptorInfo getInterceptorInfo(Class<?> interceptorClass) throws Ille
128137

129138
@Override
130139
public List<Object> resolveInterceptors(Executable executable, Object... defaultInterceptors) {
131-
return interceptorsCache.computeIfAbsent(executable, e -> {
140+
return executableInterceptors.computeIfAbsent(executable, e -> {
132141

133142
List<Object> interceptors = new LinkedList<>();
134143

@@ -159,61 +168,37 @@ public List<Object> resolveInterceptors(Executable executable, Object... default
159168

160169
@Override
161170
public List<Class<?>> resolveInterceptorClasses(Executable executable, Class<?>... defaultInterceptorClasses) {
162-
List<InterceptorInfo> interceptorInfoList = interceptorInfoCache.computeIfAbsent(executable, e -> {
163-
164-
List<Class<?>> interceptorClasses = new LinkedList<>();
165-
166-
if (!isExcludedDefaultInterceptors(executable)) {
167-
// 1. Default interceptors are invoked first
168-
interceptorClasses.addAll(asList(defaultInterceptorClasses));
169-
}
170-
171-
// Resolve interceptors using @Interceptors
172-
// 2. Interceptors declared by applying the Interceptors annotation at class-level to the target
173-
// class are invoked next.
174-
// 3. Interceptors declared by applying the Interceptors annotation at method- or constructor-level
175-
// are invoked next.
176-
interceptorClasses.addAll(resolveAnnotatedInterceptorClasses(executable));
177-
178-
// Resolve interceptors using Interceptor Bindings
179-
// 4. Interceptors declared using interceptor bindings are invoked next.
180-
interceptorClasses.addAll(resolveBindingInterceptorClasses(executable));
181-
182-
// 5.2.1 Use of the Priority Annotation in Ordering Interceptors
183-
sortInterceptors(interceptorClasses);
184-
185-
return unmodifiableList(interceptorClasses);
186-
});
187-
188-
189-
return interceptorInfoList;
171+
return null;
190172
}
191173

192174

193175
@Override
194176
public void registerInterceptorBindingType(Class<? extends Annotation> interceptorBindingType) {
195-
registerInterceptorBinding(interceptorBindingType);
177+
this.interceptorBindingTypes.add(interceptorBindingType);
196178
}
197179

198180
@Override
199-
public void registerInterceptorBinding(Class<? extends Annotation> interceptorBindingType,
200-
Annotation... interceptorBindingDef) {
201-
Set<Class<?>> interceptorClasses = bindingInterceptorClasses.computeIfAbsent(interceptorBindingType,
202-
t -> new HashSet<>());
203-
181+
public void registerInterceptorBinding(Class<? extends Annotation> interceptorBindingType, Annotation... interceptorBindingDef) {
204182
// TODO
205183
}
206184

207185
@Override
208186
public boolean isInterceptorBindingType(Class<? extends Annotation> annotationType) {
209-
if (bindingInterceptorClasses.containsKey(annotationType)) {
210-
return true;
211-
}
212-
if (isMetaAnnotation(annotationType, InterceptorBinding.class)) {
187+
boolean valid = false;
188+
if (interceptorBindingTypes.contains(annotationType)) {
189+
valid = true;
190+
} else if (isAnnotatedInterceptorBinding(annotationType)) {
213191
registerInterceptorBindingType(annotationType);
214-
return true;
192+
valid = true;
193+
} else {
194+
for (Class<? extends Annotation> interceptorBindingType : interceptorBindingTypes) {
195+
if (isMetaAnnotation(annotationType, interceptorBindingType)) {
196+
registerInterceptorBindingType(annotationType);
197+
valid = true;
198+
}
199+
}
215200
}
216-
return false;
201+
return valid;
217202
}
218203

219204
@Override
@@ -223,7 +208,7 @@ public Set<Class<?>> getInterceptorClasses() {
223208

224209
@Override
225210
public Set<Class<? extends Annotation>> getInterceptorBindingTypes() {
226-
return bindingInterceptorClasses.keySet();
211+
return unmodifiableSet(interceptorBindingTypes);
227212
}
228213

229214
@Override
@@ -247,12 +232,10 @@ private void registerDefaultInterceptorBindingType() {
247232
}
248233

249234
private boolean isExcludedDefaultInterceptors(Executable executable) {
250-
return findAnnotation(executable, ExcludeDefaultInterceptors.class) != null;
251-
}
252-
253-
private List<Class<?>> resolveBindingInterceptorClasses(Executable executable) {
254-
InterceptorBindings interceptorBindings = resolveInterceptorBindings(executable);
255-
return unmodifiableSortedSet(bindingInterceptors.getOrDefault(interceptorBindings, emptySortedSet()));
235+
if (executable != null && !executable.isAnnotationPresent(ExcludeDefaultInterceptors.class)) {
236+
return findAnnotation(executable.getDeclaringClass(), ExcludeDefaultInterceptors.class) != null;
237+
}
238+
return false;
256239
}
257240

258241
private SortedSet<Object> resolveBindingInterceptors(Executable executable) {
@@ -271,7 +254,7 @@ private SortedSet<Object> resolveBindingInterceptors(Executable executable) {
271254
* @see Interceptors
272255
* @see ExcludeClassInterceptors
273256
*/
274-
protected List<Class<?>> resolveAnnotatedInterceptorClasses(Executable executable) {
257+
private List<Class<?>> resolveAnnotatedInterceptorClasses(Executable executable) {
275258
Class<?> componentClass = executable.getDeclaringClass();
276259

277260
List<Class<?>> interceptorClasses = new LinkedList<>();
@@ -306,8 +289,27 @@ protected List<Class<?>> resolveAnnotatedInterceptorClasses(Executable executabl
306289
* @see Interceptors
307290
* @see ExcludeClassInterceptors
308291
*/
309-
protected List<Object> resolveAnnotatedInterceptors(Executable executable) {
310-
List<Class<?>> interceptorClasses = resolveAnnotatedInterceptorClasses(executable);
292+
private List<Object> resolveAnnotatedInterceptors(Executable executable) {
293+
Class<?> componentClass = executable.getDeclaringClass();
294+
295+
List<Class<?>> interceptorClasses = new LinkedList<>();
296+
297+
if (!executable.isAnnotationPresent(ExcludeClassInterceptors.class)) {
298+
Interceptors classInterceptors = searchAnnotation(componentClass, Interceptors.class);
299+
if (classInterceptors != null) {
300+
for (Class interceptorClass : classInterceptors.value()) {
301+
interceptorClasses.add(interceptorClass);
302+
}
303+
}
304+
}
305+
306+
Interceptors executableInterceptors = searchAnnotation(executable, Interceptors.class);
307+
if (executableInterceptors != null) {
308+
for (Class interceptorClass : executableInterceptors.value()) {
309+
interceptorClasses.add(interceptorClass);
310+
}
311+
}
312+
311313
return interceptorClasses.stream()
312314
.map(InterceptorUtils::unwrap)
313315
.collect(toList());

0 commit comments

Comments
 (0)