Skip to content

Commit 260f213

Browse files
committed
Polish code with issues
1 parent 1f9805a commit 260f213

File tree

6 files changed

+111
-17
lines changed

6 files changed

+111
-17
lines changed

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/standard/beans/interceptor/InterceptorBean.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.geektimes.enterprise.inject.standard.beans.interceptor;
17+
package org.geektimes.enterprise.inject.standard.beans.interceptor;
1818

1919
import org.geektimes.enterprise.inject.standard.beans.GenericBean;
2020
import org.geektimes.interceptor.InterceptorInfo;
@@ -36,7 +36,7 @@
3636
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
3737
* @since 1.0.0
3838
*/
39-
public class InterceptorBean<T> extends GenericBean<T> implements Interceptor<T> {
39+
public class InterceptorBean<T> extends GenericBean<T> implements Interceptor<T> {
4040

4141
private final AnnotatedType<?> interceptorType;
4242

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/standard/beans/manager/BeanArchiveManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ public boolean isQualifier(Class<? extends Annotation> annotationType) {
352352
}
353353

354354
public boolean isInterceptorBinding(Class<? extends Annotation> annotationType) {
355-
return InterceptorUtils.isInterceptorBinding(annotationType) ||
355+
return InterceptorUtils.isAnnotatedInterceptorBinding(annotationType) ||
356356
// Extensions
357357
syntheticInterceptorBindings.containsKey(annotationType);
358358
}

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

+75-9
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@
4747
public class DefaultInterceptorManager implements InterceptorManager {
4848

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

5455
/**
5556
* The {@link InterceptorInfo} Repository
@@ -66,11 +67,14 @@ public class DefaultInterceptorManager implements InterceptorManager {
6667
*/
6768
private final Map<Executable, List<Object>> interceptorsCache;
6869

70+
private final Map<Executable, List<InterceptorInfo>> interceptorInfoCache;
71+
6972
public DefaultInterceptorManager() {
70-
this.interceptorBindingTypes = new LinkedHashSet<>();
73+
this.bindingInterceptorClasses = new HashMap<>();
7174
this.interceptorInfoRepository = new TreeMap<>(PriorityComparator.INSTANCE);
72-
this.bindingInterceptors = new LinkedHashMap<>();
73-
this.interceptorsCache = new LinkedHashMap<>();
75+
this.bindingInterceptors = new HashMap<>();
76+
this.interceptorsCache = new HashMap<>();
77+
this.interceptorInfoCache = new HashMap<>();
7478
registerDefaultInterceptorBindingType();
7579
}
7680

@@ -153,15 +157,56 @@ public List<Object> resolveInterceptors(Executable executable, Object... default
153157
});
154158
}
155159

160+
@Override
161+
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;
190+
}
191+
156192

157193
@Override
158194
public void registerInterceptorBindingType(Class<? extends Annotation> interceptorBindingType) {
159-
this.interceptorBindingTypes.add(interceptorBindingType);
195+
registerInterceptorBinding(interceptorBindingType);
196+
}
197+
198+
@Override
199+
public void registerInterceptorBinding(Class<? extends Annotation> interceptorBindingType,
200+
Annotation... interceptorBindingDef) {
201+
Set<Class<?>> interceptorClasses = bindingInterceptorClasses.computeIfAbsent(interceptorBindingType,
202+
t -> new HashSet<>());
203+
204+
// TODO
160205
}
161206

