Skip to content

Commit 794b06a

Browse files
committed
Polish code
1 parent c06c56f commit 794b06a

File tree

9 files changed

+162
-51
lines changed

9 files changed

+162
-51
lines changed

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

+2-30
Original file line numberDiff line numberDiff line change
@@ -79,36 +79,8 @@ protected String getBeanName(Class beanClass) {
7979

8080
@Override
8181
public T create(CreationalContext<T> creationalContext) {
82-
T instance = null;
83-
84-
Map<AnnotatedConstructor, List<ConstructorParameterInjectionPoint>> injectionPointsMap =
85-
getConstructorParameterInjectionPointsMap();
86-
87-
try {
88-
if (injectionPointsMap.isEmpty()) { // non-argument constructor
89-
instance = (T) getBeanClass().newInstance();
90-
} else { // @Inject constructor
91-
// just only one Constructor annotated @Inject
92-
Map.Entry<AnnotatedConstructor, List<ConstructorParameterInjectionPoint>> entry =
93-
injectionPointsMap.entrySet().iterator().next();
94-
List<ConstructorParameterInjectionPoint> injectionPoints = entry.getValue();
95-
Object[] arguments = new Object[injectionPoints.size()];
96-
AnnotatedConstructor annotatedConstructor = entry.getKey();
97-
Constructor constructor = annotatedConstructor.getJavaMember();
98-
int i = 0;
99-
BeanManager beanManager = getBeanManager();
100-
for (ConstructorParameterInjectionPoint injectionPoint : injectionPoints) {
101-
if (constructor == null) {
102-
constructor = injectionPoint.getMember();
103-
}
104-
arguments[i++] = beanManager.getInjectableReference(injectionPoint, creationalContext);
105-
}
106-
instance = (T) constructor.newInstance(arguments);
107-
}
108-
creationalContext.push(instance);
109-
} catch (Throwable e) {
110-
throw new CreationException(e);
111-
}
82+
T instance = super.create(creationalContext);
83+
// TODO
11284
return instance;
11385
}
11486

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public boolean intercepts(InterceptionType type) {
9393
// TODO
9494
break;
9595
}
96-
return false;
96+
return supported;
9797
}
9898

