Skip to content

Commit acdb112

Browse files
committed
Polish code
1 parent b869e81 commit acdb112

File tree

3 files changed

+106
-7
lines changed

3 files changed

+106
-7
lines changed

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

+18-6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ public abstract class InterceptorUtils {
4646

4747
public static final Class<? extends Annotation> INTERCEPTOR_ANNOTATION_TYPE = javax.interceptor.Interceptor.class;
4848

49+
public static final Class<? extends Annotation> INTERCEPTOR_BINDING_ANNOTATION_TYPE = InterceptorBinding.class;
50+
51+
public static final Class<? extends Annotation> AROUND_INVOKE_ANNOTATION_TYPE = AroundInvoke.class;
52+
53+
public static final Class<? extends Annotation> AROUND_TIMEOUT_ANNOTATION_TYPE = AroundTimeout.class;
54+
55+
public static final Class<? extends Annotation> AROUND_CONSTRUCT_ANNOTATION_TYPE = AroundConstruct.class;
56+
57+
public static final Class<? extends Annotation> POST_CONSTRUCT_ANNOTATION_TYPE = PostConstruct.class;
58+
59+
public static final Class<? extends Annotation> PRE_DESTROY_ANNOTATION_TYPE = PreDestroy.class;
60+
4961

5062
public static boolean isInterceptorClass(Class<?> interceptorClass) {
5163
if (isAnnotationPresent(interceptorClass, INTERCEPTOR_ANNOTATION_TYPE)) {
@@ -105,7 +117,7 @@ public static <A extends Annotation> A searchAnnotation(Class<?> componentClass,
105117
* or if the return type of method is not <code>Object</code> or its derived type.
106118
*/
107119
public static boolean isAroundInvokeMethod(Method method) {
108-
return isInterceptionMethod(method, AroundInvoke.class, Object.class);
120+
return isInterceptionMethod(method, AROUND_INVOKE_ANNOTATION_TYPE, Object.class);
109121
}
110122

111123
/**
@@ -126,7 +138,7 @@ public static boolean isAroundInvokeMethod(Method method) {
126138
* or if the return type of method is not <code>Object</code> or its derived type.
127139
*/
128140
public static boolean isAroundTimeoutMethod(Method method) {
129-
return isInterceptionMethod(method, AroundTimeout.class, Object.class);
141+
return isInterceptionMethod(method, AROUND_TIMEOUT_ANNOTATION_TYPE, Object.class);
130142
}
131143

132144
/**
@@ -140,7 +152,7 @@ public static boolean isAroundTimeoutMethod(Method method) {
140152
* or if the return type of method is not <code>void</code>
141153
*/
142154
public static boolean isAroundConstructMethod(Method method) {
143-
return isInterceptionMethod(method, AroundConstruct.class, void.class);
155+
return isInterceptionMethod(method, AROUND_CONSTRUCT_ANNOTATION_TYPE, void.class);
144156
}
145157

146158
/**
@@ -154,7 +166,7 @@ public static boolean isAroundConstructMethod(Method method) {
154166
* or if the return type of method is not <code>void</code>
155167
*/
156168
public static boolean isPostConstructMethod(Method method) {
157-
return isInterceptionMethod(method, PostConstruct.class, void.class);
169+
return isInterceptionMethod(method, POST_CONSTRUCT_ANNOTATION_TYPE, void.class);
158170
}
159171

160172
/**
@@ -168,7 +180,7 @@ public static boolean isPostConstructMethod(Method method) {
168180
* or if the return type of method is not <code>void</code>
169181
*/
170182
public static boolean isPreDestroyMethod(Method method) {
171-
return isInterceptionMethod(method, PreDestroy.class, void.class);
183+
return isInterceptionMethod(method, PRE_DESTROY_ANNOTATION_TYPE, void.class);
172184
}
173185

174186

@@ -282,7 +294,7 @@ private static void validateInterceptorClassMethods(Class<?> interceptorClass) {
282294
}
283295

284296
public static boolean isAnnotatedInterceptorBinding(Class<? extends Annotation> annotationType) {
285-
return isMetaAnnotation(annotationType, InterceptorBinding.class);
297+
return isMetaAnnotation(annotationType, INTERCEPTOR_BINDING_ANNOTATION_TYPE);
286298
}
287299

288300
public static boolean isAnnotatedInterceptorBinding(Executable executable,

projects/stage-1/middleware-frameworks/my-interceptor/src/test/java/org/geektimes/interceptor/Monitored.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* @since 1.0.0
3333
*/
3434
@Inherited
35-
@InterceptorBinding
35+
@DataAccess
3636
@Target({TYPE, METHOD})
3737
@Retention(RUNTIME)
3838
public @interface Monitored {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.util;
18+
19+
import org.geektimes.interceptor.ExternalInterceptor;
20+
import org.geektimes.interceptor.Logging;
21+
import org.geektimes.interceptor.Monitored;
22+
import org.junit.Test;
23+
24+
import javax.annotation.PostConstruct;
25+
import javax.annotation.PreDestroy;
26+
import javax.interceptor.*;
27+
28+
import static org.geektimes.interceptor.util.InterceptorUtils.*;
29+
import static org.junit.Assert.*;
30+
31+
/**
32+
* {@link InterceptorUtils} Test
33+
*
34+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
35+
* @since 1.0.0
36+
*/
37+
public class InterceptorUtilsTest {
38+
39+
@Test
40+
public void testConstants() {
41+
assertEquals(javax.interceptor.Interceptor.class, INTERCEPTOR_ANNOTATION_TYPE);
42+
assertEquals(InterceptorBinding.class, INTERCEPTOR_BINDING_ANNOTATION_TYPE);
43+
assertEquals(AroundInvoke.class, AROUND_INVOKE_ANNOTATION_TYPE);
44+
assertEquals(AroundTimeout.class, AROUND_TIMEOUT_ANNOTATION_TYPE);
45+
assertEquals(AroundConstruct.class, AROUND_CONSTRUCT_ANNOTATION_TYPE);
46+
assertEquals(PostConstruct.class, POST_CONSTRUCT_ANNOTATION_TYPE);
47+
assertEquals(PreDestroy.class, PRE_DESTROY_ANNOTATION_TYPE);
48+
}
49+
50+
@Test
51+
public void testIsInterceptorClass() {
52+
isInterceptorClass(ExternalInterceptor.class);
53+
}
54+
55+
@Test
56+
public void testValidateInterceptorClass() {
57+
validateInterceptorClass(ExternalInterceptor.class);
58+
testOnError(() -> validateInterceptorClass(String.class), IllegalStateException.class);
59+
testOnError(() -> validateInterceptorClass(A.class), IllegalStateException.class);
60+
testOnError(() -> validateInterceptorClass(B.class), IllegalStateException.class);
61+
}
62+
63+
protected void testOnError(Runnable runnable, Class<? extends Throwable> errorClass) {
64+
Throwable throwable = null;
65+
try {
66+
runnable.run();
67+
} catch (Throwable e) {
68+
throwable = e;
69+
}
70+
assertTrue(errorClass.isAssignableFrom(throwable.getClass()));
71+
}
72+
73+
@Test
74+
public void testIsAnnotatedInterceptorBinding() {
75+
assertTrue(isAnnotatedInterceptorBinding(Logging.class));
76+
assertTrue(isAnnotatedInterceptorBinding(Monitored.class));
77+
assertFalse(isAnnotatedInterceptorBinding(Override.class));
78+
}
79+
80+
@Interceptor
81+
abstract class A {
82+
}
83+
84+
@Interceptor
85+
final class B {
86+
}
87+
}

0 commit comments

Comments
 (0)