162207
@Override
163208
public boolean isInterceptorBindingType(Class<? extends Annotation> annotationType) {
164-
if (interceptorBindingTypes.contains(annotationType)) {
209+
if (bindingInterceptorClasses.containsKey(annotationType)) {
165210
return true;
166211
}
167212
if (isMetaAnnotation(annotationType, InterceptorBinding.class)) {
@@ -178,7 +223,7 @@ public Set<Class<?>> getInterceptorClasses() {
178223

179224
@Override
180225
public Set<Class<? extends Annotation>> getInterceptorBindingTypes() {
181-
return unmodifiableSet(interceptorBindingTypes);
226+
return bindingInterceptorClasses.keySet();
182227
}
183228

184229
@Override
@@ -205,6 +250,11 @@ private boolean isExcludedDefaultInterceptors(Executable executable) {
205250
return findAnnotation(executable, ExcludeDefaultInterceptors.class) != null;
206251
}
207252

253+
private List<Class<?>> resolveBindingInterceptorClasses(Executable executable) {
254+
InterceptorBindings interceptorBindings = resolveInterceptorBindings(executable);
255+
return unmodifiableSortedSet(bindingInterceptors.getOrDefault(interceptorBindings, emptySortedSet()));
256+
}
257+
208258
private SortedSet<Object> resolveBindingInterceptors(Executable executable) {
209259
InterceptorBindings interceptorBindings = resolveInterceptorBindings(executable);
210260
return unmodifiableSortedSet(bindingInterceptors.getOrDefault(interceptorBindings, emptySortedSet()));
@@ -221,7 +271,7 @@ private SortedSet<Object> resolveBindingInterceptors(Executable executable) {
221271
* @see Interceptors
222272
* @see ExcludeClassInterceptors
223273
*/
224-
private List<Object> resolveAnnotatedInterceptors(Executable executable) {
274+
protected List<Class<?>> resolveAnnotatedInterceptorClasses(Executable executable) {
225275
Class<?> componentClass = executable.getDeclaringClass();
226276

227277
List<Class<?>> interceptorClasses = new LinkedList<>();
@@ -242,6 +292,22 @@ private List<Object> resolveAnnotatedInterceptors(Executable executable) {
242292
}
243293
}
244294

295+
return interceptorClasses;
296+
}
297+
298+
/**
299+
* Interceptors declared by applying the Interceptors annotation at class-level to the target
300+
* class are invoked next.
301+
* <p>
302+
* Interceptors declared by applying the Interceptors annotation at method- or constructor-level are invoked next.
303+
*
304+
* @param executable the intercepted of {@linkplain Method method} or {@linkplain Constructor constructor}
305+
* @return non-null
306+
* @see Interceptors
307+
* @see ExcludeClassInterceptors
308+
*/
309+
protected List<Object> resolveAnnotatedInterceptors(Executable executable) {
310+
List<Class<?>> interceptorClasses = resolveAnnotatedInterceptorClasses(executable);
245311
return interceptorClasses.stream()
246312
.map(InterceptorUtils::unwrap)
247313
.collect(toList());

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import static org.geektimes.commons.lang.util.AnnotationUtils.getAttributesMap;
2525
import static org.geektimes.interceptor.InterceptorBindingAttributeFilter.FILTERS;
26-
import static org.geektimes.interceptor.util.InterceptorUtils.isInterceptorBinding;
26+
import static org.geektimes.interceptor.util.InterceptorUtils.isAnnotatedInterceptorBinding;
2727

2828
/**
2929
* The Metadata Info Class for {@link InterceptorBinding}
@@ -47,7 +47,7 @@ public class InterceptorBindingInfo {
4747
public InterceptorBindingInfo(Annotation declaredAnnotation) {
4848
this.declaredAnnotation = declaredAnnotation;
4949
this.declaredAnnotationType = declaredAnnotation.annotationType();
50-
this.synthetic = !isInterceptorBinding(declaredAnnotationType);
50+
this.synthetic = !isAnnotatedInterceptorBinding(declaredAnnotationType);
5151
this.attributes = getAttributesMap(declaredAnnotation, FILTERS);
5252
}
5353

@@ -68,7 +68,9 @@ public boolean equals(Object o) {
6868
if (this == o) return true;
6969
if (o == null || getClass() != o.getClass()) return false;
7070
InterceptorBindingInfo that = (InterceptorBindingInfo) o;
71-
return synthetic == that.synthetic && Objects.equals(declaredAnnotationType, that.declaredAnnotationType) && Objects.equals(attributes, that.attributes);
71+
return synthetic == that.synthetic
72+
&& Objects.equals(declaredAnnotationType, that.declaredAnnotationType)
73+
&& Objects.equals(attributes, that.attributes);
7274
}
7375

7476
@Override

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

+26
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,23 @@ default List<Object> resolveInterceptors(Constructor<?> constructor, Object... d
150150
*/
151151
List<Object> resolveInterceptors(Executable executable, Object... defaultInterceptors);
152152

153+
/**
154+
* Resolve the bindings of {@link javax.interceptor.Interceptor @Interceptor} {@link Class classes} upon
155+
* the specified {@linkplain Method method} or {@link Constructor}
156+
* <p>
157+
* See Specification:
158+
* <p>
159+
* 5.2 Interceptor Ordering Rules
160+
* <p>
161+
* 5.2.1 Use of the Priority Annotation in Ordering Interceptors
162+
*
163+
* @param executable the intercepted of {@linkplain Method method} or {@linkplain Constructor constructor}
164+
* @param defaultInterceptorClasses the default interceptors {@link Class classes}
165+
* @return a non-null read-only {@link Priority priority} {@link List list} of
166+
* {@link javax.interceptor.Interceptor @Interceptor} {@link Class classes}
167+
*/
168+
List<Class<?>> resolveInterceptorClasses(Executable executable, Class<?>... defaultInterceptorClasses);
169+
153170
/**
154171
* Register an {@linkplain javax.interceptor.Interceptor @Interceptor} binding {@link Class type}
155172
* whether it adds {@link InterceptorBinding} or not.
@@ -159,6 +176,15 @@ default List<Object> resolveInterceptors(Constructor<?> constructor, Object... d
159176
*/
160177
void registerInterceptorBindingType(Class<? extends Annotation> interceptorBindingType);
161178

179+
/**
180+
* Register an {@linkplain javax.interceptor.Interceptor @Interceptor} binding {@link Class type}
181+
* whether it adds {@link InterceptorBinding} or not.
182+
*
183+
* @param interceptorBindingType An interceptor binding type
184+
* @param interceptorBindingDef An optional list of annotations defining the {@linkplain javax.enterprise.inject.spi.Interceptor interceptor}
185+
*/
186+
void registerInterceptorBinding(Class<? extends Annotation> interceptorBindingType, Annotation... interceptorBindingDef);
187+
162188
/**
163189
* Get all registered {@link Class classes} of {@link javax.interceptor.Interceptor @Interceptor}
164190
*

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static boolean isInterceptorClass(Class<?> interceptorClass) {
5454
return false;
5555
}
5656

57-
public static List<Object> sortInterceptors(List<Object> interceptors) {
57+
public static List<Object> sortInterceptors(List<?> interceptors) {
5858
List<Object> sortedInterceptors = new LinkedList<>(interceptors);
5959
sortedInterceptors.sort(PriorityComparator.INSTANCE);
6060
return sortedInterceptors;
@@ -281,7 +281,7 @@ private static void validateInterceptorClassConstructors(Class<?> interceptorCla
281281
private static void validateInterceptorClassMethods(Class<?> interceptorClass) {
282282
}
283283

284-
public static boolean isInterceptorBinding(Class<? extends Annotation> annotationType) {
284+
public static boolean isAnnotatedInterceptorBinding(Class<? extends Annotation> annotationType) {
285285
return isMetaAnnotation(annotationType, InterceptorBinding.class);
286286
}
287287

0 commit comments

Comments
 (0)