Skip to content

Commit 39b8856

Browse files
committed
Polish code
1 parent de7c784 commit 39b8856

File tree

9 files changed

+117
-37
lines changed

9 files changed

+117
-37
lines changed

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.geektimes.enterprise.inject.util.Qualifiers;
3131
import org.geektimes.enterprise.inject.util.Scopes;
3232
import org.geektimes.enterprise.inject.util.Stereotypes;
33+
import org.geektimes.interceptor.InterceptorManager;
3334
import org.geektimes.interceptor.util.InterceptorUtils;
3435

3536
import javax.enterprise.context.*;
@@ -60,7 +61,8 @@
6061
import static org.geektimes.enterprise.inject.standard.beans.BeanTypeSource.*;
6162
import static org.geektimes.enterprise.inject.standard.beans.xml.BeansReader.BEANS_XML_RESOURCE_NAME;
6263
import static org.geektimes.enterprise.inject.util.Decorators.isDecorator;
63-
import static org.geektimes.interceptor.util.InterceptorUtils.isInterceptorClass;
64+
import static org.geektimes.interceptor.InterceptorManager.getInstance;
65+
6466

6567
/**
6668
* Bean archives Manager
@@ -80,6 +82,8 @@ public class BeanArchiveManager {
8082

8183
private final SimpleClassScanner classScanner;
8284

85+
private final InterceptorManager interceptorManager;
86+
8387
private final Map<BeanTypeSource, List<Class<?>>> beanClasses;
8488

8589
private final Map<BeanTypeSource, List<Class<?>>> interceptorClasses;
@@ -140,6 +144,7 @@ public BeanArchiveManager(ClassLoader classLoader) {
140144
this.classLoader = classLoader;
141145
this.beansReader = ServiceLoaders.loadSpi(BeansReader.class, classLoader);
142146
this.classScanner = SimpleClassScanner.INSTANCE;
147+
this.interceptorManager = getInstance(classLoader);
143148

144149
this.beanClasses = new TreeMap<>();
145150
this.interceptorClasses = new TreeMap<>();
@@ -272,7 +277,7 @@ private void addEnabledInterceptorClasses(org.geektimes.enterprise.inject.standa
272277

273278
private void discoverInterceptorClasses(Set<Class<?>> discoveredTypes) {
274279
filterAndHandleBeanTypes(discoveredTypes,
275-
InterceptorUtils::isInterceptorClass,
280+
interceptorManager::isInterceptorClass,
276281
this::addDiscoveredInterceptorClass);
277282
}
278283

@@ -545,7 +550,7 @@ private void addSyntheticBeanTypes(Set<Class<?>> discoveredTypes) {
545550

546551
private void addSyntheticInterceptorClasses(Set<Class<?>> discoveredTypes) {
547552
filterAndHandleBeanTypes(discoveredTypes,
548-
InterceptorUtils::isInterceptorClass,
553+
interceptorManager::isInterceptorClass,
549554
this::addSyntheticInterceptorClass);
550555
}
551556

@@ -814,7 +819,7 @@ private BeanArchiveType resolveBeanArchiveType(Beans beans) {
814819
*/
815820
public boolean isDefiningAnnotationType(Class<?> type, boolean includedInterceptor, boolean includedDecorator) {
816821

817-
if (includedInterceptor && isInterceptorClass(type)) {
822+
if (includedInterceptor && interceptorManager.isInterceptorClass(type)) {
818823
return true;
819824
}
820825
if (includedDecorator && isDecorator(type)) {

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

+40-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.geektimes.enterprise.inject.standard.interceptor;
1818

1919
import org.geektimes.enterprise.inject.standard.AbstractAnnotatedTypeBean;
20-
import org.geektimes.interceptor.InterceptorBindings;
2120
import org.geektimes.interceptor.InterceptorInfo;
2221
import org.geektimes.interceptor.InterceptorManager;
2322

@@ -27,6 +26,7 @@
2726
import java.lang.annotation.Annotation;
2827
import java.util.Set;
2928

29+
import static java.lang.String.format;
3030
import static org.geektimes.interceptor.InterceptorManager.getInstance;
3131

3232
/**
@@ -46,6 +46,8 @@ public class InterceptorBean<T> extends AbstractAnnotatedTypeBean<T> implements
4646

4747
private final InterceptorInfo interceptorInfo;
4848

49+
private final Set<Annotation> interceptorBindings;
50+
4951
private final BeanManager beanManager;
5052

5153
public InterceptorBean(AnnotatedType<T> interceptorType, BeanManager beanManager) {
@@ -55,23 +57,56 @@ public InterceptorBean(AnnotatedType<T> interceptorType, BeanManager beanManager
5557
this.interceptorManager = getInstance(interceptorClass.getClassLoader());
5658
this.interceptorManager.registerInterceptorClass(interceptorClass);
5759
this.interceptorInfo = interceptorManager.getInterceptorInfo(interceptorClass);
60+
this.interceptorBindings = resolveInterceptorBindings();
5861
this.beanManager = beanManager;
5962
}
6063

64+
private Set<Annotation> resolveInterceptorBindings() {
65+
return interceptorInfo.getInterceptorBindings().getDeclaredAnnotations();
66+
}
67+
6168
@Override
6269
public Set<Annotation> getInterceptorBindings() {
63-
// TODO
64-
return null;
70+
return interceptorBindings;
6571
}
6672

6773
@Override
6874
public boolean intercepts(InterceptionType type) {
69-
// TODO
75+
boolean supported = false;
76+
switch (type) {
77+
case AROUND_INVOKE:
78+
supported = interceptorInfo.hasAroundInvokeMethod();
79+
break;
80+
case AROUND_TIMEOUT:
81+
supported = interceptorInfo.hasAroundTimeoutMethod();
82+
break;
83+
case AROUND_CONSTRUCT:
84+
supported = interceptorInfo.hasAroundConstructMethod();
85+
break;
86+
case POST_CONSTRUCT:
87+
supported = interceptorInfo.hasPostConstructMethod();
88+
break;
89+
case PRE_DESTROY:
90+
supported = interceptorInfo.hasPreDestroyMethod();
91+
break;
92+
case PRE_PASSIVATE:
93+
// TODO
94+
break;
95+
case POST_ACTIVATE:
96+
// TODO
97+
break;
98+
}
7099
return false;
71100
}
72101

73102
@Override
74103
public Object intercept(InterceptionType type, T instance, InvocationContext ctx) throws Exception {
104+
if (!intercepts(type)) {
105+
throw new UnsupportedOperationException(format(
106+
"The interceptor[type:%s] does not support the interception type[%s]!",
107+
interceptorClass.getName(),
108+
type.name()));
109+
}
75110
// TODO
76111
return null;
77112
}
@@ -101,12 +136,11 @@ protected String getBeanName(Class interceptorClass) {
101136

102137
@Override
103138
protected void validateAnnotatedElement(Class interceptorClass) {
104-
// TODO
139+
this.interceptorManager.validateInterceptorClass(interceptorClass);
105140
}
106141

107142
@Override
108143
public Annotated getAnnotated() {
109-
// TODO
110144
return interceptorType;
111145
}
112146
}

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/util/Beans.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.geektimes.commons.lang.util.ArrayUtils;
2020
import org.geektimes.commons.reflect.util.MemberUtils;
21+
import org.geektimes.interceptor.InterceptorManager;
2122

2223
import javax.decorator.Decorator;
2324
import javax.enterprise.context.Dependent;
@@ -47,6 +48,7 @@
4748
import static org.geektimes.commons.reflect.util.TypeUtils.*;
4849
import static org.geektimes.enterprise.inject.util.Decorators.isDecorator;
4950
import static org.geektimes.enterprise.inject.util.Qualifiers.findQualifier;
51+
import static org.geektimes.interceptor.InterceptorManager.getInstance;
5052
import static org.geektimes.interceptor.util.InterceptorUtils.isInterceptorClass;
5153

5254
/**
@@ -228,7 +230,8 @@ private static boolean hasManagedBeanConstructor(Class<?> beanClass) {
228230
* @throws DefinitionException if the bean class does not meet above conditions
229231
*/
230232
public static void validateManagedBeanType(Class<?> managedBeanClass) throws DefinitionException {
231-
if (isInterceptorClass(managedBeanClass) && isDecorator(managedBeanClass)) {
233+
InterceptorManager interceptorManager = getInstance(managedBeanClass.getClassLoader());
234+
if (interceptorManager.isInterceptorClass(managedBeanClass) && isDecorator(managedBeanClass)) {
232235
throwDefinitionException("The managed bean [class : %s] must not annotate with both %s and %s",
233236
managedBeanClass.getName(), Interceptor.class.getName(), Decorator.class.getName());
234237
}

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

+16
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public DefaultInterceptorManager() {
7676

7777
@Override
7878
public void registerInterceptorClass(Class<?> interceptorClass) {
79+
validateInterceptorClass(interceptorClass);
7980
interceptorInfoRepository.computeIfAbsent(interceptorClass, InterceptorInfo::new);
8081
}
8182

@@ -174,6 +175,21 @@ public Set<Class<? extends Annotation>> getInterceptorBindingTypes() {
174175
return unmodifiableSet(interceptorBindingTypes);
175176
}
176177

178+
@Override
179+
public boolean isInterceptorClass(Class<?> interceptorClass) {
180+
if(interceptorInfoRepository.containsKey(interceptorClass)){
181+
return true;
182+
}
183+
return InterceptorUtils.isInterceptorClass(interceptorClass);
184+
}
185+
186+
@Override
187+
public void validateInterceptorClass(Class<?> interceptorClass) throws NullPointerException, IllegalStateException {
188+
if (!interceptorInfoRepository.containsKey(interceptorClass)) {
189+
InterceptorUtils.validateInterceptorClass(interceptorClass);
190+
}
191+
}
192+
177193
private void registerDefaultInterceptorBindingType() {
178194
registerInterceptorBindingType(PostConstruct.class);
179195
registerInterceptorBindingType(PreDestroy.class);

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

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
*/
3434
public class InterceptorBindingInfo {
3535

36+
private final Annotation declaredAnnotation;
37+
3638
private final Class<? extends Annotation> declaredAnnotationType;
3739

3840
/**
@@ -43,6 +45,7 @@ public class InterceptorBindingInfo {
4345
private final Map<String, Object> attributes;
4446

4547
public InterceptorBindingInfo(Annotation declaredAnnotation) {
48+
this.declaredAnnotation = declaredAnnotation;
4649
this.declaredAnnotationType = declaredAnnotation.annotationType();
4750
this.synthetic = !isInterceptorBinding(declaredAnnotationType);
4851
this.attributes = getAttributesMap(declaredAnnotation, FILTERS);
@@ -73,6 +76,10 @@ public int hashCode() {
7376
return Objects.hash(declaredAnnotationType, synthetic, attributes);
7477
}
7578

79+
public Annotation getDeclaredAnnotation() {
80+
return declaredAnnotation;
81+
}
82+
7683
/**
7784
* New instance of {@link InterceptorBindingInfo}
7885
*

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
*/
4444
public class InterceptorBindings implements Iterable<InterceptorBindingInfo> {
4545

46-
private final Set<InterceptorBindingInfo> interceptorBindings;
46+
private final Set<Annotation> declaredAnnotations;
47+
48+
private final Set<InterceptorBindingInfo> bindingInfoSet;
4749

4850
private final Set<Class<? extends Annotation>> interceptorBindingTypes;
4951

@@ -52,12 +54,13 @@ public InterceptorBindings(Collection<Annotation> interceptorBindings) {
5254
}
5355

5456
public InterceptorBindings(Set<Annotation> interceptorBindings) {
55-
this.interceptorBindings = asInterceptorBindings(interceptorBindings);
57+
this.declaredAnnotations = interceptorBindings;
58+
this.bindingInfoSet = asInterceptorBindings(interceptorBindings);
5659
this.interceptorBindingTypes = asInterceptorBindingTypes(interceptorBindings);
5760
}
5861

59-
public Set<InterceptorBindingInfo> getInterceptorBindings() {
60-
return interceptorBindings;
62+
public Set<Annotation> getDeclaredAnnotations() {
63+
return declaredAnnotations;
6164
}
6265

6366
public Set<Class<? extends Annotation>> getInterceptorBindingTypes() {
@@ -66,20 +69,20 @@ public Set<Class<? extends Annotation>> getInterceptorBindingTypes() {
6669

6770
@Override
6871
public Iterator<InterceptorBindingInfo> iterator() {
69-
return interceptorBindings.iterator();
72+
return bindingInfoSet.iterator();
7073
}
7174

7275
@Override
7376
public boolean equals(Object o) {
7477
if (this == o) return true;
7578
if (o == null || getClass() != o.getClass()) return false;
7679
InterceptorBindings that = (InterceptorBindings) o;
77-
return Objects.equals(interceptorBindings, that.interceptorBindings);
80+
return Objects.equals(bindingInfoSet, that.bindingInfoSet);
7881
}
7982

8083
@Override
8184
public int hashCode() {
82-
return Objects.hash(interceptorBindings);
85+
return Objects.hash(bindingInfoSet);
8386
}
8487

8588
private Set<InterceptorBindingInfo> asInterceptorBindings(Collection<Annotation> interceptorBindings) {

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

+26-7
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@
3030
import java.util.function.Predicate;
3131

3232
import static java.lang.String.format;
33-
import static java.util.Collections.unmodifiableCollection;
3433
import static org.geektimes.commons.lang.util.AnnotationUtils.getAllDeclaredAnnotations;
3534
import static org.geektimes.commons.reflect.util.ClassUtils.getAllClasses;
36-
import static org.geektimes.interceptor.util.InterceptorUtils.validatorInterceptorClass;
35+
import static org.geektimes.interceptor.util.InterceptorUtils.validateInterceptorClass;
3736

3837
/**
3938
* Interceptor Info Metadata class
@@ -66,7 +65,6 @@ public class InterceptorInfo {
6665
private final InterceptorBindings interceptorBindings;
6766

6867
public InterceptorInfo(Class<?> interceptorClass) {
69-
validatorInterceptorClass(interceptorClass);
7068
this.interceptorManager = InterceptorManager.getInstance(interceptorClass.getClassLoader());
7169
this.interceptorClass = interceptorClass;
7270
this.aroundInvokeMethods = new LinkedList<>();
@@ -127,10 +125,6 @@ private InterceptorBindings resolveInterceptorBindings() {
127125
return new InterceptorBindings(getAllDeclaredAnnotations(interceptorClass, interceptorManager::isInterceptorBinding));
128126
}
129127

130-
public Class<?> getInterceptorClass() {
131-
return interceptorClass;
132-
}
133-
134128
Collection<Method> getAroundInvokeMethods() {
135129
return aroundInvokeMethods;
136130
}
@@ -151,6 +145,31 @@ Collection<Method> getPreDestroyMethods() {
151145
return preDestroyMethods;
152146
}
153147

148+
public Class<?> getInterceptorClass() {
149+
return interceptorClass;
150+
}
151+
152+
public boolean hasAroundInvokeMethod() {
153+
return !getAroundInvokeMethods().isEmpty();
154+
}
155+
156+
public boolean hasAroundTimeoutMethod(){
157+
return !getAroundTimeoutMethods().isEmpty();
158+
}
159+
160+
public boolean hasAroundConstructMethod(){
161+
return !getAroundConstructMethods().isEmpty();
162+
}
163+
164+
public boolean hasPostConstructMethod(){
165+
return !getPostConstructMethods().isEmpty();
166+
}
167+
168+
public boolean hasPreDestroyMethod(){
169+
return !getPreDestroyMethods().isEmpty();
170+
}
171+
172+
154173
public InterceptorBindings getInterceptorBindings() {
155174
return interceptorBindings;
156175
}

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

+3-10
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,9 @@ default List<Object> resolveInterceptors(Constructor<?> constructor, Object... d
177177
/**
178178
* The given interceptor class is {@link javax.interceptor.Interceptor @Interceptor} or not.
179179
*
180-
* @param interceptorClass the class of interceptor
181-
* @throws NullPointerException If <code>interceptorClass</code> is <code>null</code>
182-
* @throws IllegalStateException If an interceptor class does not annotate @Interceptor or
183-
* is abstract or have not a public no-arg constructor
180+
* @param interceptorClass the class of interceptor is abstract or have not a public no-arg constructor
184181
*/
185-
default boolean isInterceptorClass(Class<?> interceptorClass) throws NullPointerException, IllegalStateException {
186-
return InterceptorUtils.isInterceptorClass(interceptorClass);
187-
}
182+
boolean isInterceptorClass(Class<?> interceptorClass);
188183

189184
/**
190185
* An interceptor class must not be abstract and must have a public no-arg constructor.
@@ -194,9 +189,7 @@ default boolean isInterceptorClass(Class<?> interceptorClass) throws NullPointer
194189
* @throws IllegalStateException If an interceptor class does not annotate @Interceptor or
195190
* is abstract or have not a public no-arg constructor
196191
*/
197-
default void validatorInterceptorClass(Class<?> interceptorClass) throws NullPointerException, IllegalStateException {
198-
InterceptorUtils.validatorInterceptorClass(interceptorClass);
199-
}
192+
void validateInterceptorClass(Class<?> interceptorClass) throws NullPointerException, IllegalStateException;
200193

201194
default boolean isInterceptorBinding(Annotation annotation) {
202195
return isInterceptorBindingType(annotation.annotationType());

0 commit comments

Comments
 (0)