9999
@Override
@@ -110,21 +110,19 @@ public Object intercept(InterceptionType type, T instance, InvocationContext ctx
110110

111111
@Override
112112
public T create(CreationalContext<T> creationalContext) {
113-
// TODO
114113
return super.create(creationalContext);
115114
}
116115

117116
@Override
118117
public void destroy(T instance, CreationalContext<T> creationalContext) {
119-
// TODO
120118
super.destroy(instance, creationalContext);
121119
}
122120

123121
@Override
124122
protected void validate(Class interceptorClass) {
125123
this.interceptorManager.validateInterceptorClass(interceptorClass);
126124
}
127-
125+
128126
@Override
129127
public Annotated getAnnotated() {
130128
return interceptorType;

projects/stage-1/middleware-frameworks/my-commons/src/main/java/org/geektimes/commons/reflect/util/ConstructorUtils.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import static java.util.Arrays.asList;
2424
import static org.geektimes.commons.function.Streams.filter;
25-
import static org.geektimes.commons.reflect.util.MemberUtils.isPublic;
25+
import static org.geektimes.commons.reflect.util.MemberUtils.isPrivate;
2626

2727
/**
2828
* The utilities class of {@link Constructor}
@@ -33,20 +33,21 @@
3333
public abstract class ConstructorUtils {
3434

3535
/**
36-
* Is a public no-arg constructor or not ?
36+
* Is a non-private constructor without parameters
3737
*
3838
* @param constructor {@link Constructor}
3939
* @return <code>true</code> if the given {@link Constructor} is a public no-arg one,
4040
* otherwise <code>false</code>
4141
*/
42-
public static boolean isPublicNoArgConstructor(Constructor<?> constructor) {
43-
return isPublic(constructor) && constructor.getParameterCount() == 0;
42+
public static boolean isNonPrivateConstructorWithoutParameters(Constructor<?> constructor) {
43+
return !isPrivate(constructor) && constructor.getParameterCount() < 1;
4444
}
4545

46-
public static boolean hasPublicNoArgConstructor(Class<?> type) {
46+
public static boolean hasNonPrivateConstructorWithoutParameters(Class<?> type) {
47+
Constructor<?>[] constructors = type.getDeclaredConstructors();
4748
boolean has = false;
48-
for (Constructor<?> constructor : type.getConstructors()) {
49-
if (isPublicNoArgConstructor(constructor)) {
49+
for (Constructor<?> constructor : constructors) {
50+
if (isNonPrivateConstructorWithoutParameters(constructor)) {
5051
has = true;
5152
break;
5253
}

projects/stage-1/middleware-frameworks/my-commons/src/main/java/org/geektimes/commons/reflect/util/MethodUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static Set<Method> getMethods(Class<?> declaringClass, boolean includeInh
8484
}
8585
}
8686

87-
return unmodifiableSet(filter(allMethods, methodsToFilter));
87+
return filter(allMethods, methodsToFilter);
8888
}
8989

9090
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.commons.reflect.util;
18+
19+
import java.lang.reflect.Method;
20+
import java.lang.reflect.Proxy;
21+
import java.util.Set;
22+
import java.util.function.Predicate;
23+
24+
import static org.geektimes.commons.function.Predicates.and;
25+
import static org.geektimes.commons.reflect.util.ClassUtils.isFinal;
26+
import static org.geektimes.commons.reflect.util.ClassUtils.*;
27+
import static org.geektimes.commons.reflect.util.ConstructorUtils.hasNonPrivateConstructorWithoutParameters;
28+
import static org.geektimes.commons.reflect.util.MemberUtils.*;
29+
import static org.geektimes.commons.reflect.util.MethodUtils.OBJECT_METHOD_PREDICATE;
30+
import static org.geektimes.commons.reflect.util.MethodUtils.getAllDeclaredMethods;
31+
32+
/**
33+
* The utilities class for {@link Proxy Proxy}
34+
*
35+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
36+
* @since 1.0.0
37+
*/
38+
public abstract class ProxyUtils {
39+
40+
/**
41+
* <ul>
42+
* <li>class has a non-private constructor with no parameters</li>
43+
* <li>class is not declared final</li>
44+
* <li>class does not have non-static, final methods with public, protected or default visibility</li>
45+
* <li>class is not primitive type</li>
46+
* <li>class is not array type</li>
47+
* </ul>
48+
*
49+
* @param type
50+
* @return
51+
*/
52+
public static boolean isProxyable(Class<?> type) {
53+
if (isArray(type)) {
54+
return false;
55+
}
56+
57+
if (isPrimitive(type)) {
58+
return false;
59+
}
60+
61+
if (isFinal(type)) {
62+
return false;
63+
}
64+
65+
if (!hasNonPrivateConstructorWithoutParameters(type)) {
66+
return false;
67+
}
68+
69+
Predicate<Method> predicate = and(NON_STATIC_METHOD_PREDICATE, FINAL_METHOD_PREDICATE,
70+
NON_PRIVATE_METHOD_PREDICATE, OBJECT_METHOD_PREDICATE.negate());
71+
72+
Set<Method> methods = getAllDeclaredMethods(type, predicate);
73+
74+
if (!methods.isEmpty()) {
75+
return false;
76+
}
77+
78+
return true;
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.commons.reflect.util;
18+
19+
import org.junit.Test;
20+
21+
import static org.geektimes.commons.reflect.util.ProxyUtils.isProxyable;
22+
import static org.junit.Assert.assertFalse;
23+
import static org.junit.Assert.assertTrue;
24+
25+
/**
26+
* {@link ProxyUtils} Test
27+
*
28+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
29+
* @since 1.0.0
30+
*/
31+
public class ProxyUtilsTest {
32+
33+
@Test
34+
public void testIsProxyable() {
35+
assertTrue(isProxyable(getClass()));
36+
assertFalse(isProxyable(int.class));
37+
assertFalse(isProxyable(int[].class));
38+
assertFalse(isProxyable(String.class));
39+
assertFalse(isProxyable(A.class));
40+
assertFalse(isProxyable(B.class));
41+
}
42+
43+
static class A {
44+
45+
A(Object... args) {
46+
47+
}
48+
}
49+
50+
static class B {
51+
public final String toString() {
52+
return "B";
53+
}
54+
}
55+
}

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,14 @@ public void registerInterceptorBindingType(Class<? extends Annotation> intercept
161161

162162
@Override
163163
public boolean isInterceptorBindingType(Class<? extends Annotation> annotationType) {
164-
return isMetaAnnotation(annotationType, InterceptorBinding.class) ||
165-
interceptorBindingTypes.contains(annotationType);
164+
if (interceptorBindingTypes.contains(annotationType)) {
165+
return true;
166+
}
167+
if (isMetaAnnotation(annotationType, InterceptorBinding.class)) {
168+
registerInterceptorBindingType(annotationType);
169+
return true;
170+
}
171+
return false;
166172
}
167173

168174
@Override
@@ -177,7 +183,7 @@ public Set<Class<? extends Annotation>> getInterceptorBindingTypes() {
177183

178184
@Override
179185
public boolean isInterceptorClass(Class<?> interceptorClass) {
180-
if(interceptorInfoRepository.containsKey(interceptorClass)){
186+
if (interceptorInfoRepository.containsKey(interceptorClass)) {
181187
return true;
182188
}
183189
return InterceptorUtils.isInterceptorClass(interceptorClass);

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,11 @@ default List<Object> resolveInterceptors(Constructor<?> constructor, Object... d
151151
List<Object> resolveInterceptors(Executable executable, Object... defaultInterceptors);
152152

153153
/**
154+
* Register an {@linkplain javax.interceptor.Interceptor @Interceptor} binding {@link Class type}
155+
* whether it adds {@link InterceptorBinding} or not.
154156
* <p>
155-
* Declares an annotation type as an {@linkplain javax.interceptor.Interceptor @Interceptor} binding type if you
156-
* wish to make an annotation an interceptor binding type without adding {@link InterceptorBinding} to it.
157-
* </p>
158157
*
159-
* @param interceptorBindingType
158+
* @param interceptorBindingType {@linkplain javax.interceptor.Interceptor @Interceptor} binding {@link Class type}
160159
*/
161160
void registerInterceptorBindingType(Class<? extends Annotation> interceptorBindingType);
162161

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
@@ -34,7 +34,7 @@
3434
import static org.geektimes.commons.function.ThrowableSupplier.execute;
3535
import static org.geektimes.commons.lang.util.AnnotationUtils.isAnnotationPresent;
3636
import static org.geektimes.commons.lang.util.AnnotationUtils.isMetaAnnotation;
37-
import static org.geektimes.commons.reflect.util.ConstructorUtils.hasPublicNoArgConstructor;
37+
import static org.geektimes.commons.reflect.util.ConstructorUtils.hasNonPrivateConstructorWithoutParameters;
3838

3939
/**
4040
* The utilities class for {@link Interceptor}
@@ -272,7 +272,7 @@ private static void validateInterceptorClassModifiers(Class<?> interceptorClass)
272272
}
273273

274274
private static void validateInterceptorClassConstructors(Class<?> interceptorClass) {
275-
if (!hasPublicNoArgConstructor(interceptorClass)) {
275+
if (!hasNonPrivateConstructorWithoutParameters(interceptorClass)) {
276276
throw newIllegalStateException("The Interceptor class[%s] must have a public no-arg constructor!",
277277
interceptorClass.getName());
278278
}

0 commit comments

Comments
 (0)