35
35
import static java .util .stream .Collectors .toList ;
36
36
import static org .geektimes .commons .lang .util .AnnotationUtils .findAnnotation ;
37
37
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 .*;
40
39
41
40
/**
42
41
* Default {@link InterceptorManager}
47
46
public class DefaultInterceptorManager implements InterceptorManager {
48
47
49
48
/**
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.
52
50
*/
53
- private final Map <Class <? extends Annotation >, Set < Class <?>>> bindingInterceptorClasses ;
51
+ private final Set <Class <? extends Annotation >> interceptorBindingTypes ;
54
52
55
53
/**
56
54
* The {@link InterceptorInfo} Repository
@@ -63,18 +61,29 @@ public class DefaultInterceptorManager implements InterceptorManager {
63
61
private final Map <InterceptorBindings , SortedSet <Object >> bindingInterceptors ;
64
62
65
63
/**
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
67
65
*/
68
- private final Map <Executable , List < Object >> interceptorsCache ;
66
+ private final Map <Annotation , Class <? extends Annotation >> interceptorBindings ;
69
67
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 ;
71
79
72
80
public DefaultInterceptorManager () {
73
- this .bindingInterceptorClasses = new HashMap <>();
81
+ this .interceptorBindings = new HashMap <>();
82
+ this .interceptorBindingTypes = new HashSet <>();
74
83
this .interceptorInfoRepository = new TreeMap <>(PriorityComparator .INSTANCE );
75
84
this .bindingInterceptors = new HashMap <>();
76
- this .interceptorsCache = new HashMap <>();
77
- this .interceptorInfoCache = new HashMap <>();
85
+ this .executableInterceptors = new HashMap <>();
86
+ this .executableInterceptorClasses = new HashMap <>();
78
87
registerDefaultInterceptorBindingType ();
79
88
}
80
89
@@ -128,7 +137,7 @@ public InterceptorInfo getInterceptorInfo(Class<?> interceptorClass) throws Ille
128
137
129
138
@ Override
130
139
public List <Object > resolveInterceptors (Executable executable , Object ... defaultInterceptors ) {
131
- return interceptorsCache .computeIfAbsent (executable , e -> {
140
+ return executableInterceptors .computeIfAbsent (executable , e -> {
132
141
133
142
List <Object > interceptors = new LinkedList <>();
134
143
@@ -159,61 +168,37 @@ public List<Object> resolveInterceptors(Executable executable, Object... default
159
168
160
169
@ Override
161
170
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 ;
190
172
}
191
173
192
174
193
175
@ Override
194
176
public void registerInterceptorBindingType (Class <? extends Annotation > interceptorBindingType ) {
195
- registerInterceptorBinding (interceptorBindingType );
177
+ this . interceptorBindingTypes . add (interceptorBindingType );
196
178
}
197
179
198
180
@ 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 ) {
204
182
// TODO
205
183
}
206
184
207
185
@ Override
208
186
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 )) {
213
191
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
+ }
215
200
}
216
- return false ;
201
+ return valid ;
217
202
}
218
203
219
204
@ Override
@@ -223,7 +208,7 @@ public Set<Class<?>> getInterceptorClasses() {
223
208
224
209
@ Override
225
210
public Set <Class <? extends Annotation >> getInterceptorBindingTypes () {
226
- return bindingInterceptorClasses . keySet ( );
211
+ return unmodifiableSet ( interceptorBindingTypes );
227
212
}
228
213
229
214
@ Override
@@ -247,12 +232,10 @@ private void registerDefaultInterceptorBindingType() {
247
232
}
248
233
249
234
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 ;
256
239
}
257
240
258
241
private SortedSet <Object > resolveBindingInterceptors (Executable executable ) {
@@ -271,7 +254,7 @@ private SortedSet<Object> resolveBindingInterceptors(Executable executable) {
271
254
* @see Interceptors
272
255
* @see ExcludeClassInterceptors
273
256
*/
274
- protected List <Class <?>> resolveAnnotatedInterceptorClasses (Executable executable ) {
257
+ private List <Class <?>> resolveAnnotatedInterceptorClasses (Executable executable ) {
275
258
Class <?> componentClass = executable .getDeclaringClass ();
276
259
277
260
List <Class <?>> interceptorClasses = new LinkedList <>();
@@ -306,8 +289,27 @@ protected List<Class<?>> resolveAnnotatedInterceptorClasses(Executable executabl
306
289
* @see Interceptors
307
290
* @see ExcludeClassInterceptors
308
291
*/
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
+
311
313
return interceptorClasses .stream ()
312
314
.map (InterceptorUtils ::unwrap )
313
315
.collect (toList ());
0 commit comments