Skip to content

Commit d54f0fe

Browse files
committed
Polish code
1 parent df75a26 commit d54f0fe

27 files changed

+556
-245
lines changed

projects/stage-1/middleware-frameworks/my-cache/src/test/java/org/geektimes/cache/annotation/interceptor/CachePutInterceptorTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import org.geektimes.cache.DataRepository;
2020
import org.geektimes.cache.InMemoryDataRepository;
21-
import org.geektimes.interceptor.DefaultInterceptorEnhancer;
22-
import org.geektimes.interceptor.InterceptorEnhancer;
21+
import org.geektimes.interceptor.DefaultComponentEnhancer;
22+
import org.geektimes.interceptor.ComponentEnhancer;
2323
import org.junit.Test;
2424

2525
import javax.cache.CacheManager;
@@ -38,7 +38,7 @@ public class CachePutInterceptorTest {
3838

3939
private DataRepository dataRepository = new InMemoryDataRepository();
4040

41-
private InterceptorEnhancer enhancer = new DefaultInterceptorEnhancer();
41+
private ComponentEnhancer enhancer = new DefaultComponentEnhancer();
4242

4343
private CachingProvider cachingProvider = Caching.getCachingProvider();
4444

projects/stage-1/middleware-frameworks/my-cache/src/test/java/org/geektimes/cache/annotation/interceptor/CacheRemoveAllInterceptorTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import org.geektimes.cache.DataRepository;
2020
import org.geektimes.cache.InMemoryDataRepository;
21-
import org.geektimes.interceptor.DefaultInterceptorEnhancer;
22-
import org.geektimes.interceptor.InterceptorEnhancer;
21+
import org.geektimes.interceptor.DefaultComponentEnhancer;
22+
import org.geektimes.interceptor.ComponentEnhancer;
2323
import org.junit.Test;
2424

2525
import static org.junit.Assert.*;
@@ -34,7 +34,7 @@ public class CacheRemoveAllInterceptorTest {
3434

3535
private DataRepository dataRepository = new InMemoryDataRepository();
3636

37-
private InterceptorEnhancer enhancer = new DefaultInterceptorEnhancer();
37+
private ComponentEnhancer enhancer = new DefaultComponentEnhancer();
3838

3939
@Test
4040
public void test() {

projects/stage-1/middleware-frameworks/my-cache/src/test/java/org/geektimes/cache/annotation/interceptor/CacheRemoveInterceptorTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import org.geektimes.cache.DataRepository;
2020
import org.geektimes.cache.InMemoryDataRepository;
21-
import org.geektimes.interceptor.DefaultInterceptorEnhancer;
22-
import org.geektimes.interceptor.InterceptorEnhancer;
21+
import org.geektimes.interceptor.DefaultComponentEnhancer;
22+
import org.geektimes.interceptor.ComponentEnhancer;
2323
import org.junit.Test;
2424

2525
import static org.junit.Assert.assertNull;
@@ -35,7 +35,7 @@ public class CacheRemoveInterceptorTest {
3535

3636
private DataRepository dataRepository = new InMemoryDataRepository();
3737

38-
private InterceptorEnhancer enhancer = new DefaultInterceptorEnhancer();
38+
private ComponentEnhancer enhancer = new DefaultComponentEnhancer();
3939

4040
@Test
4141
public void test() {

projects/stage-1/middleware-frameworks/my-cache/src/test/java/org/geektimes/cache/annotation/interceptor/CacheResultInterceptorTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
import org.geektimes.cache.DataRepository;
2020
import org.geektimes.cache.InMemoryDataRepository;
21-
import org.geektimes.interceptor.DefaultInterceptorEnhancer;
21+
import org.geektimes.interceptor.DefaultComponentEnhancer;
2222
import org.geektimes.interceptor.Interceptor;
23-
import org.geektimes.interceptor.InterceptorEnhancer;
23+
import org.geektimes.interceptor.ComponentEnhancer;
2424
import org.junit.Test;
2525

2626
import static org.geektimes.commons.util.ServiceLoaders.loadAsArray;
@@ -37,7 +37,7 @@ public class CacheResultInterceptorTest {
3737

3838
private DataRepository dataRepository = new InMemoryDataRepository();
3939

40-
private InterceptorEnhancer enhancer = new DefaultInterceptorEnhancer();
40+
private ComponentEnhancer enhancer = new DefaultComponentEnhancer();
4141

4242
@Test
4343
public void test() {

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434

3535
import static java.lang.String.format;
3636
import static java.util.ServiceLoader.load;
37-
import static org.geektimes.commons.function.Streams.stream;
3837
import static org.geektimes.commons.reflect.util.TypeUtils.resolveTypeArguments;
39-
import static org.geektimes.interceptor.InterceptorRegistry.getInstance;
38+
import static org.geektimes.interceptor.InterceptorManager.getInstance;
4039
import static org.geektimes.interceptor.util.InterceptorUtils.INTERCEPTOR_ANNOTATION_TYPE;
4140

4241
/**
@@ -51,7 +50,7 @@ public abstract class AnnotatedInterceptor<A extends Annotation> implements Inte
5150

5251
private final Logger logger = Logger.getLogger(getClass().getName());
5352

54-
private final InterceptorRegistry interceptorRegistry;
53+
private final InterceptorManager interceptorManager;
5554

5655
private final Class<A> interceptorBindingType;
5756

@@ -63,10 +62,10 @@ public abstract class AnnotatedInterceptor<A extends Annotation> implements Inte
6362
*/
6463
public AnnotatedInterceptor() throws IllegalArgumentException {
6564
Class<?> interceptorClass = getClass();
66-
this.interceptorRegistry = getInstance(interceptorClass.getClassLoader());
67-
this.interceptorRegistry.registerInterceptorClass(interceptorClass);
65+
this.interceptorManager = getInstance(interceptorClass.getClassLoader());
66+
this.interceptorManager.registerInterceptorClass(interceptorClass);
6867
this.interceptorBindingType = resolveInterceptorBindingType(interceptorClass);
69-
this.interceptorRegistry.registerInterceptor(this);
68+
this.interceptorManager.registerInterceptor(this);
7069
}
7170

7271
@Override
@@ -232,11 +231,11 @@ protected Class<A> resolveInterceptorBindingType(Class<?> interceptorClass) {
232231
}
233232

234233
private boolean isInterceptorBindingType(Class<? extends Annotation> annotationType) {
235-
return interceptorRegistry.isInterceptorBindingType(annotationType);
234+
return interceptorManager.isInterceptorBindingType(annotationType);
236235
}
237236

238237
private void registerSyntheticInterceptorBindingType(Class<A> annotationType) {
239-
interceptorRegistry.registerInterceptorBindingType(annotationType);
238+
interceptorManager.registerInterceptorBindingType(annotationType);
240239
}
241240

242241
protected boolean shouldRegisterSyntheticInterceptorBindingType() {
@@ -278,8 +277,8 @@ protected Throwable getFailure(Throwable e) {
278277
return failure;
279278
}
280279

281-
public InterceptorRegistry getInterceptorRegistry() {
282-
return interceptorRegistry;
280+
public InterceptorManager getInterceptorRegistry() {
281+
return interceptorManager;
283282
}
284283

285284
private boolean excludeInterceptorAnnotation(Annotation annotation) {

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

+12-15
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@
1919
import javax.annotation.PostConstruct;
2020
import javax.annotation.PreDestroy;
2121
import javax.interceptor.InvocationContext;
22-
import java.lang.reflect.AnnotatedElement;
2322
import java.lang.reflect.Constructor;
2423
import java.lang.reflect.Method;
2524
import java.util.List;
2625
import java.util.Map;
2726

28-
import static org.geektimes.interceptor.InterceptorRegistry.getInstance;
27+
import static org.geektimes.interceptor.InterceptorManager.getInstance;
2928

3029
/**
3130
* Chainable {@link InvocationContext}
@@ -41,17 +40,14 @@ public class ChainableInvocationContext implements InvocationContext {
4140

4241
private final int size;
4342

44-
private final InterceptorRegistry interceptorRegistry;
43+
private final InterceptorManager interceptorManager;
4544

4645
private int pos; // position
4746

48-
public ChainableInvocationContext(InvocationContext delegateContext, Object... interceptors) {
47+
public ChainableInvocationContext(InvocationContext delegateContext, Object... defaultInterceptors) {
4948
this.delegateContext = delegateContext;
50-
// sort
51-
this.interceptorRegistry = getInstance(resolveClassLoader(interceptors));
52-
this.interceptorRegistry.registerInterceptors(interceptors);
53-
this.interceptorRegistry.registerDiscoveredInterceptors();
54-
this.interceptors = resolveInterceptors();
49+
this.interceptorManager = getInstance(resolveClassLoader(defaultInterceptors));
50+
this.interceptors = resolveInterceptors(defaultInterceptors);
5551
this.size = this.interceptors.size();
5652
this.pos = 0;
5753
}
@@ -108,16 +104,17 @@ private ClassLoader resolveClassLoader(Object[] interceptors) {
108104
return target.getClass().getClassLoader();
109105
}
110106

111-
private List<Object> resolveInterceptors() {
112-
AnnotatedElement annotatedElement = getMethod();
113-
if (annotatedElement == null) {
114-
annotatedElement = getConstructor();
107+
private List<Object> resolveInterceptors(Object[] defaultInterceptors) {
108+
Method method = getMethod();
109+
if (method != null) {
110+
return interceptorManager.resolveInterceptors(method, defaultInterceptors);
115111
}
116-
return interceptorRegistry.getInterceptors(annotatedElement);
112+
return interceptorManager.resolveInterceptors(getConstructor(), defaultInterceptors);
117113
}
118114

115+
119116
private Method resolveInterceptionMethod(Object interceptor) {
120-
InterceptorInfo interceptorInfo = interceptorRegistry.getInterceptorInfo(interceptor.getClass());
117+
InterceptorInfo interceptorInfo = interceptorManager.getInterceptorInfo(interceptor.getClass());
121118

122119
final Method interceptionMethod; // nerver null
123120

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.geektimes.interceptor;
18+
19+
/**
20+
* Component Enhancer
21+
* <p>
22+
* If a component class declares or inherits a class-level interceptor binding,
23+
* it must not be declared final, or have any non-static, non-private, final
24+
* methods. If a component has a class-level interceptor binding and is declared
25+
* final or has a non-static, non-private, final method, the container automatically
26+
* detects the problem and treats it as a definition error, and causes deployment to
27+
* fail.
28+
* <p>
29+
* If a non-static, non-private method of a component class declares a method-level
30+
* interceptor binding, neither the method nor the component class may be declared final.
31+
* If a non-static, non-private, final method of a component has a method-level interceptor
32+
* binding, the container automatically detects the problem and treats it as a definition
33+
* error, and causes deployment to fail.
34+
*
35+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
36+
* @since 1.0.0
37+
*/
38+
public interface ComponentEnhancer {
39+
40+
default <T> T enhance(T component) {
41+
return enhance(component, (Class<? super T>) component.getClass());
42+
}
43+
44+
default <T> T enhance(T component, Class<? super T> componentClass) {
45+
return enhance(component, componentClass, new Object[0]);
46+
}
47+
48+
default <T> T enhance(T component, Object... defaultInterceptors) {
49+
return enhance(component, (Class<? super T>) component.getClass(), defaultInterceptors);
50+
}
51+
52+
<T> T enhance(T component, Class<? super T> componentClass, Object... defaultInterceptors);
53+
54+
}
+17-17
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,39 @@
1616
*/
1717
package org.geektimes.interceptor;
1818

19-
import org.geektimes.interceptor.cglib.CglibInterceptorEnhancer;
20-
import org.geektimes.interceptor.jdk.DynamicProxyInterceptorEnhancer;
19+
import org.geektimes.interceptor.cglib.CglibComponentEnhancer;
20+
import org.geektimes.interceptor.jdk.DynamicProxyComponentEnhancer;
2121

2222
import static org.geektimes.commons.lang.util.ClassLoaderUtils.getClassLoader;
2323

2424
/**
25-
* Default {@link InterceptorEnhancer}
25+
* Default {@link ComponentEnhancer}
2626
*
2727
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
2828
* @since 1.0.0
2929
*/
30-
public class DefaultInterceptorEnhancer implements InterceptorEnhancer {
30+
public class DefaultComponentEnhancer implements ComponentEnhancer {
3131

32-
private final InterceptorEnhancer jdkProxyInterceptorEnhancer;
32+
private final ComponentEnhancer jdkProxyInterceptorEnhancer;
3333

34-
private final InterceptorEnhancer cglibInterceptorEnhancer;
34+
private final ComponentEnhancer cglibInterceptorEnhancer;
3535

36-
private final InterceptorRegistry interceptorRegistry;
36+
private final InterceptorManager interceptorManager;
3737

38-
public DefaultInterceptorEnhancer() {
39-
this.jdkProxyInterceptorEnhancer = new DynamicProxyInterceptorEnhancer();
40-
this.cglibInterceptorEnhancer = new CglibInterceptorEnhancer();
41-
this.interceptorRegistry = InterceptorRegistry.getInstance(getClassLoader(this.getClass()));
42-
this.interceptorRegistry.registerDiscoveredInterceptors();
38+
public DefaultComponentEnhancer() {
39+
this.jdkProxyInterceptorEnhancer = new DynamicProxyComponentEnhancer();
40+
this.cglibInterceptorEnhancer = new CglibComponentEnhancer();
41+
this.interceptorManager = InterceptorManager.getInstance(getClassLoader(this.getClass()));
42+
this.interceptorManager.registerDiscoveredInterceptors();
4343
}
4444

4545
@Override
46-
public <T> T enhance(T source, Class<? super T> type, Object... interceptors) {
47-
assertType(type);
48-
if (type.isInterface()) {
49-
return jdkProxyInterceptorEnhancer.enhance(source, type, interceptors);
46+
public <T> T enhance(T source, Class<? super T> componentClass, Object... defaultInterceptors) {
47+
assertType(componentClass);
48+
if (componentClass.isInterface()) {
49+
return jdkProxyInterceptorEnhancer.enhance(source, componentClass, defaultInterceptors);
5050
} else {
51-
return cglibInterceptorEnhancer.enhance(source, type, interceptors);
51+
return cglibInterceptorEnhancer.enhance(source, componentClass, defaultInterceptors);
5252
}
5353
}
5454

0 commit comments

Comments
 (0)